Pages: 1 [2] 3 4   Go Down
Author Topic: Struggling with switching Arduino and Random numbers  (Read 2458 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

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............ smiley-mad smiley-wink
Logged

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

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

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

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

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

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

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Code:
  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.
Logged

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

That code dear Henry will not turn on one LED at random, but will turn on a random number of LEDs on.
Logged

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

OK so I have now spent another 12 hours on this and I am out of ideas.
This is the code and I have made notes saying what I think it is going to do.
I suspect my problem is in the way things are bracketed but despite hours or reading I cant figure it out.

This is how it currently stands..........
Code:
int ranNum;
int signal, oldsignal;
void setup()
{Serial.begin(9600);         
randomSeed(analogRead(0));
    delay(100);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(2,INPUT);
    }

void loop()
{
ranNum=random(3,10);                                      // selects random number between 3 and 9

delay(1000);
signal = digitalRead(2);                                      //signal is high if input is on or low if off

if((signal == HIGH)&&(oldsignal==HIGH))               //if signal is high and oldsignal is low then program goes to 9999 if signal is low and oldsignal is high or low then        program  goes to 3333 if signal is high and oldsignal is high then
{
digitalWrite(ranNum,HIGH);                                //turns on digital output that equals the random number
}
{
while(signal=HIGH)                                             //allows the program to stay here unless
{
if(signal=LOW)                                                   //the input turns off
break;                                                        //at which point the while loop stops
}
{
if(signal=LOW);                                               //3333

return;
}
if(signal=HIGH);                                                //9999

delay(50);
digitalWrite(ranNum,LOW);                              //causing the led to be turnmed off
delay(1000);                                                 
oldsignal=signal;
}}
Logged

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

Way way too complicated.
Never use = in a logic expression like a while always a ==

Only generate the random number when you have to not each time round the loop.
This is not code but how the program should flow:-

Read input
Compair last input to new input
     If diffrent look at signal level
          If high generate a random number and turn that LED on if low turn led off
Save input in old input variable.

That will turn on a diffrent random LED each time your input signal is at one level and turn it off when it changes to the other level. Is that what you want to do?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please put each { on a new line, and each } on a new line, and use Tools + Auto Format before you post code again. It's a wonder you can make any sense of that code as posted.

Learn to bang the space bar once in a while, too.
Logged

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

Cheers Mike, once again, I appreciate your input and its all taken on board. I have not done any programming for about 30 years or whenever the first PCs became available and i had to learn Basic. Naturally over time much of that is now fuzzy and while for this project I could have used Basic and worked with Picaxe controllers, I decided that it might be smarter to learn a new more up to date skill than rehashing an old one. Sooo here I am.

I will start reading up on your new clues and see what can be achieved. lol Once again I learned a crap load today and once again, my sincere thanks for bearing with me.


PAUL.S............Im guessing you are referring to my opening post and your thoughts are appreciated, I think Mike helped me with that part already.  However if you mean the one a couple up then Im not quite sure what you mean but happy to be enlightened.
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
while(signal=HIGH)   

You will never get out a loop like that, unless you break it in the loop.

Likely you are looking for something like this:

Code:
  while (read_mysignal() == HIGH) ...

The same with your multiple if statements.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
PAUL.S............Im guessing you are referring to my opening post and your thoughts are appreciated, I think Mike helped me with that part already.  However if you mean the one a couple up then Im not quite sure what you mean but happy to be enlightened.
No. I'm referring to the code in Reply #21.
Logged

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

Sorry Mike, I have had enough. I cant work it out and I dont understand what your clues are.

I am wasting my time and I think its better if I just use the picaxe as I am confident I can do that and wont be wasting anyone elses time or my own.

To be perfectly honest I am not impressed with the way all the program codes etc are set out. It makes no sense to me that only some of the stuff is shown under reference and other stuff is not and you have to try and hunt high and low to find it.

I spent a crap load on a book from Arduino that I was told would contain all the code necessary and when I got home I find it just has a bunch of projects and nothing else to help me.

Thanks for everything.

Logged

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

Mazza: A certain amount of discouragement and fruatration is normal.  There is a lot to learn, and as you say it is not always set out in the easiest way.  Don't give up.

I took the time to go through your code and produce something closer to what you want.  Let me offer some feedback about what I found while editing.

1. The construct 'if (x=HIGH)' should be 'if (x==HIGH)'.  Note carefully the difference.  The first one assigns HIGH to X.  The second one tests whether x is equal to HIGH.  If you use the first where you think you are using the second one you get an interesting debugging experience.

2. The construct 'if (x==HIGH');' should not have the semicolon after it.  Semicolon is a statement.  This construct says "if X is HIGH do nothing", and the following statement is executed without regard for the if.  Another interesting debugging experience.  Use if(x==HIGH) {stmt;stmt;stmt;} instead.

3. The code wasn't consistently indented to reflect the nesting structure.  This will trip up your eyes when you try to find bugs.  Adopt a consistent style, preferably by reading and imitating code from people you like.  Fortune favors those who keep their code neat as a pin.

4. Thinking carefully about the algorithm pays off.  Look how much simpler the loop() is below.

Code:
byte whichpin = 3; // set to 3 so turning it off is harmless

void setup() {
    Serial.begin(9600);         
    randomSeed(analogRead(0));
    pinMode(2,INPUT);
    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() {
if (digitalRead(2)) {
digitalWrite(whichpin, LOW);
whichpin = random(3,10);
digitalWrite(whichpin, HIGH);
}
}


There is a lot of fun to be had here when things are working correctly, but a certain amount of frustration is part of the bargain.  Stick with it, you'll figure it out.

-br

Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Mazza!
you post code in your reply#1 in which condition of IF statement like that
Code:
if(ranNum==3)
        {
        digitalWrite(3, HIGH);
        }

but in your reply#21

Code:
if(signal=LOW)                                                   //the input turns off
break;

and

Code:
if(signal=HIGH);

By looking first reply it's mean you know how to use IF statement

"="  is you used for assignment
==  is you used for comparing

Why you put terminator ";" at the end of third IF statement
if(signal=HIGH);
Logged

From Idea To Invention

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