Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

led - How to PWM ground on an Arduino?

I have a strip of RGB LEDs that came with one single light controller. I used about half the roll, and am left with the rest of the roll without another controller.

I want to see if I can use an Arduino to cycle through the colors just for fun. Upon further testing, the way they control the RGB is not through varying the positive, but by varying the negative. is there any way I can do this with the Arduino Mega?

I have already tried hooking up the positive and negative the correct and opposite way on both sides, and it still doesn't work the way I need it to.

Here's the code I use. I don't have any RGB LEDs, but separate red, green, and blue LEDs to show that it still works perfectly.

#define Rpin 3  // sets up
#define Gpin 5  // the pins
#define Bpin 7  // for easy writing

void setup() {
  // initialize digital pins for output.
  pinMode(Rpin, OUTPUT);
  pinMode(Gpin, OUTPUT);
  pinMode(Bpin, OUTPUT);
}

int phase = 0;        // phase for loop
int Max = 127;        // easier for
int i = 1;            // changing a lot
int maxAll = Max - i; // of values

int red = 0;          // sets up
int green = 0;        // rgb colors
int blue = Max;       // for loop

// the loop runs over and over to change the rgb values.
void loop() {
 if (phase == 0) {
    red += i;
    blue -= i;
    if (red >= maxAll) {
      phase = 1;
      red = Max;
      blue = 0;
    }
  } else if (phase == 1) {
    red -= i;
    green += i;
    if (green >= maxAll) {
      phase = 2;
      red = 0;
      green = Max;
    }
  } else if (phase == 2) {
    green -= i;
    blue += i;
    if (blue >= maxAll) {
      phase = 0;
      green = 0;
      blue = Max;
    }
  }
  
  analogWrite(Rpin, red);   // write to each led for the ease of typing
  analogWrite(Gpin, green); //
  analogWrite(Bpin, blue);  //
  delay(10);
}
question from:https://stackoverflow.com/questions/65909635/how-to-pwm-ground-on-an-arduino

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...