#include <Servo.h>
Servo servo7;
Servo servo8;
int Button = 2;
unsigned long ButtonStartTime = 0;
int ButtonState = 0;
int LastButtonState = 0;
int FiveSecPress = 3750; //5 second timer
int DoubleClickPress = 500; //Time between clicks
bool DoubleClickDetected = false;
unsigned long LastButtonClickTime = 0;
unsigned long timePress = 0;
unsigned long timePressLimit = 0;
int clicks = 0;
const int temperaturePin = A0; //needs analogue pin
const int LED = 11;
const float temperaturemax = 50.0; //deg cel
//the following is needed if using the LM335
//float sensorValue;
//float voltageOut;
//float tempcelcius;
void setup() {
// put your setup code here, to run once:
servo7.attach(7);
servo8.attach(8);
pinMode(Button, INPUT_PULLUP);
pinMode(LED, OUTPUT);
servo7.write(0);
servo8.write(0);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
ButtonState = digitalRead(Button);
if (ButtonState == LOW){
if (LastButtonState == HIGH){
ButtonStartTime = millis();
}
if (millis() - ButtonStartTime >= FiveSecPress){
servo7.write(0); //change angle according to how far we want hand to pull in
servo8.write(0);
delay(1000);
}
}
LastButtonState = ButtonState;
//if (DoubleClickDetected) {
//DoubleClickDetected = false; // Reset the flag
//}
ButtonState = digitalRead(Button);
if (ButtonState == LOW){
//delay(200);
Serial.println("Button Pressed");
if (clicks == 0) {
timePress = millis();
timePressLimit = timePress + 1000;
clicks = 1;
//delay(200);
}
//LastButtonState = ButtonState;
}
ButtonState = digitalRead(Button);
if (ButtonState == LOW && clicks == 1){
if ((millis() - timePress) < DoubleClickPress){
Serial.println("Button Pressed twice");
servo7.write(180);
servo8.write(180);
delay(1000);
timePress = 0;
timePressLimit = 0;
clicks = 0;
}
LastButtonState = ButtonState;
}
//the following is for the wokwi temp sensor
const float BETA = 3950;
int analogValue = analogRead(A0);
float tempcelcius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
//the following is for the LM335
//sensorValue = analogRead(temperaturePin);
//voltageOut = (sensorValue * 5000) / 1024;
//tempcelcius = voltageOut / 10;
if (tempcelcius > temperaturemax){
digitalWrite(LED, HIGH);
}
else{
digitalWrite(LED, LOW);
}
}