Struggling with switching Arduino and Random numbers

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.

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

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

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

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?

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.

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.

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:

  while (read_mysignal() == HIGH) ...

The same with your multiple if statements.

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.

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.

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.

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

Hi Mazza!
you post code in your reply#1 in which condition of IF statement like that

if(ranNum==3)
        {
        digitalWrite(3, HIGH);
        }

but in your reply#21

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

and

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

Cyber, yeah cheers, stupid mistake partly because of exhaustion and partly because I am struggling with the syntax.

Billroy, cheers for that mate and also a perfect example of why I am having so many problems and frustrations...........there simply is no such thing as a whichpin in the code section.

I am used to having a book full of code, there uses, syntax and options sitting next to me so I can compare them all and then pick and choose what I want.Well thats how it was in Basic days........lol. I still have all those old books.

I am totalled for tonight so gunna hit the sack.............Im in Australia and its quite late/early morning actually. However tomorrow I will give that a run but more importantly try to understand what its doing and why.........I consider that as important as having it work.

Im a sparky by trade so the wiring and hardware is pretty much straight forward for me with just the odd calculation that I have to look up here and there that is no longer in my head. But this programming thing.............lol, well.

By any chance is there a decent book or website with the code that you would suggest?

Anyways off to bed and thanks again mate. Will post back, probably with a heap of questions tomorrow.

Best wishes.