Pages: [1]   Go Down
Author Topic: Resetting a input signal  (Read 584 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey all,

So i'm very new to learning the Arduino coding but i have managed to build a simple code from some tutorials that after a button press input 5 lights come on in sequence, each delayed 1 second to the previous.

Now that is done what i want the code to do is at a random time interval of between 4 to 7 seconds turn all the lights out at once. Is this possible within the code?

I've been using the Simulator for Arduino software which shows the light on sequence is working as i want. And if i turn the switch input off again the leds go out as expected, i just want it to do this automatically.

the code to date is as follows but this has no random time off functions within it

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin1 =  9;      // the number of the LED pin
const int ledPin2 =  10;      // the number of the LED pin
const int ledPin3 =  11;      // the number of the LED pin
const int ledPin4 =  12;      // the number of the LED pin
const int ledPin5 =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);  
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);  
  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);    
}
void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
    // turn LED on:    
    digitalWrite(ledPin1, HIGH);
    delay (1000);
    digitalWrite(ledPin2, HIGH);
    delay (1000);
    digitalWrite(ledPin3, HIGH);
    delay (1000);
    digitalWrite(ledPin4, HIGH);
    delay (1000);
    digitalWrite(ledPin5, HIGH);
    
    
  }
  else {
    // turn LED off:
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin5, LOW);  
  }
}
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you could save yourself a lot of typing with a for loop (and remember you don't NEED to give your pins names- in this case it makes your life easier).

Code:

for (int i = 1; i <=5; i++){
  pinMode(i, OUTPUT);
}

for (int i = 1; 1 <=5; i++){
digitalWrite(i, HIGH);
delay(1000);
}
Logged

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

Ok i'll try and shorten it down a bit, as i say new to this so just making it work is a bonus right now.....

Can a rand off function be used to turn the switch input off?
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok i'll try and shorten it down a bit, as i say new to this so just making it work is a bonus right now.....

Can a rand off function be used to turn the switch input off?

yes you can. Could you describe in detain on how you want the led to react toward your input?
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to check if the lights are already on before you check if the button is pressed.

Code:
// top of your program
int ledsOn = 0;

Then check it in loop()

Code:
if  (ledsOn == 0 && buttonState == HIGH) {
// turn the LEDs on, one by one
// then set ledsOn = 1
}
else {
// wait a random time between 4 and 7 seconds
// turn the LEDs off
// set ledsOn = 0
}

I'm new to Arduino too, so this may be crap smiley-wink
Logged

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

So currently as it is set up i press the button (my input obviously goes to HIGH) which then allows the 5 leds to come on at 1 second intervals until all 5 are lit together.

What i would then like to happen is the input to return to LOW which then in the code makes the 5 lights turn off all at once.

I want the input to return to LOW randomly between 3 to 7 seconds after the 5 leds are all lit. I am assuming that would mean the code would read something like

randOff = random (8000, 12000);

This would allow 5 seconds for the leds to light up then 3 to 7 seconds late turn the button input back to LOW
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So currently as it is set up i press the button (my input obviously goes to HIGH) which then allows the 5 leds to come on at 1 second intervals until all 5 are lit together.

What i would then like to happen is the input to return to LOW which then in the code makes the 5 lights turn off all at once.

I want the input to return to LOW randomly between 3 to 7 seconds after the 5 leds are all lit. I am assuming that would mean the code would read something like

randOff = random (8000, 12000);

This would allow 5 seconds for the leds to light up then 3 to 7 seconds late turn the button input back to LOW

when you say input return to LOW, isnt it mean that its from a user input?
Logged

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

I've cracked it smiley

Yes it is a momentary push button which i forgot will go high then low so it sets the lights off high then all i had to do was add in a random delay after all the lights where off before it turned the lights back off smiley

Thanks for the help guys
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

good to know that you made it work....
Logged

Pages: [1]   Go Up
Jump to: