Back to Parent

Code for control panel / phone tray
int buttonPin = D0;         //button to start the project
int ledPin = D3;            //LED indicating start and end of project
int projectStatus = 0;      // 0 = off, 1 = on
int buttonState;
int brushPin1 = D1;          //represents brush movement for diner1
int brushPin2 = D5;          //represents brush movement for diner2
//int fsrPin = A0;          //pressure sensor
//int fsrReading;
//int phoneOn = 3000;       //pressure with phone
//int phoneOff = 1500;      //pressure without phone
int switchPin1 = D2;        //switch as an alternative to pressure sensor for diner1
int switchState1;
int switchPin2 = D4;        //switch to show phone down for diner2
int switchState2;
int drip1 = 0;              // 0 = no dripping, 1 = dripping
int drip2 = 0;
int potPin = A1;            //time input
int potReading = 0;
int duration = 1;           // duration in minutes
int starttime,endtime;      // start and end time of the project
void setup(){
  Serial.begin(9600);
  pinMode( buttonPin , INPUT_PULLUP);
  //pinMode( fsrPin, INPUT);
  pinMode( switchPin1, INPUT_PULLUP);
  pinMode( switchPin2, INPUT_PULLUP);
  pinMode( brushPin1, OUTPUT );
  pinMode( brushPin2, OUTPUT );
  pinMode( ledPin, OUTPUT);
  //Particle.variable("pressure",&fsrReading,INT);
  Particle.variable("pot", &potReading, INT);
}
void loop(){
  buttonState = digitalRead( buttonPin );
  //fsrReading = analogRead(fsrPin);
  switchState1 = digitalRead( switchPin1);
  switchState2 = digitalRead( switchPin2);
  potReading = analogRead(potPin);
  //Serial.println(potReading);
  if ( buttonState == LOW && projectStatus == 0){
    //project begin
    digitalWrite(ledPin, HIGH);
    projectStatus = 1;
    delay(1000);
    //setup starttime and endtime
    starttime=millis();
    Serial.println(starttime);
    if (potReading<=575){
      duration = 1;
    }else if (potReading>575 && potReading<=1150){
      duration = 2;
    }else if (potReading>1150 && potReading<=1725){
      duration = 3;
    }else if (potReading>1725 && potReading<=2300){
      duration = 4;
    }else if (potReading>2300){
      duration = 5;
    }
    duration = duration * 60000;
    endtime = starttime + duration;
  }else if ( (buttonState == LOW || endtime == millis() ) && projectStatus == 1 ){  // if button is pressed again or time is up
    // project end
    digitalWrite( ledPin, LOW);
    projectStatus = 0;
    delay(1000);
  }
  if (projectStatus == 1){              // project is on
    project();    //run project
  }else if(projectStatus == 0){         // project is low
    // stop brush or drippter
    digitalWrite(brushPin1, LOW);
    digitalWrite(brushPin2, LOW);
    Particle.publish("diotDimSum1Off");
    Particle.publish("diotDimSum2Off");
  }
}
void project(){
  if (switchState1 == LOW){                  // put phone on switch
  // move brush or drippter
    digitalWrite(brushPin1, HIGH);
    if(drip1 == 0)                           // if it's not dripping now
    {
      Particle.publish("diotDimSum1On");
      drip1=1;                               // let it drip
    }
  }else if (switchState1 == HIGH){           // lift up phone from switch
    // stop brush or drippter
    digitalWrite(brushPin1, LOW);
    if(drip1==1)                             // if it's dripping now
    {
      Particle.publish("diotDimSum1Off");
      drip1 = 0;                             // stop dripping
    }
  }
  if (switchState2 == LOW){
  // move brush or drippter
    digitalWrite(brushPin2, HIGH);
    if(drip2 == 0)
    {
      Particle.publish("diotDimSum2On");
      drip2 = 1;
    }
  }else if (switchState2 == HIGH){
    // stop brush or drippter
    digitalWrite(brushPin2, LOW);
    if(drip2 == 1)
    {
      Particle.publish("diotDimSum2Off");
      drip2 = 0;
    }
  }
}
Tongtong Lu Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0