Suppose I have 4 led:when the enter ‘1’(all leds turn on),‘2’(all leds flash),‘3’(leds cycle repeatedly),‘e’(leds turn off),+ or -(is pressed the spee

int first_led = 2;
int second_led = 3;
int third_led = 4;
int fourth_led = 5;

int val = 0;

void setup(){
  Serial.begin(9600);
  pinMode(first_led,OUTPUT);
  pinMode(second_led,OUTPUT);
  pinMode(third_led,OUTPUT);
  pinMode(fourth_led,OUTPUT);  
  }

 void loop()
{
  if(Serial.available()>0) { 
  val = Serial.read(); 
  Serial.println(val); 
} 
  if(val == 1){
    digitalWrite(first_led, HIGH); 
    delay(1000);
    digitalWrite(second_led, HIGH);
    delay(1000); 
    digitalWrite(third_led, HIGH); 
    delay(1000);
    digitalWrite(fourth_led, HIGH); 
    delay(1000);
  }else{
    digitalWrite(first_led, LOW); 
    delay(1000);
    digitalWrite(second_led, LOW);
    delay(1000); 
    digitalWrite(third_led, LOW); 
    delay(1000);
    digitalWrite(fourth_led, LOW); 
    delay(1000);
  }
  
 }

Hello jamyneck643
It seems to be a school assigment, isn´t it?
And your questions is?

It is very unlikely that val will ever be 1 with serial data from serial monitor. What does the Serial.read() return?

No sure with your problem kindly properly elaborate and is it a assignment given to u to help u further

my question, how to user input work?

Hello jamyneck643
Generaly spoken the sketch needs the following basic functions:

  1. command line interpreter
  2. a FSM
  3. a timer modul with associated functions like startTimer() and stopTimer().
  4. some arrays for the prepared led states to be used.

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.