First I turned on ESP32 and started to develop a concept for the project. The concept development is on this page:
After the first stage, I went in search of learning more about programming C++ and I tested some basic led actions
After the first tests, I started a more complex test. I wanted to try through a button to light a led and after it was pressed again and the next led would light and so on. For this, I needed to use the switch function so that each led was identified individually and had an independent behavior.
#define button 15 //Push button on 15
#define redLED 16 //red led on 16
#define greenLED 17 //green led on 17
#define yellowLED 18 //yellow led on 18
int state = 0; //current state
int old = 0; //last state
int buttonPoll = 0; //button state
void setup () {
//Serial.begin(115200);
pinMode(button, INPUT); //button as input
pinMode(redLED, OUTPUT); //LEDs as output
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
//Serial.println("Finish Mode");
digitalWrite(redLED,LOW);
digitalWrite(greenLED,LOW);
digitalWrite(yellowLED, LOW);
}
void loop(){
//debouncing routine
buttonPoll = digitalRead(button);
if (buttonPoll == 1){ //check if it has been pressed
delay(50); //wait 50ms
buttonPoll = digitalRead (button); //poll button again
if(buttonPoll == 0){ //if it is 0 considered one press
Serial.println("Button Pressed!");
state = old + 1; //increase state by 1
}
}
else{ //if button has not been pressed
delay(100); //wait 100ms
}
switch(state){ //react to button press and state
case 1: //if state is 1
digitalWrite (redLED,HIGH); //red LED on
digitalWrite(greenLED, LOW); //green LED off
digitalWrite(yellowLED,LOW); //yellow led off
old = state; //set old state as current state
break;
case 2:
digitalWrite (redLED,LOW);
digitalWrite(greenLED, HIGH);
digitalWrite(yellowLED,LOW);
old = state;
break;
case 3:
digitalWrite (redLED,LOW);
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED,HIGH);
old = state;
break;
default:
digitalWrite(redLED, LOW); //if state is not 1,2,3
digitalWrite(greenLED, LOW); //all LED off
digitalWrite(yellowLED,LOW);
old = 0; //reset to all off/state 0
break;
}
}