24 Second Food + Fitness

Critical Making Spring 2014 Provocation 1

Full instructions provided1,245
24 Second Food + Fitness

Story

Read more

Code

coding.

Plain text
coding.
/*
 Code for our 24 Second Food + Fitness toaster and microwave prototypes.
 
 This code was built off of a foundation of "Button", that is in the public domain.
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe
 
 http://www.arduino.cc/en/Tutorial/Button
 */
 //************SOUND COMPONENT ***********************//
#include <Wtv020sd16p.h>

int resetPin = 2;  // The pin number of the reset pin.
int clockPin = 3;  // The pin number of the clock pin.
int dataPin = 4;  // The pin number of the data pin.
int busyPin = 5;  // The pin number of the busy pin.

/*
Create an instance of the Wtv020sd16p class.
 1st parameter: Reset pin number.
 2nd parameter: Clock pin number.
 3rd parameter: Data pin number.
 4th parameter: Busy pin number.
 */
Wtv020sd16p wtv020sd16p(resetPin,clockPin,dataPin,busyPin);

//************SOUND COMPONENT ***********************//

//************ MICROWAVE STUFF ********************//
const int lightSensorPin = 0;
const int ledPin = 9;

int readingCount = 0;
unsigned int totalReading = 0;
int MAX_READINGS = 100;
int cutOff = 30; // needs to be tuned to environment, btw light/dark
int lightOn = 0;
//************ MICROWAVE STUFF *******************//


// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 1;     // the number of the pushbutton pin
const int redLedPin =  13;      // the number of the red LED pin
const int yelLedPin = 12;     // the number of the yellow LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
//unsigned long time;
int toasting = 0;  

int mode = 0; // 1 = microwave, 0 = toaster

void setup() {
  //Initializes the sound module.
  wtv020sd16p.reset();
  // initialize the LED pins as outputs:
  pinMode(redLedPin, OUTPUT); 
  pinMode(yelLedPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);  
  // start microsecond counter
  //Serial.begin(9600);  
}

void loop(){
  if (mode) {
    microwave_loop();
    delay(15);
  } else {
    toaster_loop();
  }
  
}

void toaster_loop(){
  //Serial.println("TOASTER MODE");
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  
  // # of microseconds since program started.
  //time = micros();

  // check if the pushbutton is pressed.
  // if it is, the buttonState is LOW:
  // when it is relased (the toaster is started)
  // the buttonState is HIGH
  if (buttonState == HIGH && toasting == 0) { 
    // indicate that we're in the toasting phase 
    toasting = 1;    
    // turn red LED on:    
    digitalWrite(redLedPin, HIGH);
    // PLAY TRACK
    wtv020sd16p.asyncPlayVoice(1);
    delay(500);
  } 
  if (buttonState == LOW && toasting == 1){
    // button is depressed, and we were just in toasting mode
    toasting = 0; // that means we're done toasting
    //start the "ready" sound
    //wtv020sd16p.asyncPlayVoice(1);
    
    // turn off the red LED:
    digitalWrite(redLedPin, LOW);
    //turn on the yellow pin:
    digitalWrite(yelLedPin, HIGH);
    wtv020sd16p.stopVoice();
    delay(500);
  }
    
  if (buttonState == LOW && toasting == 0){
    //button is depressed, and we're done toasting
    
    // start timer
    //Serial.begin(9600);
    // turn both LEDs off:
    digitalWrite(redLedPin, LOW); 
    digitalWrite(yelLedPin, LOW);
  }
}

void microwave_loop(){
  //  x = 7 % 5;   // x now contains 2
  int lightLevel = analogRead(lightSensorPin);
  totalReading = (lightLevel/10) + totalReading;
  readingCount = readingCount + 1;
  //Serial.println(lightLevel);
  if (readingCount > MAX_READINGS) {
    int averageVoltage = totalReading / readingCount;
    Serial.println(averageVoltage);
    readingCount = 0;
    totalReading = 0;
    if (averageVoltage > cutOff) {    //// Now we are in light open time
      if (lightOn == 0) {  /// Was off, incremenent to open
         Serial.println("Above Cutoff 0");
        lightOn = lightOn + 1;
      } else if (lightOn == 1) { /// Was open, is still open. Don't do anything
         Serial.println("Above Cutoff 1");
      } else if (lightOn == 2) { /// Was on and off twice. No it was started and the food
         Serial.println("Play track");
         Serial.println("Above Cutoff 2");
         /// Play track :)
         wtv020sd16p.asyncPlayVoice(0);
         lightOn = lightOn + 1;
      }else if (lightOn == 3) {  /// The microwave is running, dont have to do anything
         Serial.println("Above Cutoff 3");
      } else if (lightOn == 4) {  /// The food is done and the door is open
      
         lightOn = lightOn + 1;
         Serial.println("Above Cutoff 4");
      }else if (lightOn == 5) {  /// Door is open for microvave
         Serial.println("Above Cutoff 5");      
      }
    } else {  // below cutOff. Dark is off
    
       if (lightOn == 0) {  /// Was off, still off. Don't do anything
        Serial.println("Under Cutoff 0");  
      } else if (lightOn == 1) { /// Was open, but now it is closed. Increment to closed with food inside. 
        lightOn = lightOn + 1;
        Serial.println("Under Cutoff 1");  
      } else if (lightOn == 2) { /// Was off and is still off
        Serial.println("Under Cutoff 2");  
      }else if (lightOn == 3) { /// Has finished microwaving
        // Stop the track
        lightOn = lightOn + 1;
        Serial.println("Under Cutoff 3");  
        Serial.println("Stop Music"); 
        wtv020sd16p.stopVoice();
      }else if (lightOn == 4) {  /// Food is sitting in microwave
        Serial.println("Under Cutoff 4");  
      }else if (lightOn == 5) {  /// Door is closed and microwave is ready again
          Serial.println("Under Cutoff 5");  
          lightOn = 0;
      }
    }
  }

  delay(15); //just here to slow down the output for easier reading
    
}

Credits

Negah Nafisi

Negah Nafisi

4 projects • 2 followers
Emon Motamedi

Emon Motamedi

4 projects • 3 followers
Jeremy Fiance

Jeremy Fiance

5 projects • 0 followers
Kevin Simons

Kevin Simons

3 projects • 0 followers
Senior studying Electrical Engineering and Computer Science at UC: Berkeley
Molly Nicholas

Molly Nicholas

9 projects • 8 followers
I ran away from the Circus to join Science.

Comments