When tasked with setting up one LED to fade on and then off after reading an input voltage, and to also have that same LED digitalWrite on/off due to a button press, we were fine. However, when expanding our code to include 5 LED's (each which should be able to fade due to input voltage, and turn on/off due to a button), and expanding our circuit to a breadboard, we can only get the fade function to work. 3 of the 5 LEDs immediately write HIGH, the other 2 remain LOW. The read pins do not trigger as programmed, and the LEDs change state erratically when the arduino board is tapped and 2 LEDs get triggered by one read pin irregularly. I don't know what to make of this. Do you?
Here's our set-up
In the instance that you would like to see our code:
int voltage0, voltage1, voltage2, voltage3, voltage4 = 0;
int fade0 = 11;
int fade1 = 10;
int fade2 = 9;
int fade3 = 6;
int fade4 = 5;
int buttonPin0 = 4;
int buttonPin1 = 3;
int buttonPin2 = 2;
int buttonPin3 = 1;
int buttonPin4 = 0;
int analog0 = 0;
int analog1 = 1;
int analog2 = 2;
int analog3 = 3;
int analog4 = 4;
int buttonPushCounter0, buttonPushCounter1, buttonPushCounter2, buttonPushCounter3, buttonPushCounter4 = 0;
int buttonState0, buttonState1, buttonState2, buttonState3, buttonState4 = 0;
int lastButtonState0, lastButtonState1, lastButtonState2, lastButtonState3, lastButtonState4 = 0;
void setup()
{
pinMode(fade0, OUTPUT);
pinMode(fade1, OUTPUT);
pinMode(fade2, OUTPUT);
pinMode(fade3, OUTPUT);
pinMode(fade4, OUTPUT);
pinMode(analog0, INPUT);
pinMode(analog1, INPUT);
pinMode(analog2, INPUT);
pinMode(analog3, INPUT);
pinMode(analog4, INPUT);
pinMode(buttonState0, INPUT);
pinMode(buttonState1, INPUT);
pinMode(buttonState2, INPUT);
pinMode(buttonState3, INPUT);
pinMode(buttonState4, INPUT);
}
void loop()
{
whoopie(fade0, voltage0, analog0);
whoopie(fade1, voltage1, analog1);
whoopie(fade2, voltage2, analog2);
whoopie(fade3, voltage3, analog3);
whoopie(fade4, voltage4, analog4);
button(buttonState0, buttonPin0, lastButtonState0, buttonPushCounter0, fade0);
button(buttonState1, buttonPin1, lastButtonState1, buttonPushCounter1, fade1);
button(buttonState2, buttonPin2, lastButtonState2, buttonPushCounter2, fade2);
button(buttonState3, buttonPin3, lastButtonState3, buttonPushCounter3, fade3);
button(buttonState4, buttonPin4, lastButtonState4, buttonPushCounter4, fade4);
}
void whoopie(int led, int voltage, int analog)
{
voltage = analogRead(analog);
if(voltage > 950 && voltage <= 1029)
{
for(int brightness = 0; brightness <255; brightness +=20)
{
analogWrite(led, brightness);
delay(5);
}
for(int brightness = 255; brightness > 0; brightness -=4)
{
analogWrite(led, brightness);
delay(50);
}
}
}
void button(int buttonState, int buttonPin, int lastButtonState, int counter, int led)
{
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == HIGH)
{
counter++;
}
delay(50);
}
lastButtonState = buttonState;
if (counter % 2 == 0)
{
digitalWrite(led, HIGH);
}
else
{
digitalWrite(led, LOW);
}
}
