A
Arduino•2mo ago
TheStudier

Can you masterWizards give me homework.

So I was just doing the toggle switch code again and kinda realized my logical thinking isn't very good, so could You guys give me a few hard logical coding problems involving buttons and digital/analog outputs. thanks CodeWizards
16 Replies
DarwinWasWrong
DarwinWasWrong•2mo ago
Push a button - led 1 turns on push button again - led 2 turns on, led led 1 turns off push button again - led 2 turns on, led led 1 turns on push button again - all leds off
TheStudier
TheStudierOP•2mo ago
okei Imma try dat tomorrow thanks wizard thanks Ill do these both now So do you mean that the when I release the button the leds need to stay on? I made this with the leds staying on My code: int button=4; int redLed=2; int yelLed=3; int butValNow; int butValBehind; int counter; void setup() { // put your setup code here, to run once: pinMode(button,INPUT_PULLUP); pinMode(redLed,OUTPUT); pinMode(yelLed,OUTPUT); Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: butValNow=digitalRead(button); if(butValNow==LOW&&butValBehind==HIGH) { counter++; } if(counter==0) { digitalWrite(redLed,LOW); digitalWrite(yelLed,LOW); } if(counter==1) { digitalWrite(redLed,HIGH); digitalWrite(yelLed,LOW); } if(counter==2) { digitalWrite(redLed,LOW); digitalWrite(yelLed,HIGH); } if(counter==3) { digitalWrite(redLed,HIGH); digitalWrite(yelLed,HIGH); } if(counter>3) { counter=0; } Serial.print(butValNow); Serial.print(butValBehind); Serial.println(counter); butValBehind=butValNow; //laittaa but val behindin butvalnowin taakse } Imade it with out toggle too A bit more complicates int button=4; int redLed=2; int yelLed=3; int butValNow; int butValBehind; int counter; int counter2; void setup() { // put your setup code here, to run once: pinMode(button,INPUT_PULLUP); pinMode(redLed,OUTPUT); pinMode(yelLed,OUTPUT); Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: butValNow=digitalRead(button); if(butValNow==LOW&&butValBehind==HIGH) { counter++; } if(counter==1&&butValNow==HIGHcounter==2&&butValNow==HIGHcounter==3&&butValNow==HIGH) { digitalWrite(redLed,LOW); digitalWrite(yelLed,LOW); } if(counter==0) { digitalWrite(redLed,LOW); digitalWrite(yelLed,LOW); } if(counter==1&&butValNow==LOW) { digitalWrite(redLed,HIGH); digitalWrite(yelLed,LOW); } if(counter==2&&butValNow==LOW) { digitalWrite(redLed,LOW); digitalWrite(yelLed,HIGH); } if(counter==3&&butValNow==LOW) { digitalWrite(redLed,HIGH); digitalWrite(yelLed,HIGH); } if(counter>3) { counter=0; } Serial.print(butValNow); Serial.print(butValBehind); Serial.println(counter); butValBehind=butValNow; delay(100); //laittaa but val behindin butvalnowin taakse } btw what was the command to change text to code here?
AnonEngineering
AnonEngineering•2mo ago
for your consideration, a more robust way to handle buttons, and checking for change before printing https://wokwi.com/projects/436961657173079041
TheStudier
TheStudierOP•2mo ago
thanks what does it mean when int has ()
AnonEngineering
AnonEngineering•2mo ago
int checkButton() ?
TheStudier
TheStudierOP•2mo ago
yes
AnonEngineering
AnonEngineering•2mo ago
the checkButton function returns an integer void setup() means the setup function does not return anything (void)
TheStudier
TheStudierOP•2mo ago
oh okay, so the intside of int() you can write the inside result to the int() idk
AnonEngineering
AnonEngineering•2mo ago
when the loop function calls checkButton the check function returns an int so int count = checkButton();, count holds the returned value (0 - 3) line 22 return counter;
TheStudier
TheStudierOP•2mo ago
hmm
AnonEngineering
AnonEngineering•2mo ago
loop calls to checkButton - "Hey, what is the count?" checkButton returns the answer
TheStudier
TheStudierOP•2mo ago
Im propanly gonna get this when I have got some sleep I havent slept
AnonEngineering
AnonEngineering•2mo ago
sleep is an important part of learning 😉
TheStudier
TheStudierOP•2mo ago
Im gonna read these tomorrow, actually today as its 6AM yep but thanks anyway
AnonEngineering
AnonEngineering•2mo ago
have a good night (day?) feel free to ask questions
TheStudier
TheStudierOP•2mo ago
you too,

Did you find this page helpful?