Go Down

Topic: Struggling with switching Arduino and Random numbers (Read 2 times) previous topic - next topic

Grumpy_Mike

Quote
Hope that makes more sense mate.

Sorry no.

The code:-
Code: [Select]
ranNum=random(3,10);
digitalWrite(ranNum,HIGH);

Will turn on a random numbed LED.
The variable ranNum will contain a number from 3 to 9.
That number will be used by digitalWrite st set HIGH the pin that corresponds to that number.
So those two lines, alond with a delay and a digitalWrite(ranNum,LOW)  will replace all the code in your loop.

Mazza

#11
Dec 02, 2012, 01:10 pm Last Edit: Dec 02, 2012, 01:22 pm by Mazza Reason: 1
Aha sorry, I was googling something different............

hence the new code should look like this?

Code: [Select]

   int ranNum;
   int switchPin = 2;
   
   void setup() {
     Serial.begin(9600);           // set up Serial library at 9600 bps
   pinMode(switchPin, INPUT);    // sets the digital pin as input to read switch
 
   delay(100);
   randomSeed(analogRead(0));
   // Setup 8 output ports for LED's
   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(){
     
         ranNum=random(3,10);
digitalWrite(ranNum,HIGH);
delay(1000);
digitalWrite(ranNum,LOW);
delay(1000);}


OK so this has not changed anything.

The switched Input has to control when a single LED is turned on..............at the moment the program is changing them at a period determined by the delay in the program

Grumpy_Mike

Quote
The problem when running that is that all the LEDs come on at once and stay on.


So you missed the bit in my last post that said:-
Quote
along with a delay and a digitalWrite(ranNum,LOW)  will replace all the code in your loop.


So loop should be:-
Code: [Select]
void loop(){
       
        ranNum=random(3,10);
digitalWrite(ranNum,HIGH);
delay(2000);
digitalWrite(ranNum,LOW);
        }

billroy

I bet they don't turn on all at once, but rather one at a time, very quickly, since there is no code to turn them off.

Perhaps something more like this:

Code: [Select]

int lit = 3; // save the lit one so we can turn it off

void loop() {
if (digitalRead(switchPin)) { // button pressed?
digitalWrite(lit, LOW); // turn off the old one
lit = random(3,10); // pick a new one
digitalWrite(lit, HIGH); // turn it on
}
}


-br

Mazza

Yeah sorry, mate. I probably need to go to bed as I can see I am making dum mistakes like not reading your entire post. I am exhausted.

OK so thats fine and now only one comes on one at a time BUT the determining factor in the gap between them changing is the delay in the code and thats not what I need.  I need them to change just once for every input from the external variable timer.

This is a basic flow chart of whats to happen..........

Human input via variable timer to determine length of time for an individual "LED" to turn on.

Arduina recives signal and randomly turns on "LED" until the variable timer circuit turns off.

variable timer circuit now off for variable time as set by human and the "LED" is off

variable timer circuit comes on and sends signal to arduino to select another random number in order to turn "LED" on.


and so on.


I say "LED" because for the purpose of testing thats all that is needed...........Once testing is complete and everything working the actual job will be to activate a choice of switches on a transmitter which will in turn send a signal to a receiver to do yet another job dependant on the signal sent which is determined by the random number generator.

Go Up