06 – class notes, 14 Sep 2017 — State Machines II

06 – class notes, 14 Sep 2017 — State Machines II

Switch/Case Statements

Switch/Case statements are useful for implementing state machines. There are two good exercises on the arduino site, Tutorial1 lays out a simple example and Tutorial2 shows how to use the “default:” case as “turn everything off”.

Here’s class responses for a “make a state machine” assignment I taught in the first Making Things Interactive, there are good examples of switch/case statements in these submissions.

for() Loops

for() loops are a way to iterate through a range of values and take an action on each iteration.  In my example (below) I use for() loops to pulse a light on and off.  In the arduino tutorial they use it for a “Knight Rider” effect of cycling through LEDs.

// -*-c++-*-
/*
  The MIT License (MIT)

  Copyright (c) 2017 J. Eric Townsend

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/



const int onOffPin = 2;
const int switch1Pin = 7;
const int switch2Pin = 8;

const int yellowLEDPin = 3;
const int redLEDPin = 5;
const int blueLEDPin = 6;

const int redOn = 1;
const int redOff = 0;
int redStatus = redOff;

const int blueOn = 1;
const int blueOff = 0;
int blueStatus = blueOff;

const int yellowOn = 1;
const int yellowOff = 0;
int yellowStatus = yellowOff;

const int noLED = 0;
const int redLED = 1;
const int blueLED = 2;
const int yellowLED = 3;

int activeLED = noLED;
int activeLEDPin = 0;

const int maxPWM = 255;
const int lowPWM = 0;

int pushCount = 0;

int currentPWM = 0;

void setup() {

  Serial.begin(115200);
  pinMode (onOffPin, INPUT);
  pinMode (switch1Pin, INPUT);
  pinMode (switch2Pin, INPUT);

  pinMode(yellowLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);
}

void loop() {

  if (digitalRead(switch1Pin) == HIGH) {
    activeLED++;
    if (activeLED > yellowLED) {
      activeLED = noLED;
    }
  }
  if (digitalRead(onOffPin) == HIGH) {
    //    if (redStatus || blueStatus || yellowStatus) {
    if (activeLED != noLED)  {
      switch (activeLED) {
        case redLED:
          activeLEDPin = redLEDPin;
          break;
        case blueLED:
          activeLEDPin = blueLEDPin;
          break;
        case yellowLED:
          activeLEDPin = yellowLEDPin;
          break;
        default:
          Serial.println("WARNING");
          // if nothing else matches, do the default
          // default is optional
          break;
      }
      analogWrite(yellowLEDPin, lowPWM);
      analogWrite(blueLEDPin, lowPWM);
      analogWrite(redLEDPin, lowPWM);

      for (int v = lowPWM; v < maxPWM; v++) {
        delay(10);
        analogWrite(activeLEDPin, v);
        //          analogWrite(yellowLEDPin, v);
        //          analogWrite(blueLEDPin, v);
        //          analogWrite(redLEDPin, v);
      }

    }
  }
  else   {
    //
    //    for (int v = maxPWM; v > lowPWM; v--) {
    //      delay(10);
    //      analogWrite(yellowLEDPin, v);
    //      analogWrite(blueLEDPin, v);
    //      analogWrite(redLEDPin, v);
    //    }
    activeLED == noLED;
    analogWrite(yellowLEDPin, lowPWM);
    analogWrite(blueLEDPin, lowPWM);
    analogWrite(redLEDPin, lowPWM);
  }

}
// -*-c++-*-
/*
  The MIT License (MIT)

  Copyright (c) 2017 J. Eric Townsend

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/



const int onOffPin = 2;
const int switch1Pin = 7;
const int switch2Pin = 8;

const int yellowLEDPin = 3;
const int redLEDPin = 5;
const int blueLEDPin = 6;

const int redOn = 1;
const int redOff = 0;
int redStatus = redOff;

const int blueOn = 1;
const int blueOff = 0;
int blueStatus = blueOff;

const int yellowOn = 1;
const int yellowOff = 0;
int yellowStatus = yellowOff;

const int noLED = 0;
const int redLED = 1;
const int blueLED = 2;
const int yellowLED = 3;

int activeLED = noLED;
int activeLEDPin = 0;

const int maxPWM = 255;
const int lowPWM = 0;

int pushCount = 0;

int currentPWM = 0;

void setup() {

  Serial.begin(115200);
  pinMode (onOffPin, INPUT);
  pinMode (switch1Pin, INPUT);
  pinMode (switch2Pin, INPUT);

  pinMode(yellowLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);
}

void loop() {

  if (digitalRead(switch1Pin) == HIGH) {
    activeLED++;
    if (activeLED > yellowLED) {
      activeLED = noLED;
    }
  }
  if (digitalRead(onOffPin) == HIGH) {
    //    if (redStatus || blueStatus || yellowStatus) {
    if (activeLED != noLED)  {

      if (activeLED == redLED) {
        activeLEDPin = redLEDPin;
      }
      else if (activeLED == blueLED) {
        activeLEDPin = blueLEDPin;
      }
      else if (activeLED == yellowLED) {
        activeLEDPin = yellowLEDPin;
      }
      else {
        Serial.println("WARNING");
      }
    }
    for (int v = lowPWM; v < maxPWM; v++) {
      delay(10);
      analogWrite(activeLEDPin, v);
      //          analogWrite(yellowLEDPin, v);
      //          analogWrite(blueLEDPin, v);
      //          analogWrite(redLEDPin, v);
    }

  }
}
else   {
  //
  //    for (int v = maxPWM; v > lowPWM; v--) {
  //      delay(10);
  //      analogWrite(yellowLEDPin, v);
  //      analogWrite(blueLEDPin, v);
  //      analogWrite(redLEDPin, v);
  //    }
  activeLED == noLED;
  analogWrite(yellowLEDPin, lowPWM);
  analogWrite(blueLEDPin, lowPWM);
  analogWrite(redLEDPin, lowPWM);
}

}

Leave a Reply