So I’ve been working on a new project and have run into a bit of an issue. I plan to send a serial message over to Processing, however it very important that I don’t send multiple values stating the same button press. Since Arduino is in a constant loop state, when i press a button using something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | // BUTTONS const int buttonGreen = 8; //LEDS const int ledGreen = 2; //STORE BUTTON STATES int greenState = 0; void setup() { Serial.begin(9600); pinMode(ledGreen, OUTPUT); pinMode(buttonGreen, INPUT); } void loop(){ // CHECK BTN STATES greenState = digitalRead(buttonGreen); //GREEN if (greenState == HIGH) { Serial.print(ledGreen, DEC); digitalWrite(ledGreen, HIGH); } else { digitalWrite(ledGreen, LOW); } |
When i check my serial monitor I receive something like:
22222222222222222222222222…..
That won’t work, since I plan for each of the Serial events to make an API call to Twitter. This activity would wipe out my allotted request in a matter of a couple of clicks, and probably would result in getting my account suspended due to unusual activity. So bellow is a quick fix which store individual button clicks, and prints out the Serial output once. I also decided against using any delays, since that may have potentially ended up in ignoring a button press.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | // BUTTONS const int buttonGreen = 8; const int buttonRed = 9; const int buttonYellow = 10; //LEDS const int ledGreen = 2; const int ledRed = 3; const int ledYellow = 4; //STORE BUTTON STATES int greenState = 0; int redState = 0; int yellowState = 0; int lastActiveLED = 0; // Keep track of the activeLED void setup() { Serial.begin(9600); pinMode(ledGreen, OUTPUT); pinMode(ledRed, OUTPUT); pinMode(ledYellow, OUTPUT); pinMode(buttonGreen, INPUT); pinMode(buttonRed, INPUT); pinMode(buttonYellow, INPUT); } void loop(){ // CHECK BTN STATES greenState = digitalRead(buttonGreen); redState = digitalRead(buttonRed); yellowState = digitalRead(buttonYellow); /********** * Each of the following conditionals only sends one Serial message * per button press **********/ //GREEN if (greenState == HIGH) { //check if last active LED was not Green if(lastActiveLED != ledGreen){ //print Green since it is a new press Serial.print(ledGreen, DEC); } //store green as last active press lastActiveLED = ledGreen; digitalWrite(ledGreen, HIGH); } else { //clear out last active so you could reclick it if(lastActiveLED == ledGreen){ lastActiveLED = 0; } digitalWrite(ledGreen, LOW); } //RED if (redState == HIGH) { if(lastActiveLED != ledRed){ Serial.print(ledRed, DEC); } lastActiveLED = ledRed; digitalWrite(ledRed, HIGH); delay(100); } else { if(lastActiveLED == ledRed){ lastActiveLED = 0; } digitalWrite(ledRed, LOW); } //YELLOW if (yellowState == HIGH) { if(lastActiveLED != ledYellow){ Serial.print(ledYellow, DEC); } lastActiveLED = ledYellow; digitalWrite(ledYellow, HIGH); delay(100); } else { if(lastActiveLED == ledYellow){ lastActiveLED = 0; } digitalWrite(ledYellow, LOW); } } |
If anyone knows of any other methods of doing this, let me know!
Browse Timeline
- « Will Wright presents “Why Games are Good for Learning” at NYU
- » Update Twitter Status w/ Arduino & Processing
