Offline
Newbie
Karma: 0
Posts: 14
|
 |
« on: February 20, 2012, 11:40:59 pm » |
I have been experimenting with pieces of code, but haven't been able to figure out - how can I use the button state to change direction (so that I do not have to hold the button to change direction, as I wrote it - but instead press it once to change back and forth (forward sequence and backward sequence). Tried using if statements to essentially reverse the equation on a button state change - but have yet to get that working. Second question can I get the code working the way it is, so that you hold the button, but instead of waiting until the end of the sequence (forward:1234, backward:4321) to change directions, it changes directions as soon as I hold down the button (i.e. 123(holdbutton)2143214321(releasebutton)2341234) Below is the instructions for this simple program, and the code thus far, which technically works for what I need, just curious about the above questions. Thanks in advance for at least taking a look! Instructions: 1. Only one LED should light up at one time. At least one LED should be lighted up. 2. Each LED should light up for half a second. 3. The sequence should be L1, L2, L3, L4, L1 and so on. 4. Put in a button for direction control. If the button is pressed, the LED's should light up in reverse sequency, i.e. L4, L3, L2, L1, L4 and so on. Code:
const int buttonPin = 10;
int buttonState = 0;
int lastButtonState = 0;
int timer = 500;
void setup() { pinMode(buttonPin, INPUT); for (int thisPin = 2; thisPin < 8; thisPin++) { pinMode(thisPin, OUTPUT); } }
void loop() { if (buttonState == HIGH) { for (int thisPin = 2; thisPin < 6; thisPin++) { digitalWrite(thisPin, HIGH); delay(timer); digitalWrite(thisPin, LOW); buttonState = digitalRead(buttonPin); } } else { for (int thisPin = 5; thisPin >= 2; thisPin--) { digitalWrite(thisPin, HIGH); delay(timer); digitalWrite(thisPin, LOW); buttonState = digitalRead(buttonPin); } }}
(code tags added by moderator)
|
|
|
|
« Last Edit: February 21, 2012, 12:44:54 am by CrossRoads »
|
Logged
|
|
|
|
|
Central MN, USA
Offline
Faraday Member
Karma: 35
Posts: 5920
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #1 on: February 21, 2012, 01:23:10 am » |
Couldn't choose a better file name?  Let's be clear. You have forward sequence. Then press down the button and hold it down, the sequence goes reverse. Release the button, the sequence runs forward. If you really want to see reverse sequence, your finger will get tired. Are you sure you don't want one click then reverse, next click then forward...?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 312
Posts: 35483
Seattle, WA USA
|
 |
« Reply #2 on: February 21, 2012, 06:45:56 am » |
Put each { on a new line. Each } goes on it's own line, too. Use Tools + Auto Format to make that code readable.
That, along with a clarification of your requirements, will get you some help.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 14
|
 |
« Reply #3 on: February 21, 2012, 01:38:40 pm » |
My code thus far: Basically, I am having trouble understanding how to make button presses work. The code I have currently I have to hold the button to change LED sequence. How would I set that to simple reverse every time I press the button? (aharasewych, adds these tags next time you post: [ code ] & [/ code ], without the spaces - Moderator) const int buttonPin = 10;
int buttonState = 0; int timer = 500;
void setup() { pinMode(buttonPin, INPUT); for (int thisPin = 2; thisPin < 8; thisPin++) { pinMode(thisPin, OUTPUT); } }
void loop() { if (buttonState == HIGH) { for (int thisPin = 2; thisPin < 6; thisPin++) { digitalWrite(thisPin, HIGH); delay(timer); digitalWrite(thisPin, LOW); buttonState = digitalRead(buttonPin); } } else { for (int thisPin = 5; thisPin >= 2; thisPin--) { digitalWrite(thisPin, HIGH); delay(timer); digitalWrite(thisPin, LOW); buttonState = digitalRead(buttonPin); } } }
|
|
|
|
« Last Edit: February 21, 2012, 02:29:14 pm by CrossRoads »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 312
Posts: 35483
Seattle, WA USA
|
 |
« Reply #4 on: February 21, 2012, 02:50:56 pm » |
Do you have a reading comprehension issue?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 243
Posts: 16515
Available for Design & Build services
|
 |
« Reply #5 on: February 21, 2012, 03:27:46 pm » |
Change this part: buttonState = digitalRead(buttonPin);
to if (digitalRead (buttonPin) == 0){ // assumes buttonPin declared as INPUTwith internal pullup enabled buttonState = 1-buttonState; // result is 1,0,1,0,1,0 delay(50); // crude debounce }
then you change direction & keep that change after a button press @PaulS, that was uncalled for.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 14
|
 |
« Reply #6 on: February 21, 2012, 04:12:10 pm » |
Thanks crossroads, and no thank you Paul.
Figured the question was simple enough. I listed the requirements, the specific code didnt matter, just wanted a prod in the right direction. Sorry i didn't simplify it further for you Paul. Between all the sarcasm and the condescension, im sure it is hard for you to find time to read and be helpful.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 312
Posts: 35483
Seattle, WA USA
|
 |
« Reply #7 on: February 22, 2012, 04:09:34 am » |
@PaulS, that was uncalled for. Really? I asked that the poster make the code readable, and OP posted the same unreadable mess again. What's the problem with making code readable before posting it? Often times, when the { and } are lined up, and the code properly indented, it is then obvious that else blocks are associated to the wrong if statement, because the poorly positioned { and } did not make it clear what ended where. Since OP's issue seemed to be related to program structure, I thought that reformatting the code would make it more apparent what the problem was. I do not apologize for, or retract, my comment in any way.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 14
|
 |
« Reply #8 on: February 22, 2012, 10:06:51 am » |
Paul, I do autoformat and I get this. Compare the two, it was damn close if not right. If you feel I post unreadable messes - then you can feel free not to respond. Thank you. Sorry I didn't have [ code ] in there - didn't know. - Thanks for editing that into the last posts CrossRoads. People like you are why I'll be back. const int buttonPin = 10;
int buttonState = 0; int timer = 500;
void setup() {
pinMode(buttonPin, INPUT); for (int thisPin = 2; thisPin < 8; thisPin++) { pinMode(thisPin, OUTPUT); } }
void loop() { if (buttonState == HIGH) { for (int thisPin = 2; thisPin < 6; thisPin++) { digitalWrite(thisPin, HIGH); delay(timer); digitalWrite(thisPin, LOW); buttonState = digitalRead(buttonPin);
} } else { for (int thisPin = 5; thisPin >= 2; thisPin--) { digitalWrite(thisPin, HIGH); delay(timer); digitalWrite(thisPin, LOW); buttonState = digitalRead(buttonPin); } } }
|
|
|
|
« Last Edit: February 22, 2012, 10:11:01 am by aharasewych »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 14
|
 |
« Reply #9 on: February 22, 2012, 10:22:09 am » |
CrossRoads - this button press works great - however the direction change can only occur at the beginning of the loop. How could I adjust the code to alter direction from the point it the button is pressed during the loop? i.e. instead of 1234123412(press)3443214321, it would go 1234123412(press)143214321 (does that make sense - not sure if I am asking the question correctly) const int buttonPin = 13;
int buttonState = 0; int timer = 500;
void setup() {
pinMode(buttonPin, INPUT); for (int thisPin = 3; thisPin < 13; thisPin++) { pinMode(thisPin, OUTPUT); } }
void loop() { if (buttonState == HIGH) { for (int thisPin = 3; thisPin < 13; thisPin++) { digitalWrite(thisPin, HIGH); delay(timer); digitalWrite(thisPin, LOW); if (digitalRead (buttonPin) == 0){ // assumes buttonPin declared as INPUTwith internal pullup enabled buttonState = 1-buttonState; // result is 1,0,1,0,1,0 delay(50); // crude debounce } } } else { for (int thisPin = 12; thisPin >= 3; thisPin--) { digitalWrite(thisPin, HIGH); delay(timer); digitalWrite(thisPin, LOW); if (digitalRead (buttonPin) == 0){ // assumes buttonPin declared as INPUTwith internal pullup enabled buttonState = 1-buttonState; // result is 1,0,1,0,1,0 delay(50); // crude debounce } } } }
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
God Member
Karma: 6
Posts: 918
Arduino rocks
|
 |
« Reply #10 on: February 22, 2012, 01:41:54 pm » |
@PaulS, that was uncalled for.
typical 'mo/style'.. (sigh) not to mention the auto format doesnt even 'format' the code in a way Paul 'requires' to be helpful. (ie: does NOT put { or } on new lines) nobody 'requires' anybody to surf threads here, help or do anything.. just skip it, if its not 'for you'...
|
|
|
|
|
Logged
|
|
|
|
|
Central MN, USA
Offline
Faraday Member
Karma: 35
Posts: 5920
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #11 on: February 22, 2012, 02:03:53 pm » |
You can't break out of a complete cycle since you have no statement to do it. Your current program only uses the button status at the start of a complete cycle, so what do you expect? There's a reason Paul lashed out on your formatting. Paul probably puts the left brace on a separate line and lines it up with the corresponding right brace, which is also what I do by the way. It won't take you much effort to do it and will save time for everyone reading your code. Your current code is already alright but we can be picky. So here's what I would do, to make it work for you. Replace your loop with this: int lit_LED=3; int LED_direction=1; while(1) { //do the button sensing stuff here LED_direction=buttonState==HIGH?1:-1; digitalWrite(lit_LED,HIGH); delay(timer); digitalWrite(lit_LED,LOW); lit_LED+=LED_direction; if (lit_LED==1) lit_LED=5; if (lit_LED==6) lit_LED=2; }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 243
Posts: 16515
Available for Design & Build services
|
 |
« Reply #12 on: February 22, 2012, 03:48:47 pm » |
To get out of the loop, set your test condition to the end state when the button is pressed: if (digitalRead (buttonPin) == 0){ // assumes buttonPin declared as INPUTwith internal pullup enabled buttonState = 1-buttonState; // result is 1,0,1,0,1,0 thisPin = 13; delay(50); // crude debounce
and if (digitalRead (buttonPin) == 0){ // assumes buttonPin declared as INPUTwith internal pullup enabled buttonState = 1-buttonState; // result is 1,0,1,0,1,0 thispin = 2; delay(50); // crude debounce
The code as posted looked autoformatted to me already. I had no trouble following it.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 312
Posts: 35483
Seattle, WA USA
|
 |
« Reply #13 on: February 23, 2012, 07:38:56 am » |
Paul, I do autoformat and I get this. Compare the two, it was damn close if not right. If you feel I post unreadable messes - then you can feel free not to respond. Thank you. What I asked you to do was: Put each { on a new line. Each } goes on it's own line, too. Use Tools + Auto Format to make that code readable. You managed one out of three. Properly formatted code is much easier to read AT A GLANCE. If you prefer to study code to line up { and }, to find where operators are (this==that is a lot harder to process than this == that), etc. then fine. If you want other people to help you, it isn't asking that much to make their job easier.
|
|
|
|
|
Logged
|
|
|
|
|
|