isabela, philippines.
Offline
Jr. Member
Karma: 1
Posts: 62
if you dont go, you will never know.
|
 |
« on: January 13, 2013, 07:56:39 am » |
this is the situation, press the switch in more than 3 seconds before the led turn on unless it wont. my idea didnt work. if ( button, HIGH) { delay(500); x + 1; if ( button, HIGH) { delay(500); x + 1; if (button, HIGH) { delay(500); x + 1; if ( x == 3) {
digitalwrite( "leds turns on") x = 0; } } } } else {
x=0;
}
just give me the idea.
|
|
|
|
|
Logged
|
jaylisto
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35483
Seattle, WA USA
|
 |
« Reply #1 on: January 13, 2013, 08:07:48 am » |
if ( button, HIGH) { It is highly unlikely that the comma operator is doing what you think it is doing. You can't just invent a "command" and expect it to work. There is a function to read the state of a digital pin. You should figure out what that digitalRead() function is, and how to use it.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35483
Seattle, WA USA
|
 |
« Reply #2 on: January 13, 2013, 08:08:41 am » |
Oh, and adding 1 to x and discarding the result is an exercise in futility.
|
|
|
|
|
Logged
|
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #3 on: January 13, 2013, 08:33:44 am » |
it should be something along this line if (Switch==HIGH) { x++; if (x==3) { digitalWrite(LED,HIGH); x=0; } }
|
|
|
|
|
Logged
|
|
|
|
|
isabela, philippines.
Offline
Jr. Member
Karma: 1
Posts: 62
if you dont go, you will never know.
|
 |
« Reply #4 on: January 13, 2013, 09:07:04 am » |
it should be something along this line if (Switch==HIGH) { x++; if (x==3) { digitalWrite(LED,HIGH); x=0; }
thank you very much with this sir. ill try this style. but i think if you press the switch 3 times it will light up. (not holding the switch) . though ill just try it if it works.. }
|
|
|
|
|
Logged
|
jaylisto
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6367
-
|
 |
« Reply #5 on: January 13, 2013, 11:13:07 am » |
Your code: if ( button, HIGH) {
Should be : if(digitalRead(button) == HIGH) {
Your code: x + 1;
Should be: x = x + 1;
Your code layout makes it very difficult what your control structure is. Please adopt the habit of putting each { and } on separate lines, with matching pair indented by the same amount, and with the code between them indented one extra level. It makes it much, much easier for us (and you) to understand the structure of the code and also to spot where the structure is not what you intended.
|
|
|
|
|
Logged
|
|
|
|
|
isabela, philippines.
Offline
Jr. Member
Karma: 1
Posts: 62
if you dont go, you will never know.
|
 |
« Reply #6 on: January 14, 2013, 04:42:40 am » |
Your code: if ( button, HIGH) {
Should be : if(digitalRead(button) == HIGH) {
Your code: x + 1;
Should be: x = x + 1;
Your code layout makes it very difficult what your control structure is. Please adopt the habit of putting each { and } on separate lines, with matching pair indented by the same amount, and with the code between them indented one extra level. It makes it much, much easier for us (and you) to understand the structure of the code and also to spot where the structure is not what you intended. pls give me the answer coz my program didnt work. i repeat, there is no problem with the correct code. i only did that for representation (i know its wrong) this is a very simple problem, though no one can answer even me. the solution for my answer, i think... check if the switch is high delay (100) check if the switch still high delay (100) check if the switch still high delay (100) check if the switch still high turn on the led but how can i transfer this to a code or something?. if no one can help me then ill keep trying to solve this problem.
|
|
|
|
|
Logged
|
jaylisto
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35483
Seattle, WA USA
|
 |
« Reply #7 on: January 14, 2013, 05:34:10 am » |
there is no problem with the correct code. Then post that code.
|
|
|
|
|
Logged
|
|
|
|
|
isabela, philippines.
Offline
Jr. Member
Karma: 1
Posts: 62
if you dont go, you will never know.
|
 |
« Reply #8 on: January 14, 2013, 05:50:20 am » |
it should be something along this line if (Switch==HIGH) { x++; if (x==3) { digitalWrite(LED,HIGH); x=0; }
}
woohooooooo. yes! it works! for all the answer out their. this is the only one made me smile  , very good sir, thank you very much!. you only use looping but its the same with mine. what exactly is the one second as a delay? is it delay(1000)? . you really help me sir. thank you very much. Moderator edit: gibberish removed
|
|
|
|
« Last Edit: January 14, 2013, 01:36:36 pm by AWOL »
|
Logged
|
jaylisto
|
|
|
|
isabela, philippines.
Offline
Jr. Member
Karma: 1
Posts: 62
if you dont go, you will never know.
|
 |
« Reply #9 on: January 14, 2013, 06:02:40 am » |
there is no problem with the correct code. Then post that code. const int buttonPin = 2; const int ledPin = 11; int x = 0; int buttonState = 0; void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); }
void loop(){ buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) { x++; delay(1000);// turn LED on: if (x==3) { digitalWrite(ledPin, HIGH); x=0; } } else { digitalWrite(ledPin, LOW); } }
|
|
|
|
« Last Edit: January 14, 2013, 01:51:00 pm by AWOL »
|
Logged
|
jaylisto
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 48
Posts: 1406
May all of your blinks be without delay
|
 |
« Reply #10 on: January 14, 2013, 06:24:09 am » |
Does that code do what you want ?
What happens if the user presses the button for 2.5 seconds then releases it ? What will the value of x be then ?
What value will x have after the user now presses the button for 1 second ?
|
|
|
|
|
Logged
|
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #11 on: January 14, 2013, 09:39:38 am » |
Im sorry jaylisto, The program that i gave to you does not do what you need it to do. I thought you need to press the switch 3 time b4 the led will light up. so its does not do acording to what you want. btw i want to say its not good that you anger all those post a reply for your question. those people have help me learn alot.
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1563
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #12 on: January 14, 2013, 11:20:39 am » |
const int buttonPin = 2; const int ledPin = 11;
int buttonState = LOW; int lastReading = LOW; long onTime = 0;
void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); }
void loop(){ buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && lastReading == LOW) { onTime = millis(); }
//held if (buttonState == HIGH && lastReading == HIGH) { if ((millis() - onTime) > 3000 ) { //(current time - first pressed time) must be greater than 3000 (3 seconds) digitalWrite(ledPin, HIGH); lastReading = LOW; //reset state } else { digitalWrite(ledPin, LOW); } } lastReading = buttonState; //write button to lastreading for compare }
I can not try this myself right now, but it should work.
|
|
|
|
« Last Edit: January 14, 2013, 11:30:57 am by HazardsMind »
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
California
Offline
Edison Member
Karma: 39
Posts: 1852
|
 |
« Reply #13 on: January 14, 2013, 12:02:32 pm » |
Of course, there are always people who will write code for others despite any attitude problems.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19034
I don't think you connected the grounds, Dave.
|
 |
« Reply #14 on: January 14, 2013, 01:40:06 pm » |
I've removed a lot of invective and insults from this thread. OP, if this continues, this thread goes and your account risks going with it.
|
|
|
|
« Last Edit: January 14, 2013, 01:50:06 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
|