简体   繁体   English

使用H-Bridge和neopixel以及Arduino UNO控制直流电机

[英]Control dc-motor using H-Bridge and neopixel together with Arduino UNO

I am working on a project to control a DC motor rotation in forward and backward with an H-Bridge. 我正在一个项目中,通过H桥控制直流电动机正反旋转。 And light up an adafruit neopixel same time when circuit turns on. 并在电路接通时同时点亮一个adafruit neopixel。

I made both things working separately. 我使这两件事分开工作。 Now I combined the code but its not working as should. 现在,我合并了代码,但是它无法正常工作。 It works like the dc motor does one forward and back loop and stops. 它的工作方式类似于直流电动机执行一个正向和反向循环并停止。 Then the neopixel ring lights up. 然后,新像素环亮起。 After some time when the neopixel code finishes, the dc motor does same one loop and this cycle continues. 一段时间后,当新像素代码完成时,直流电动机执行相同的一个循环,此循环继续进行。

I need help in making this code start dc motor working and neo pixel lighting same time. 我需要使此代码同时启动直流电动机工作和新像素照明的帮助。

Here is the code: 这是代码:

#include <Adafruit_NeoPixel.h>

#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);


//DC motor
const int pwm = 3;  //initializing pin 2 as pwm
const int in_1 = 8 ;
const int in_2 = 7 ;

//For providing logic to L298 IC to choose the direction of the DC motor

void setup()
{
pinMode(pwm,OUTPUT) ;   //we have to set PWM pin as output
pinMode(in_1,OUTPUT) ;  //Logic pins are also set as output
pinMode(in_2,OUTPUT) ;

//Neopixel Ring
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

strip.begin();
strip.show(); // Initialize all pixels to 'off'


}

void loop()
{
//For Clock wise motion , in_1 = High , in_2 = Low

digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,LOW) ;
analogWrite(pwm,255) ;

/*setting pwm of the motor to 255
we can change the speed of rotaion
by chaning pwm input but we are only
using arduino so we are using higest
value to driver the motor  */

//Clockwise for 3 secs
delay(1000) ;    

//For brake
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,HIGH) ;
delay(50) ;

//For Anti Clock-wise motion - IN_1 = LOW , IN_2 = HIGH
digitalWrite(in_1,LOW) ;
digitalWrite(in_2,HIGH) ;
delay(1000) ;

//For brake
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,HIGH) ;
delay(50) ;

//Neopixel Ring

// Some example procedures showing how to display to the pixels:
  colorWipe(strip.Color(255, 0, 0), 50); // Red
  colorWipe(strip.Color(0, 255, 0), 50); // Green
  colorWipe(strip.Color(0, 0, 255), 50); // Blue
//colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW
  // Send a theater pixel chase in...
  theaterChase(strip.Color(127, 127, 127), 50); // White
  theaterChase(strip.Color(127, 0, 0), 50); // Red
  theaterChase(strip.Color(0, 0, 127), 50); // Blue

  rainbow(20);
  rainbowCycle(20);
  theaterChaseRainbow(50);
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);


}

Essentially every time you call delay, the arduino cannot do anything else. 基本上,每次您打电话延迟时,arduino都无能为力。 So you need to switch control to whatever else you wanted to be doing. 因此,您需要将控制切换到您想做的其他事情。

There are several approaches: 有几种方法:

Timers - use a timer to update the state of one of your two tasks. 计时器-使用计时器更新两个任务之一的状态。 This has the advantage that your tasks don't rely on the other to yield, but doesn't grow beyond a couple of tasks. 这样做的好处是,您的任务不依赖其他任务来完成任务,但不会超出几个任务。

Software state machine - create a state machine for each task, and in the loop increment each state machine in turn. 软件状态机-为每个任务创建一个状态机,并在循环中依次递增每个状态机。 You'll want to create a structure to keep track of the state of the task, and each time you would call delay you'd instead update the structure and return. 您将需要创建一个结构来跟踪任务的状态,并且每次调用延迟时,您都需要更新结构并返回。 Usually you'd create a class for each task, as is done in this Adafruit tutorial . 通常,您将为每个任务创建一个类,如本Adafruit教程中所述 This can be a lot of work, so there are libraries to do it for you. 这可能需要很多工作,因此有一些库可以为您完成。

Coroutines - these use a library to construct the state machines for you. 协程-这些协程使用库为您构造状态机。 eg http://forum.arduino.cc/index.php?topic=256256.0 例如http://forum.arduino.cc/index.php?topic=256256.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM