First I admit to being a complete newb to this but I am trying to learn quickly. I have now spent two days working the code in an attempt to get this working with no luck. Main reason I spent so long trying before asking was because I knew that I would be learning even if I failed. That mission was successful at least. (both the learning and the failure part...lol)
I am using an Arduino Uno.
The job is fairly simple really. To create a random LED (randomly 1 0f 9 possibles) light for an amount of time being determined by an external timer. When the external timer turns off and comes back on again the Arduino is to generate a new random LED.
The external timer has an input at digital 2.
The Problem as it stands is this..........
The random changes are being controlled by the arduino and not the switched input. ie lets assume the switched input turns off after 10 secs, in the meantime the arduino is making approx ten changes.........this is due to the delay I have incorporated. However I cant use the delay function in coding as the input timer will provide variable times.
I am aware that I can get the arduino to do everything itself however this is part of a much larger project and it requires a timer seperate to the arduino to determine the switching.
I have made numerous changes to my original program in an attempt to get this working so its quite possibly that I have a pile of stuff in this sketch that is not actually needed anymore.
I am hoping that someone can point me in the right direction and also feel free to point out any parts of the program that are not needed or not doing anything. I am keen to learn so any other comments that can help me learn will be welcomed.
This is the latest incarnation of the program with the latest addition being the coding for the switch pin and it is currently not doing anything at all sadly and has prompted to me to admit when Im beaten and seek assistance.
int ranNum;
int switchPin = 2;
void setup() {
Serial.begin(9600);
pinMode(switchPin, INPUT);
delay(10);
randomSeed(analogRead(0));
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}
void loop(){
Serial.print("Read switch input: ");
ranNum=random(3,10);
if(ranNum==3)
{
digitalWrite(3, HIGH);
}
else
if(ranNum==4)
{
digitalWrite(4, HIGH);
}
else
if(ranNum==5)
{
digitalWrite(5, HIGH);
}
else
if(ranNum==6)
{
digitalWrite(6, HIGH);
}
else
if(ranNum==7)
{
digitalWrite(7, HIGH);
}
else
if(ranNum==8)
{
digitalWrite(8, HIGH);
}
else
if(ranNum==9)
{
digitalWrite(9, HIGH);
}
delay(1000);
//Turn off the LED
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
}