Programming question in LED's (Stuck)

I am fairly new to programming but wanted to seek advise on how to code this question. **Set up the serial monitor. A user will enter a single digit number, 0 through 9. Make the LED blink that
many times. For example, if the user enters 5, make the LED blink 5 times. Then wait for more user
input to do this again.

When is our assignment due?
You could use a for loop and a delay or two.
Bonus marks if you use neither.

Are you new enough to the Arduino not to have tried the Blink, BlinkWithoutDelay and ReadASCIIString examples in the IDE ?

What have you tried?

well here's what I would do, something to start you off (using a for loop AND a delay, see if you can get rid of them!)

void setup(){
Serial.begin(115200);                           //init. serial communication
    pinMode(*your pin number*, OUTPUT);     
 }

void loop(){
Serial.println("Enter number between 0-9");
if (Serial.available() >0){                    // check if any characters have been entered
   int Num = Serial.read();                    // if they have, store character in variable 'Num'
   for(int a = 1; a <= Num; a++){             // execute for loop n times
      digitalWrite(*your pin number*, HIGH);
      delay(500);
      digitalWrite(*your pin number*, LOW);
      delay(500);
    }
  }
}

If there is no serial input with that code, it will keep sending the prompt over and over. eriknyquist, see if YOU can get rid of that.

ha. Yes you are right. Bad Erik! Bad!

void setup(){
Serial.begin(115200);                           //init. serial communication
    pinMode(8, OUTPUT); 
Serial.println("Enter number between 0-9");    
 }

void loop(){
if (Serial.available() >0){                    // check if any characters have been entered
   int Num = Serial.read();                    // if they have, store character in variable 'Num'
   Num -= 48;                                  // minus 48 because int 0-9 is actually ASCII 48-57
                        
   for(int a = 1; a <= Num; a++){             // for loop:
      digitalWrite(8, HIGH);                     //              everything inside the for loop's curly
      delay(500);                                     //               brackets will be executed
      digitalWrite(8, LOW);                     //               n times, where n is the value entered  
      delay(500);                                    //                by the user and stored in variable 'Num'
    }                                                    //close the for loop
  }                                                      // close the if statement
}                                                        //close main loop

ain't perfect but I tested it with a board and it works.

Should that not read:

int Num = Serial.read()  - '0';

Because Num would be the ASCII value of the character he typed.

EDIT: That was for your original post Erik. Your modified code is correct.

Num -= '0';  is much easier to read

eriknyquist:
ain't perfect but I tested it with a board and it works.

Yeah, but giving the OP the complete solution to what is obviously an academic exercise completely negates the point of the exercise.

oh well. There's plenty of unsolved excercises out there.

eriknyquist:
oh well. There's plenty of unsolved excercises out there.

Yes, but this particular person is now likely to hand in his homework without having learned a thing from it.

it's up to him wether he learns anything. If he copies and pastes the code without giving it a second thought, then no he won't learn anything. If he makes an effort to understand it and optimise it, then great!

If I hadn't given a solution, then he would've just googled for loops and ended up copying an example anyway.

What did you expect? that he would think really really hard and then suddenly have an understanding of something he previously didn't know how to do?

The point is to try and get them to understand. Giving them the full code is ok... sometimes; I do it myself, when it is necessary. But if they know nothing at all, then make them do some research. Don't give them anything unless they provide you with something, and have some clue as to what and where the problem is.

I absolutely agree with HazardsMind here.
Let them post some code - it may be embarrassing, but it shows a will to learn.

fair points. There is also the fact that I too am new to this, and solving his problem has helped me to learn too :slight_smile:
duly noted. will think first next time.