Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different explanation/annotation for sketch #5

Open
mwweinberg opened this issue Jun 13, 2014 · 0 comments
Open

Different explanation/annotation for sketch #5

mwweinberg opened this issue Jun 13, 2014 · 0 comments

Comments

@mwweinberg
Copy link
Collaborator

OK, I'm pretty damn sure this is the wrong place for something like this, but I don't know what I'm doing so I put it here. In an attempt to actually understand the debouncing thing, I re-annotated the sketch. Also, since I'm not used to reading in-line code comments, I semi-turned them into a narrative. I'm mostly putting this here as a note to myself tomorrow, but also in case it is helpful to anyone else (especially the non-coders in the group).

Narrative (note: bracketed terms are variables):

  1. set the variable named [reading] to equal the value of pin 4, which is the pin connected to the button
  2. If that [reading] value - the value of the button right now - does not equal [lastButtonState] - which is the baseline value that gets created at the end of this process (in other words, if the button state is different than last time), then redefine [lastDebounceTime] as the number of milliseconds since this program started
  3. take the amount of milliseconds that this program has been running and subtract the milliseconds recorded above as the definition of [lastDebounceTime]. If you have a more than 50 millisecond difference, move on to the next step. If not, this is just noise so ignore the change until you have a more than 50 millisecond difference.
  4. if the variable [reading] - which was set as the value of pin 4 and is what the button is doing right now - is not the same as the variable [buttonState], redefine [buttonState] as the current value of [reading]
  5. if the newly defined [buttonState](which is the value of pin 4 and is what the button is doing right now) is LOW (which means the button is being pushed) then redefine [currentPinMode] as one number higher than before
  6. but if the value of [currentPinMode] gets to 3, change it to 0 because we only have LED modes that correspond to 0, 1, and 2 so 3 and anything higher would be meaningless
  7. take the value of [currentPinMode] and find the correct case code block. Execute that block to set the LED to the correct color
  8. reset [lastButtonState] to create a new baseline for the button state (pin 4)

Reannotated sketch (those annotations put just above the corresponding part of the code):

int buttonPin = 4;

int ledPin_R = 13;
int ledPin_G = 12;
int ledPin_B = 11;

int currentPinMode = 0;

/* debounce variables */
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers


void setup() {
  // set pin 4 for input, pin 11-13 for output
  pinMode(buttonPin, INPUT);
  pinMode(ledPin_R, OUTPUT);
  pinMode(ledPin_G, OUTPUT);
  pinMode(ledPin_B, OUTPUT);

  // turn off all those LEDs by setting them HIGH
  digitalWrite(ledPin_R, HIGH);
  digitalWrite(ledPin_G, HIGH);
  digitalWrite(ledPin_B, HIGH);
}

void loop() {
  // set the variable named [reading] to equal the value of pin 4, which is the pin connected to the button
  int reading = digitalRead(buttonPin);

// If that [reading] value - the value of the button right now - does not equal [lastButtonState] - which is the baseline value that gets created at the end of this process (in other words, if the button state is different than last time):
  if (reading != lastButtonState) {
    // then redefine [lastDebounceTime] as the number of milliseconds since this program started
    lastDebounceTime = millis();
  }

  // take the amount of milliseconds that this program has been running and subtract the milliseconds recorded above as the definition of [lastDebounceTime]. If you have more than a 50 millisecond difference, move on to the next step.  If not, this is just noise so ignore the change until you have a more than 50 millisecond difference.
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // if the variable [reading] - which was set as the value of pin 4 and is what the button is doing right now - is not the same as the variable [buttonState], redefine [buttonState] as the current value of [reading]:
    if (reading != buttonState) {
      buttonState = reading;

      // if the newly defined [buttonState] (which is the value of pin 4) is LOW (which means the buton is being pushed)
      if (buttonState == LOW) {
      // then redefine [currentPinMode] as one number higher than before
        currentPinMode = currentPinMode + 1;
        // but if the value of [currentPinMode] gets to 3, change it to 0 because we only have LED modes that correspond to 0, 1, and 2 so 3 and anything higher would be meaningless
        if (currentPinMode >= 3) {
          currentPinMode = 0;
        }
      }
    }
  }
  // take the value of [currentPinMode] and find the correct case code block. Execute that block to set the LED to the correct color
  switch (currentPinMode) {
    case 0: // red
      digitalWrite(ledPin_R, LOW);
      digitalWrite(ledPin_G, HIGH);
      digitalWrite(ledPin_B, HIGH);
      break;

    case 1: // green
      digitalWrite(ledPin_R, HIGH);
      digitalWrite(ledPin_G, LOW);
      digitalWrite(ledPin_B, HIGH);
      break;

    case 2: // blue
      digitalWrite(ledPin_R, HIGH);
      digitalWrite(ledPin_G, HIGH);
      digitalWrite(ledPin_B, LOW);
      break;
  }

  // reset [lastButtonState] to create a new baseline for the button state (pin 4):
  lastButtonState = reading;
}
@konklone konklone changed the title Different explination/annotation for sketch Different explanation/annotation for sketch Jun 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant