I have made two temperature/humidity devices for my workspace. One is plugged in for the room, and the other is using a battery inside my filament dry box. I used a Raspberry Pi Pico, a temp/humidity sensor, and an OLED display. The code was generated by Claude. I say this as background. I can get things working, but I want to understand more deeply. To that end, I am working my way through the Elegoo The Most Complete Starter Kit MEGA 2560 Project that I purchased a few years ago.
I am doing Lesson 5 Digital Inputs, and of course, the project is working because there are diagrams, etc. I am, however, having difficulty understanding what is happening in the code. Their explanation is lackluster for my small brain. Clearly, pushing one of the two buttons closes the circuit to light the LED while the other opens it to turn it off, but I am unclear as to how this is happening via the code and how the Arduino hardware is functioning. If anyone would be willing to explain this so teenager could understand it (I do not think we need to go back to a five year old), I would really appreciate it. The Elegoo code is as follows:
//www.elegoo.com
//2016.12.08
int ledPin = 5;
int buttonApin = 9;
int buttonBpin = 8;
byte leds = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(buttonApin) == LOW)
{
digitalWrite(ledPin, HIGH);
}
if (digitalRead(buttonBpin) == LOW)
{
digitalWrite(ledPin, LOW);
}
}
//www.elegoo.com
//2016.12.08
int ledPin = 5;
int buttonApin = 9;
int buttonBpin = 8;
byte leds = 0; //this variablke is not used
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP); //set pin as an input and turn on the built in pullup resistor
pinMode(buttonBpin, INPUT_PULLUP); //to keep the pin HIGH when the button is not pressed
}
void loop()
{
if (digitalRead(buttonApin) == LOW) //if buttonA is pressed the input will be LOW
{
digitalWrite(ledPin, HIGH); //so set the LED pin HIGH. Presumably this turs it on
}
if (digitalRead(buttonBpin) == LOW) //if buttonB is pressed the input will be LOW
{
digitalWrite(ledPin, LOW); //so set the LED pin LOW. Presumably this turs it off
}
}
It's all a matter of logic and speed.
Let me explain: If you press button "A", the LED will turn on, but it will only stay on while it is pressed.
Button "B" doesn't make sense to me, because as soon as you release button "A", the LED goes off, so what would be the purpose of button "B" if the LED is already off?
Continuing;
If you keep button "A" pressed, the LED will remain on, and then press button "B", the LED will always appear to be off, because the time that button "A" stays on is so fast that your eyes won't feel it being on.
Sorry, my explanition is wrong.
I will rewite.
I got distracted and wrote it wrong.
If you press button "A", the LED will turn on.
If you press button "B", the LED will turn off.
Continuing;
If you keep button "A" pressed, the LED will remain on, and then press button "B", the LED will always appear to be off, because the time that button "A" stays on is so fast that your eyes won't feel it being on.
Actually, when you press the "A" button the light turns on and stays on even when you release the switch. Once you press the "B" button the light turns off and stays off even when you release the switch.
So ledPin is set to ouput and pressing button "A" sets ledPin to high (electricity flowing and the LED turns on), and pressing button "B" sets ledPin low (no electricity flowing and the LED turns off)? Am I getting close? I mean, perhaps high and low are not on and off, but one voltage that is considered "high" and one that is considered "low". I will check with my multimeter in a second.
Setting a pin HIGH will output 5V or close to it and LOW 0V or close to it. Whether that turns the LED connected to the pin on or off depends on how tou have the LED wired. I assume that you have a current limiting resistor in series with the LED
I believe I do. The Arduino allows you to set the voltage pin (ledPin) HIGH or LOW. By assigning the switches to change that value it either provides power to the led (closed circuit) or cuts power off (open circuit). Have I got it?