Struggling with switching Arduino and Random numbers

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);  
         
    }

First of all there is a sticky topic that is called read this before posting.
You have not so read it. Then modify that post to use the proper code tags.

[quoteThe external timer has an input at digital 3.][/quote]
But you have used that pin as an output. You never look at it.

Post the code you are having a problem with. Your description of what happens does not match the code because you talk about a new random sequence, there is only one here.

I would check the output of analogRead. After all this is your only source of entropy. So if analogRead does not provide enough entropy at the start, then your random start will be not so random after all.

ranNum=random(3,10);
digitalWrite(ranNum,HIGH);

and use for loops instead of repeating code.

Hi Mike, Thanks for your reply, I actually did read the sticky. However reading and actually understanding are two completely different things. And I admit to not having a clue.

Is there a complete and utter twats and newbs area in the forum somewhere because that is probably where I would be better posting if you wish. :blush: :blush:

No special area here is fine.
View the post and hit the modify button.
Select your code.
Hit the # icon, that puts code tags round it. And then save.

Cheers for that mate, fixed coding with tags and also fixed the typo of input from 3 to 2.

CJ can you explain the for loops please? I figured there had to be a better way and toyed with commas but no luck.

Thanks all and Im sorry for being a newb, I imagine its a pain for you guys but everyone has to start somewhere I guess.

OK I just found a mistake in the coding whereby at some stage I decided to try a different input for the random seed analogue input.

I have changed that back to 0 and things have started working again and I seem to have fixed the problem of the same sequence appearing.

There is now just one problem.......

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 have modified my first post to indicate this so as not to mislead anyone.

Reply #3 showed you how do do virtually the whole of your code in the loop so far in two lines.

You haven't fully understood what you want to do, if you have then you have not explainedit to us.
That code produces random flashes. Now what part does the input play? Just to say it changes the random patten dosn't make sense because it already is a random pattern therefore there will be no change.

Thanks Mike, I realised I probably could make it shorter but dont understand how. I am googling what he said, to try and understand, as yet with no luck. But I am willing to learn.

The input should be telling the random number generater to generate a number and in turn make its relative LED turn on and then stay on until the timer turns off and comes back on again at which point another random number is to be generated.

Hope that makes more sense mate.

Thanks for your time and patience all.

Hope that makes more sense mate.

Sorry no.

The code:-

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.

Aha sorry, I was googling something different............

hence the new code should look like this?

    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

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:-

along with a delay and a digitalWrite(ranNum,LOW) will replace all the code in your loop.

So loop should be:-

 void loop(){
       
        ranNum=random(3,10);
 digitalWrite(ranNum,HIGH); 
delay(2000);
digitalWrite(ranNum,LOW); 
        }

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:

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

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.

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.

lol, you win that bet bill, spot on correct and my bad for not reading Mikes post properly. Who is the idiot that invented tiredness............ :0 :wink:

I need to somehow stop the loop but have it reactivated when the variable timer completes its cycle and sends a new input.

You need to use the if statement. Read up about this.
Read your timer signal and put it in a variable called signal.

At the end of the loop function save that variable is another variable called something like oldSignal.

in the body of the loop use an if statement to compare the value of signal and oldSignal.
If they are not the same something has changed. use another if to see the state of the variable signal. then you either turn off the LED or generate a new random number and turn it on.

You are better off writing this yourself as in that way you will learn something.

Thanks heaps Mike and I totally agree with the concept of writing it myself in order to learn.

Gunna hit the sack and then when fresh, hopefully can think straight and get my head arround it.

Will return tomorrow, hopefully lavishing crap loads of praise on you for your assistance.

In the meantime, my sincere appreciation for your patience and assistance to date.

Your a legend.

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.

It is fairly simple:

  1. When the timer comes back on, generate a random number - 8-bit since you are driving 8 leds;
  2. Turn on/off the leds to reproduce the pattern. You can assign each bit to represent the 8 leds.
  ranNumber = generate_random(); //generate a random number
  digitalWrite(LED1, ranNumber & 0x01); //LED1 is bit 0  
  digitalWrite(LED2, ranNumber & 0x02); //LED1 is bit 1
  ...
  digitalWrite(LED8, ranNumber & 0x80); //LED8 is bit 7

That's it.

You can then focus on getting the random number seeded and generated.