int tiltPin = 1; //Pin for the the tilt sensor int buzzPin = 0; //Pin for the piezo buzzer int points = 0; //A point system is used to keep track of how long the tilt switch has been active as the sensor is very noisy and boucy int slouch = 0; //The variable associated with the tilt sensor. int totalPoints = 250; //the amount of points needed to trigger the tone. long timer = 0; //initializing the timer at 0 long interval = 25; //how often the code checks for a slouched postion and changes the point value void setup() { pinMode(tiltPin, INPUT); pinMode(buzzPin, OUTPUT); } void loop() { if(millis() - timer > interval){ slouch = digitalRead(tiltPin);//checks to see if the tilt sensor is in a slouch position every 25 ms if (slouch == 1) { points+=2;//if user is slouching, add 2 points. This is 2 instead of 1 since the sensor gives many false negatives when the piezo is vibrating the board. } else{ points--;//if usre is not slouching, subtract 1 point to try to get back to where the buzzer is shut off. } points = constrain(points, 0, totalPoints * 2); //Keeps the points from getting too far in one direction which keeps the system responsive. timer = millis(); } if (points > totalPoints) { tone(buzzPin, 1000);//will buzz continuously at 1000hz until the points go less than the threshold. } else { noTone(buzzPin); // needs to be called since the third input of tone(interval) is not being used. } }