Pages: [1] 2 3 4   Go Down
Author Topic: Struggling with switching Arduino and Random numbers  (Read 2308 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.



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

« Last Edit: December 02, 2012, 06:24:07 am by Mazza » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34152
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Faraday Member
**
Karma: 23
Posts: 3482
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Check out my experiments http://blog.blinkenlight.net

Offline Offline
Edison Member
*
Karma: 29
Posts: 2448
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

and use for loops instead of repeating code.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley-red smiley-red
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34152
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: December 02, 2012, 06:25:01 am by Mazza » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34152
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34152
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Hope that makes more sense mate.
Sorry no.

The code:-
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

hence the new code should look like this?

Code:
   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
« Last Edit: December 02, 2012, 07:22:04 am by Mazza » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34152
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void loop(){
       
        ranNum=random(3,10);
 digitalWrite(ranNum,HIGH);
delay(2000);
digitalWrite(ranNum,LOW);
        }
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1] 2 3 4   Go Up
Jump to: