// We'll be controlling the motor from pin 9. This must be one of the PWM-capable pins. const int motorPin = 9; /// This pin brings the pressure data back to the computer const int pressureSensorPin = A0; //LED Pins const int led1 = 2; const int led2 = 3; const int led3 = 4; const int led4 = 5; const int led5 = 6; // Electir Imp pin const int electricImpPin = 7; // Project constants const int maxTimeSitting = .5 * 1000; // This is how many loop cycles the user can be sitting down const int pressureCutOff = 800; /// Projects Changing Variables int currentTimeSitting = 0; /// How long the user has been sitting. Right now it resets when the motor turns on void setup() { pinMode(motorPin, OUTPUT); pinMode(led1,OUTPUT); pinMode(led2,OUTPUT); pinMode(led3,OUTPUT); pinMode(led4,OUTPUT); pinMode(led5,OUTPUT); pinMode(electricImpPin,INPUT); Serial.begin(9600); } void loop() { int isSitting = isSittingFunction(); if (isSitting == 1) { handleSitting(); } else { handleStanding(); } } /// Checks if the user it sitting by reading in the pressure sensor int isSittingFunction(){ int pressureSensorReading = analogRead(pressureSensorPin); if (pressureSensorReading < pressureCutOff){ return 1; } return 0; } /// Handles the user sitting down. Increments the currentTimeSitting and if need be starts the process of making the user stand up void handleSitting() { int impVal = digitalRead(electricImpPin); if (currentTimeSitting >= maxTimeSitting && impVal == 0) { /// Sat and used all time, have not taken enough steps, sat back down. turnAllOn(); return; } if (currentTimeSitting >= maxTimeSitting && impVal == 1) { /// Sat and used all time, have taken enough steps, sat back down. Will reset resetState(); return; } if (currentTimeSitting < maxTimeSitting){ currentTimeSitting = currentTimeSitting + 1; } if (currentTimeSitting > maxTimeSitting/5) { /// if statement needed for integer overflow digitalWrite(led1, HIGH); } if (currentTimeSitting > 2*maxTimeSitting/5) { digitalWrite(led2, HIGH); } if (currentTimeSitting > 3*maxTimeSitting/5) { digitalWrite(led3, HIGH); } if (currentTimeSitting > 4*maxTimeSitting/5) { digitalWrite(led4, HIGH); } if (currentTimeSitting >= maxTimeSitting) { /// COULD SET OUT TO HOT TO SIGNAL START STEPS digitalWrite(led5, HIGH); digitalWrite(motorPin, HIGH); } } void resetState(){ currentTimeSitting = 0; turnAllOn(); } /// Function that handles the user not being in the chair void handleStanding(){ turnAllOff(); } void turnAllOn(){ digitalWrite(led1, HIGH); digitalWrite(led2, HIGH); digitalWrite(led3, HIGH); digitalWrite(led4, HIGH); digitalWrite(led5, HIGH); digitalWrite(motorPin, HIGH); } void turnAllOff(){ digitalWrite(led1, LOW); digitalWrite(led2, LOW); digitalWrite(led3, LOW); digitalWrite(led4, LOW); digitalWrite(led5, LOW); digitalWrite(motorPin, LOW); }