Hello,
I just started recently using Arduino
I bought the Geekcreit Mega 2560 version but I am trying to follow the Elegoo lessons
I got it set up and I got through the first couple of lessons
I am a little bit stuck at Digital inputs Lesson 5
Since I don't have this lesson in my kit I tried to add it manually following some videos so I am not sure if my code is wrong or maybe it's not compatible with my version or something is wrong with the setup or the logic
The main problem is it's not latching or staying in that state
If the light is on and I push the button it goes off but if I let go of the button the light comes back on In the video that shows that code the light goes off and have to press the other button to turn it on or vice versa
There was another video that was simpler which was using a simple INPUT pinmode with external resistor and that worked as it should so I am not sure if the problem is with the INPUT_PULLUP or it cannot be used with this version
// Global Variables
int ledPin = 5;
int buttonApin = 9;
int buttonBpin = 8;
//int LED = 12;
//int BUTTON = 9;
byte leds = 0;
// Sketch Setup routine - run once
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
// pinMode(LED,OUTPUT);
// pinMode(BUTTON,INPUT);
}
// main loop - runs repeatedly
void loop() {
//if(digitalRead(BUTTON)==HIGH)
//{
//digitalWrite(LED, HIGH);
//}else
// {
// digitalWrite(LED, LOW);
// }
// digitalWrite(ledPin, HIGH);
if (digitalRead(buttonApin) == LOW)
{
digitalWrite(ledPin, HIGH);
}
else
//{
// digitalWrite(ledPin, LOW);
// }
if (digitalRead(buttonBpin) == LOW);
{
digitalWrite(ledPin, LOW);
}
}
Hello,
From the looks of your code, you are having two buttons, and what it's doing is that if one button is down the light is on and if the other is down the light is off. However, this doesn't do what you want it to do because when you hold the button down the light goes on, and nothing else.
From my understanding, if you want to have one button that turns it on and one button that turns it off you will need to have a variable that can be toggled, and base the light being on or off on that variable. For example you could at the beginning of the code (before the setup and the loop) declare variable "int toggle = 0;" and where you put the "digitalWrite(ledPin, LOW);" you could put "int toggle = 0;" and where you put digitalWrite(ledPin, HIGH) you could put "int toggle = 1;" At the end of your code, put an if else statement with the toggle variable and there put the digtalWrite statement.
Hope this helps!
Thanks,
That makes some sense logically speaking and there is a line there about byte leds =0 that perhaps was supposed to be used as the variable but it's not used anywhere in the code so I am not sure how that works without it unless the elegoo board is somehow a little different or the person in the video is not actually using the code presented
Still a mystery but I will look into adding a variable and see if I can make it work that way
As it turns out there is nothing wrong with the logic and it works as it supposed to without adding any variables
I got a complete code from Adafruit and than I ripped out my setup and rebuilt it with just one switch first just to see if it works and to keep it simple and it worked so than I added the second switch and that worked too
Not sure where exactly was the problem but if it's connected correctly and with the correct code it will work
So the main takeaway from this is that when the switch is pressed it sends a signal to the processor that will activate the line where the led is located and it will stay that way until the other switch is pressed
I am glad I worked it out because that would have affected the logic of any other projects that would use the digital logic pins
Not sure how to post new questions I don't see any way to do it so I am just changing the subject of my original question I don't know why this has to be made so complicated
So my new problem I am having is with getting the 7 segment display Originally I had it set up for lesson 27 to use the 74hc595 with the 7 segment but it did not work which I later figure it was different because I had common anode instead of common cathode so I tried to simplify it by just using the 7 segment only
I got it working with the DigitalWrite version but I wanted to make it work with the port manipulation but it's not working None of the led's light up
I am trying to use this code but it's not working
int digit[10] = {B10000000, B11110010, B01001000, B01100000, B00110010, B00100100, B00000100, B11110000, B00000000, B00100000}; // common anode
// int Digit[10]={B01111111,B00001101,B10110111,B10011111,B11001101,B11011011,B11111011,B00001111,B11111111,B11011111}; // commmon chathode
void setup() {
DDRD = B11111110;
}
void loop() {
for (int i = 0; i < 10; i++)
{
PORTD = digit[i];
delay(1000);
}
}
Are all PORTD configuration and pinouts the same on all versions of chips ?
I am using it with ATMEGA2560 while on the video it is an older through hole version
What is confusing me a little is how is this supposed to work with common anode
Since it has to go to gnd to light up the diode does the bit has to be 1 or 0 ?
On the other hand it should not make any difference as far as lighting up Maybe it's not lighting up the correct one but at least some led should light up but I am not getting anything to light up so I am not sure if it's the code or maybe it's not compatible with this version of the chip
After some more searching it appears that the 2560 pinout is different so I can't use the PORTD for the 7 segment display as it was shown in the video so I moved the wires to the side connector and changed the code to
PORTA and DDRA and it's working now