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
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
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?
for your consideration, a more robust way to handle buttons, and checking for change before printing https://wokwi.com/projects/436961657173079041
thanks
what does it mean when int has ()
int checkButton()
?yes
the
checkButton
function returns an int
eger
void setup()
means the setup
function does not return anything (void
)oh
okay, so the intside of int()
you can write the inside result to the int()
idk
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;
hmm
loop calls to
checkButton
- "Hey, what is the count?" checkButton
returns the answerIm propanly gonna get this
when I have got some sleep
I havent slept
sleep is an important part of learning 😉
Im gonna read these tomorrow, actually today as its 6AM
yep
but thanks anyway
have a good night (day?) feel free to ask questions
you too,