Hey guys, OK im loosing my mind here.
Im using a virtual applet that shows the application is working.
//Save some memory, use bytes and booleans rather than ints for pin numbers and pin states. (pin numbers are <255, so an int isnt needed)
const byte buttonPin = 9; // the number of the pushbutton pin
// variables will change:
boolean buttonState = 0; // Variable for reading the pushbutton status
byte ledPin = 0;
boolean loopwait = 0;
#define maxLEDs 9 //This replaces anything which say 'maxLEDs' with the number 9
byte ledPins[maxLEDs] = {0,1,2,3,4,5,6,7,8}; //set your LED pins here
void setup() {
//pinMode(ledPin, OUTPUT); // Initialize the LED pin as an output:
pinMode(buttonPin, INPUT); // Initialize the pushbutton pin as an input:
for (byte i = 0; i < maxLEDs; i++){
pinMode(ledPins[i], OUTPUT); //Set all of the different LEDs to outputs.
}
}
void loop(){
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
if (buttonState == HIGH) { // check if the pushbutton is pressed.
if (loopwait == 0) {
// turn LED on:
byte randomNumber = random(100); //You want all statements to check against the same number, not generate a new one for each elseif() statement
if (randomNumber < 20){
ledPin = 0;
}
else if (randomNumber < 40){
ledPin = 1;
}
else if (randomNumber < 60){
ledPin = 2;
}
else if (randomNumber < 70){
ledPin = 3;
}
else if (randomNumber < 80){
ledPin = 4;
}
else if (randomNumber < 90){
ledPin = 5;
}
else if (randomNumber < 94){
ledPin = 6;
}
else if (randomNumber < 98){
ledPin = 7;
}
else if (randomNumber < 101){
ledPin = 8;
}
digitalWrite(ledPins[ledPin], HIGH);
loopwait = 1;
}
} else {
loopwait = 0;
digitalWrite(ledPins[ledPin], LOW); // turn LED off:
}
}
but I cant get the wiring right at all on this. And I cant find anything at the moment. Can anyone help, I keep getting it running random at massive speed when switch is open and when closed it stops.
Can anyone assist either my code is wonky or the wiring is wonky.