Commanding LEDs from Serial port, while loop/parameter issues.

Hi guys, I'm starting to read this book ("Arduino - A quick-start guide"), and I am now working on the exercise where I have to give it more commands other than turn LED on/off. I am attempting to make it so when I give it another command, it sets off on a loop where it just keeps blinking until I give it the command 4 - where it stops and just stays that way, and as you can see at the very beginning of the code, it simply does not work - prints the line just fine, but that's as far as it goes. Now, I am waaay too used to Python, and when I try to work with while loops, it gives me errors. So I googled it, read about it, and here's how I have it so far-

//Project b.1 - command an LED from a serial port.
//Status - get the stupid while loop and function working. 
//It does not seem to work for some reason.

const unsigned int ledPin = 13;

void setup(){
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void blinkmode(){
  int blinkmodeon = 1;
  while (blinkmodeon == '1')
  {
    if (Serial.available() > 0){
      int command = Serial.read();
      if (command == '4') {
        blinkmodeon == 0;
      }
      else {
        digitalWrite(ledPin, HIGH);
      delay(500);
      digitalWrite(ledPin, LOW);
      delay(500);
      }
    }    
  }
}
void loop(){
  if (Serial.available() > 0) {
    int command = Serial.read();
    if (command == '1') {
      digitalWrite(ledPin, HIGH);
      Serial.println("Led is on.");
    }
    else if (command == '2'){
      digitalWrite(ledPin, LOW);
      Serial.println("Led is off.");
    }
    else if (command == '3'){
      Serial.println("Blink mode started. Send command '4' to stop.");
      blinkmode();
    }
    else {
      Serial.println("Huh?");
    }
  }
}

Biggest issue of mine - parameters. How do they work and how can I use them? I keep seeing thousands of examples, but I never really got to understand them fully :cold_sweat: Any help would be appreciated.

I can only suggest you read a book on C or C++, however as far as your code goes:

int blinkmodeon = 1; // sets blinkmodeon to the integer value 1 while (blinkmodeon == '1') // compares blinkmodeon to the character '1', i.e. the byte value 0x49

If you feel up to it, have a look at the blinkwithoutdelay example in the reference.

shelleycat: I can only suggest you read a book on C or C++, however as far as your code goes:

int blinkmodeon = 1; // sets blinkmodeon to the integer value 1 while (blinkmodeon == '1') // compares blinkmodeon to the character '1', i.e. the byte value 0x49

If you feel up to it, have a look at the blinkwithoutdelay example in the reference.

Considering that I don't have all the time in the universe (it's really a science project, and I have a form of a time limit), I will attempt to understand C++, but it will take me a while. Oh, and the problem for me is that when I make the integer blinkmodeon, the while loop doesn't seem to care and just ignores it, therefore making the LED not blink. Could you at least tell me which part of the code (that is not supposed to be there or missing) is not making the while loop work? :|

while (blinkmodeon == '1') // compares blinkmodeon to the character '1', i.e. the byte value 0x49

The comment here told you EXACTLY what the problem is. blinkmodeon is an integer value, but you are comparing it to the character '1'. Try comparing it to the integer value 1, and you'll probably have better results.

while (blinkmodeon == 1)

PaulS: while (blinkmodeon == '1') // compares blinkmodeon to the character '1', i.e. the byte value 0x49

The comment here told you EXACTLY what the problem is. blinkmodeon is an integer value, but you are comparing it to the character '1'. Try comparing it to the integer value 1, and you'll probably have better results.

while (blinkmodeon == 1)

-facepalm- Gah, you're right. Those books I'm reading are confusing me so much :roll_eyes: