hi, I'm working on a project where the arduino generates a random number and will light up a light depending on the light. Here is the code i have so far:
long randNumber;
const int buttonPin = 2; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
void setup(){
Serial.begin(9600);
pinMode(buttonPin, INPUT);
// if analog input pin 0 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
randomSeed(analogRead(0));
}
I'm still a bit between programming languages, there may be a better method, but maybe... this would work ?
long randNumber;
const int buttonPin = 2; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
void setup(){
Serial.begin(9600);
pinMode(buttonPin, INPUT);
// if analog input pin 0 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
randomSeed(analogRead(0));
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
while (buttonState == HIGH){ // Keep checking buttonstate untill button is released
buttonState = digitalRead(buttonPin);
}
randNumber = random(5);
Serial.println(randNumber);
}
else {
}
//delay(50); wouldn't be needed anymore
}
In your example, you use a delay(50) call, presumably to give you time to release the button before it checks again.
This is pretty fast, and so you get multiple checks on the pin before you can release it, and so multiple numbers are sent.
Simpson has the right idea, where you wait for a button press and then for a button release before checking again for a button press. But not quite a working idea.
The contacts in the switch sort of chatter quickly when you press it, but the arduino is fast enough to pick up this chatter, and so it senses multiple presses. (called contact bounce)
The solution is debouncing, where you only accept a state change (unpressed->pressed or pressed->released) if the button stays in that state for a certain amount of time. This way you ignore the quick chatter of the contacts.
thats kind of what I'm trying to do, what i need exactly is for me to press the button once and i get one random number back, not multiple numbers, and also if its possible i would like to have it so that if i keep holding it after it returns the one number, nothing happens. if that makes sense.
#define BUTTONPIN 2 // takes less memory than a const int
long randNumber;
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT);
randomSeed(analogRead(0));
}
void loop()
{
while ( digitalRead(BUTTONPIN ) == LOW); //wait for button HIGH
randNumber = random(5);
Serial.println(randNumber));
while ( digitalRead(BUTTONPIN ) == HIGH); //wait for button LOW
}
If you want to extend this, you could create a simple state machine:
Take paper and pencil and draw three circles.
the first is state RELEASED the second is state PRESSED the third is state HANDLED
draw an arrow from RELEASED to PRESSED and write "button high" besides it
draw an arrow from PRESSED to HANDLED and write "print random" besides it
draw three arrows from every circle to RELEASED and write "button low" besides it. (yes, that includes an arrow from RELEASED to RELEASED !)
draw an arrow from nowhere to RELEASED and write "start" besides it
try to understand the above before running the code. // of course you may use other names for the states but keep them meaningfull
(not tested no Arduino nearby)
#define RELEASED 0
#define PRESSED 1
#define HANDLED 2
int state = RELEASED; // initial state
#define BUTTONPIN 2 // takes less memory than a const int
long randNumber;
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT);
randomSeed(analogRead(0));
}
void loop()
{
button = digitalRead(BUTTONPIN );
if (button == LOW) state = RELEASED;
if (state == RELEASED && button == HIGH) state = PRESSED;
if (state == PRESSED)
{
randNumber = random(5);
Serial.println(randNumber));
state = HANDLED;
}
}
State machines can be far more complex but it is great technique to handle programs (functions) at almost any level of complexity.
awesome.... i just got it to work thanks so much everyone... i just changed the refresh rate and so every time i press the button it generates a random number at the right time.