When I click a button on my breadboard, it turns off the Elegoo for some reason. I have a video I will attach in a comment. IT also doesnt do its proper function
const char bluePin = 9; // The pin of the blue LED
const char greenPin = 2; // The pin of the green LED
const char blueButtonPin = 11; // The pin of the button for team blue
const char greenButtonPin = 12; // The pin of the button for team green
const char redPins[] = {3,4,5,6,7,8}; // Each red pin, going from green to blue
void setup()
{
Serial.begin(9600);
pinMode(bluePin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(blueButtonPin, INPUT_PULLUP);
pinMode(greenButtonPin, INPUT_PULLUP);
for(int redPin = 0; redPin < sizeof(redPins)/sizeof(*redPins); redPin++) // For each red pin
{
pinMode(redPin, OUTPUT);
}
}
void loop()
{
int blueButtonValue = digitalRead(blueButtonPin);
Serial.println(blueButtonValue);
if(blueButtonValue == HIGH)
{
Serial.println("Blue Button Pressed");
digitalWrite(bluePin, HIGH);
}
}
Just connect it to the ground, not the 5v too. You are probably grounding out all the power you are supplying to the Arduino if you don't have a resistor on it. Pin -> button -> ground
I'm new to this too, so forgive me if I'm wrong, but I believe your button will read high when not pressed, and low when pressed, so to read when it is pressed you want this:
On a four pin button the terminals diagonal to each other will never be connected without pressing the button, whereas the neighboring ones sometimes are. If that makes sense.
I trimmed down the code and hardware to just what we are dealing with now and this works. It starts with the LED off, and it turns on once pressed.
const byte bluePin = 9; // The pin of the blue LED
const byte blueButtonPin = 11; // The pin of the button for team blue
void setup() {
Serial.begin(9600);
pinMode(bluePin, OUTPUT);
pinMode(blueButtonPin, INPUT_PULLUP);
}
void loop() {
int blueButtonValue = digitalRead(blueButtonPin);
Serial.println(blueButtonValue);
if (blueButtonValue == LOW)
{
Serial.println("Blue Button Pressed");
digitalWrite(bluePin, HIGH);
}
}