These are chat archives for esp8266/Arduino

28th
Jan 2018
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 22:20
Hello
I wonder if anyone could explain this code in C++/Arduino.
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 22:27
int led = 4;
int speed = 100;
void setup(){
pinMode(2, INPUT);
pinMode(12, INPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
}
void loop(){
if (digitalRead(2) == HIGH){
  speed = 50;
}
else {
  speed = 150;
}
if (digitalRead(12) == HIGH){
digitalWrite(led, HIGH);
} else {
  digitalWrite(led, LOW);
led = led + 1;
if (led > 8) {
led = 4;
}
  digitalWrite(led, HIGH);
  delay(speed);
}
}
I do not understand about these speed set to 50 or 150 in the if statements
But I was typing this and I think I get it.. I did not see the 'delay(speed)' at the end. So if pin 2 is HIGH (on) and speed will contain 50 otherwise speed will contain 150. Then digitalWrite(led, HIGH) will be on depending on the speed (seconds).
Myles Eftos
@madpilot
Jan 28 2018 22:30
If what ever is connected to pin 2 is high, then toggle the LED, and wait 50 milliseconds. If it is low, toggle the LED and wait 150 milliseconds
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 22:36
Oh ok I get it, I don't remember what pin 2 is but pin 12 is the push button, so if I push the button then the pin 4 will be high otherwise pin 4 will be low but only things that I am confused is why there is led = led +1 if pin 4 is low. I understand that led increase by 1, between pin 4 to 8.
if pin 12 is low*
Myles Eftos
@madpilot
Jan 28 2018 22:37
I assume you have 5 LEDs wired up to pins 4, 5, 6, 7, 8
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 22:37
Yeah
Myles Eftos
@madpilot
Jan 28 2018 22:37
Every time it loops through the code, it accesses the next LED in the sequence.
After it talks to the LED on pin 8, it goes back to the LED on pin 4
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 22:38
Ok I understand that part
Ok, I am just guessing, if I press and hold the push button then not press then it randomly pick any led between 4 to 8 to be high?
Myles Eftos
@madpilot
Jan 28 2018 22:40
I think the delay is on the wrong line.
If the button is pressed, it stops on an LED
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 22:40
oh yeah
Myles Eftos
@madpilot
Jan 28 2018 22:40
It would probably look random
Is this like a skill tester or something?
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 22:41
Not really.. I think we are just playing around with the part, hold on I will link you about this part
Myles Eftos
@madpilot
Jan 28 2018 22:42
You need to swap the digitalWrite(led, HIGH) and delay(speed) lines around
Otherwise the LED is transitioning from LOW to HIGH faster than you can see it
Assuming you want to see if flash
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 22:44
Sorry I can't tell if the code is wrong but I missed the class so I had to copy and paste the code in the Arduino IDE from oneNote.
I wish I have my own digital sandbox so I can play around with it
Myles Eftos
@madpilot
Jan 28 2018 22:45
Pin 2 is the slide switch. That controls the flash speed.
Ah. Wait
I see what it's trying to do.
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 22:46
It just say on or off switch, I'm assuming if the switch is off, speed will contain 50 otherwise 150
Myles Eftos
@madpilot
Jan 28 2018 22:46
The LEDs will chase at the selected speed, unless you hold do the push button in which case they will stop.
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 22:47
Oh, this does make sense
So depending on the switch, LEDS will chase at the selected speed.
Myles Eftos
@madpilot
Jan 28 2018 22:49
So the basic flow: Turn off the last LED that was turned on, increment the LED counter (If it is bigger than 8, set it to 4), then turn on the next LED. Sleep for some time (depending on the slide switch). Keep doing that. If the use holds the push button, keep the current LED on.
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 22:49

about this line

if (digitalRead(12) == HIGH){
digitalWrite(led, HIGH);
}

LEDs will chase at the selected speed depending on the switch, so if I press down the button (pin 12) then pin 4 will high. Correct?

Myles Eftos
@madpilot
Jan 28 2018 22:50
Yup
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 22:50
Ok I am closer understanding this clearing
Okay this line is what confused me the most
} else {
  digitalWrite(led, LOW);
led = led + 1;
if (led > 8) {
led = 4;
}
if pin 12 is not high, led increase by 4 and if led is greater than 8, it go back to 4. But what does this do exactly?
led increase by 1*
Myles Eftos
@madpilot
Jan 28 2018 22:53
If pin 12 is low, then set the LED on pin $led to LOW. Add 1 to $led. If the value of $led is greater than 8 (Which is the last pin with an LED on it), set $led to 4 (Which is the first pin with an LED on it).
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 22:54
So none of the led will be high?
Myles Eftos
@madpilot
Jan 28 2018 22:55
Might be easier if you think about it this way. Say LED 4 is high (led = 4). This code turns off LED 4, then adds one to the led variable. The next line then turns on LED 5 (because led now = 5)
On the next run, the code turns off LED 5, adds on to the led variable (so it is now 6) then turns on LED 6
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 22:57
I get what you are saying but how is each led turned on?
Myles Eftos
@madpilot
Jan 28 2018 22:57
If LED 8 was on, first we turn off LED 8. Adding one to it will make it 9. There is no LED on pin 9 - so, instead, it sets the led variable to 4, turning on LED 4.
digitalWrite(led, HIGH); on the next line
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 22:57
oh facepalm
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 23:04
@madpilot I was just reviewing the code, now I understand clearly. Thank you!
Myles Eftos
@madpilot
Jan 28 2018 23:04
No worries
Not sure if it was a cut/paste issue, but if you indent the code a bit better, it'll be easier to read and understand what is going on
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 23:16
Oops, I forgot to ask you a question
Why is speed set to 100? Does it matter?
Myles Eftos
@madpilot
Jan 28 2018 23:16
It doesn't matter - it gets set to 50 or 150 on the first run on the loop anyway.
Saboor Malik
@ProgrammingForFun01
Jan 28 2018 23:18
Ah, I understand
Thanks again
Myles Eftos
@madpilot
Jan 28 2018 23:19
No worries!