//20240612 prj18 while loop use slide switch
//program use switchs control leds, array structure
//in working Lab use 1 slide(no bauce), hardware connect together in Simulation
// software we write command to each 1 slide control 4 leds ledleft()
// sw1 mean slide control 4 lcds function ledleft()
//important this program use command pinMode(pinsw[i], INPUT); Cannot use INPUT_PULLUP
//because wokwi has bug, We slove the problem by use R 100k connect to 3.3V and pin slide
//normal hardware Simulation can use command pinMode(pinsw[i], INPUT_PULLUP); and No need
//to use R 100k connect to 3.3V and pin slide, as surely It work
int pin[]={15, 4, 5, 18}; // array of pins leg led
int pinsw[]={13, 12}; // array of pins led switch
int sw1; // switch1(pin13) control red , yellow led(pin15, 4)
int sw2; // switch2(pin12) control green, blue led(pin5, 18)
int swcurrent=0; // swcurrent for ทำงานค้าง
//-------
void setup(){
Serial.begin(115200); // Serial monitor
// init pin leds array
for(int i=0; i<=3; i++) {
pinMode(pin[i], OUTPUT); // init pin array keep leg pin LED
}
// init pin switchs array
for(int i=0; i<=1; i++) {
pinMode(pinsw[i], INPUT); // init pinsw array keep leg pin SWITCH
} // use INPUT_PULLUP มี R ในตัว
}
//-------
void loop(){
sw1 = digitalRead(pinsw[0]); // 13=pinsw[0]
Serial.print("sw1= "); // serial monitor
Serial.println(sw1);
int val = 0;
while (sw1 == 0 && val <=2){
ledleft();
val++;
Serial.print("val= "); // serial monitor
Serial.println(val);
}
}
//- eof loop
////////// function led left
void ledleft(){
digitalWrite(pin[0], HIGH); // output red led light
delay(500);
digitalWrite(pin[0], LOW); // output red led not bright
delay(500);
digitalWrite(pin[1], HIGH); // output yellow led light
delay(500);
digitalWrite(pin[1], LOW); // output yellow led not bright
delay(500);
digitalWrite(pin[2], HIGH); // output green led light
delay(500);
digitalWrite(pin[2], LOW); // output green led not bright
delay(500);
digitalWrite(pin[3], HIGH); // output blue led light
delay(500);
digitalWrite(pin[3], LOW); // output blue led not bright
delay(500);
}
//// eof function ledleft()
////////// function led right
void ledright(){
digitalWrite(pin[3], HIGH); // output blue led light
delay(500);
digitalWrite(pin[3], LOW); // output blue led not bright
delay(500);
digitalWrite(pin[2], HIGH); // output green led light
delay(500);
digitalWrite(pin[2], LOW); // output green led not bright
delay(500);
digitalWrite(pin[1], HIGH); // output yellow led light
delay(500);
digitalWrite(pin[1], LOW); // output yellow led not bright
delay(500);
digitalWrite(pin[0], HIGH); // output red led light
delay(500);
digitalWrite(pin[0], LOW); // output red led not bright
delay(500);
}
//// eof function ledright()
////////// function led in out sequential
void ledinout(){
digitalWrite(pin[0], HIGH); // output red led light
delay(500);
digitalWrite(pin[1], HIGH); // output yellow led light
delay(500);
digitalWrite(pin[2], HIGH); // output green led light
delay(500);
digitalWrite(pin[3], HIGH); // output blue led light
delay(500);
delay(500);
digitalWrite(pin[0], LOW); // output red led not bright
delay(500);
digitalWrite(pin[1], LOW); // output yellow led not bright
delay(500);
digitalWrite(pin[2], LOW); // output green led not bright
delay(500);
digitalWrite(pin[3], LOW); // output blue led not bright
delay(500);
}
//// eof function ledinout() sequential
// eof file