Pages: [1]   Go Down
Author Topic: Stop for / while loops with a serial command  (Read 300 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Need some help with this sketch.

The rgb fade stops with any input from serial, but doesnt respond to any serial commands after...

Any help would be appreciated.

Code:
const int redPin = 11;
const int greenPin = 10;
const int bluePin = 9;
boolean rgbfade = false;

void setup() {
setColourRgb(0,0,0);
// initialize serial communication:
  Serial.begin(57600);
}
 
void fade() {
unsigned int rgbColour[3];

rgbColour[0] = 255;
rgbColour[1] = 0;
rgbColour[2] = 0;

while (rgbfade = true){

for (int decColour = 0; decColour < 3; decColour += 1) {
int incColour = decColour == 2 ? 0 : decColour + 1;

for(int i = 0; i < 255; i += 1) {
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
 
delay(5);
if (Serial.available() > 0) {
         rgbfade = false;
         break;
    }
   }
  }
 }
}

void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

void loop() {
 
  if (Serial.available() > 0) {
    int inByte = Serial.read();
   
    switch (inByte) {
    case '1': //   start fading rgb
      rgbfade = true;   
      fade();
      break;
    case '2': // turn off fading and turn on red   
      setColourRgb(0, 255, 0);
      rgbfade = false;
      break;
   
      default: // turn off fading and shut all colors off
      setColourRgb(0, 0, 0);
      rgbfade = false;
      break;
     
    }
  }
}

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49252
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
while (rgbfade = true){
Why are you assigning a value (=), here, instead of testing (==)?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset




You gotta be kidding... That was it??!?!


Your the man paul!
Thanks!
Logged

Pages: [1]   Go Up
Jump to: