Back to Parent

//Variables to control blinking for status indicator
const int ledPin = D0;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 1000; //interval at which to blink the LED

//variabless for sensor feedback
const int sensePin = D1;
int PWM = 0;

//Holds GND and 5V pins of accelerometer
const int gndPin = A0;
const int powerPin = A4;

//Pin of x axis on the accelerometer
const int xPin = A3;

//Variables to take time into account
long t1 = 0;
long t2 = 0;
bool delayState = false;
int dt = 1000;

//variables for low pass filter
int x = 0;
int x1 = 0;
int x2 = 0;
float y1 = 0;
float y2 = 0;
int y2int = 0;
float alpha = .05; //value for low pass

//Values to tell whether the washer is on
bool on = FALSE;
int onPin = D2;
bool washing_machine = false;

//control switch
int switchPin = D3;
int switchState = LOW;
int reading;
long t; //helps to prevent switch bounce
long debounce = 200; //time interval for how often the pin is checked
int previous =HIGH;


void setup(){
  //this method for linking the DXL325 accelerometer can be found on their website
  pinMode(gndPin, OUTPUT);
  pinMode(powerPin,OUTPUT);
  digitalWrite(gndPin, LOW);
  digitalWrite(powerPin, HIGH);
  //Starting Serial to use for debugging
  Serial.begin(9600);
  // lone input: off/on switch
  pinMode(switchPin,INPUT);
  //setting outputs
  pinMode(ledPin, OUTPUT);
  pinMode(sensePin, OUTPUT);
  pinMode(onPin, OUTPUT);

}

void filter(){
  x = analogRead(xPin) - 2037; //accounting for gravity offset
  x2 = map(abs(x),0,400,0,255); //mapping sensor data to PWM
  y2 = (y1 + alpha*(x2 - y1)); //algorithm found on wikipedia
  y1 = y2;
  x1 = x2;
  y2int = int(y2); //converting result to integer to pass to PWM
  analogWrite(sensePin, y2int); // controls light to correlate to how much shaking
  delay(100);
}
void loop(){
  reading = digitalRead(switchPin);
  if (reading == LOW && previous == HIGH && millis() - t > debounce) {
    if (switchState == HIGH)
      switchState = LOW;
    else
      switchState = HIGH;
    t = millis();
  }
  previous = reading;
  Serial.print(" switchState:");
  Serial.println(switchState);
  if (switchState == HIGH){
    filter();
    Serial.print(" y2int:");
    Serial.println(y2int);
    if (y2int > 20)
      {on = TRUE;}
    if (on == TRUE && y2int < 10){
      if(!delayState)  {
        t1 = millis();
        delayState = true;
        Serial.print(" delaying ");
      }
      else {
        t2 = millis() - t1;
        if (t2 > dt) {
          Serial.print(" time elapsed ");
          if (y2int < 10) {
            for (int i = 0; i< 3; i++) {
              digitalWrite(onPin, HIGH);
              delay(500);
              digitalWrite(onPin, LOW);
              delay(500);
              }
              on = FALSE;
              Particle.publish("done!");
              Serial.println(" done ");
            }
          else {
              Serial.println(" still shaking ");
            }
            delayState = false;
            Serial.println(" delaying ");
          }
        }
      }
  //function for blinking LED without delay
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // save the last time LED blinked
    previousMillis = currentMillis;
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    digitalWrite(ledPin, ledState);
  }
  }
}
Click to Expand

Content Rating

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

0