const byte button = 15; // potentiometer wire 1 connected to pin A1 , other wires connected to ground and +5v
void setup()
{
pinMode(2, OUTPUT ); // PINS 2 - 13, 16- 19 are output leds
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(14, INPUT); // A0 , random seed pin
pinMode(15, INPUT); // A1 , my delay value pin
pinMode(16, OUTPUT);
pinMode(17, OUTPUT);
pinMode(18, OUTPUT);
pinMode(19, OUTPUT);
digitalWrite(2, LOW) ; // all LED's are off to start
digitalWrite(3, LOW) ;
digitalWrite(4, LOW) ;
digitalWrite(5, LOW) ;
digitalWrite(6, LOW) ;
digitalWrite(7, LOW) ;
digitalWrite(8, LOW) ;
digitalWrite(9, LOW) ;
digitalWrite(10, LOW) ;
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
digitalWrite(A1, LOW); // connects to button and sets my delay value
digitalWrite(16, LOW); // A2
digitalWrite(17, LOW); // A3
digitalWrite(18, LOW); // A4
digitalWrite(19, LOW); // A5
digitalWrite(2, HIGH) ; // All LEDS flash on for inital start up, used to make sure they are all functioning
digitalWrite(3, HIGH) ;
digitalWrite(4, HIGH) ;
digitalWrite(5, HIGH) ;
digitalWrite(6, HIGH) ;
digitalWrite(7, HIGH) ;
digitalWrite(8, HIGH) ;
digitalWrite(9, HIGH) ;
digitalWrite(10, HIGH) ;
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
digitalWrite(16, HIGH);
digitalWrite(17, HIGH);
digitalWrite(18, HIGH);
digitalWrite(19, HIGH);
delay (2000) ;
digitalWrite(2, LOW) ; // all LED's are off to start
digitalWrite(3, LOW) ;
digitalWrite(4, LOW) ;
digitalWrite(5, LOW) ;
digitalWrite(6, LOW) ;
digitalWrite(7, LOW) ;
digitalWrite(8, LOW) ;
digitalWrite(9, LOW) ;
digitalWrite(10, LOW) ;
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
digitalWrite(A1, LOW); // connects to button and sets my delay value
digitalWrite(16, LOW); // A2
digitalWrite(17, LOW); // A3
digitalWrite(18, LOW); // A4
digitalWrite(19, LOW); // A5
pinMode(button,INPUT); //button at pin A1
randomSeed(analogRead(A0));
}
void loop ()
{
static bool ledflag = false;
if (digitalRead(button) == HIGH) // if button is on ( button connected to pin A1 and 5v pin )
ledflag = !ledflag; // Toggle
delay(20); // Crude form of debounce
if (ledflag) // if led = 1 , button is on, if button is on run loop for random led
{
Serial.begin(9600);
int myDelay = analogRead(A1) ; // Set myDelay based off of potentiometer value connected to pin A1 and 5v, Range .2 - 300 ohms
//int myDelay =200;
int Led = random (2 , 13) ; // pick random number from 2-12, store value in variable called LED
digitalWrite(Led, HIGH) ; // turn on the random LED
delay(myDelay); // leave on for # of milliseconds set by potentiometer ( 1-10 dial )
digitalWrite(Led, LOW); // turn off that led
Serial.println(myDelay);
Serial.println(Led);
delay(myDelay) ; // delay off for amount of time it was on
}
}
I have an old machine rigged up with a 30 led's
If I turn on the power without the switch plugged into pin A1 the code acts correctly. All the leds flash then nothing happens because it is waiting for a HIGH signal from pin A1 to continue to the next part of the code where the lights will flash randomly. If my switch ( that is connected to 5v pin and A1) is off and I check the voltage between the A1 hot wire and the LED Ground, it reads a few mV, then I plug it into PIN A1 expecting nothing to happen because 1mV is not above the 3V threshold for HIGH, But the lights start blinking as if the HIGH value was met. But even stranger is when I remove the PIN from A1, the lights continue to blink. They should stop blinking because the start of the loop looks for the initial HIGH or LOW condition to start, which seemed to work when the uno was first turned on without pin A1 connected. so I defiantly have a software issue and I might have a hardware issue. Also, I keep getting a value of around 900 for my potentiometer regardless of how I adjust the knob ( the resistance changes when checked with voltmeter, may not be grounded properly) , which is hooked up to the 5v pin and to the on off switch connected to A1 .Lastly, I don't have a pull down resister hooked up yet to pin A1 but i dont think that is the problem since the voltage was so low and because im only going through one cycle to test it.
A mess of wires