Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Find ButtonOne Library at https://github.com/MrAlvin/ButtonOne
- *
- * Test circuit:
- *
- * Connect a pushbutton to pin A1 (ButtonPin), connect other side of button to ground.
- *
- * Pin 13 (StatusPin) is used for output
- * many Arduino versions already have a LED connected to this pin,
- * otherwise connect: pin13 - LED - resistor - ground
- *
- * Potentiometer connected to <+ - A3 - ground> see diagram example https://labitat.dk/wiki/Arduino_beginners_workshop#CIRC08_-_Twisting_-_.28Potentiometers.29
- *
- */
- #include "ButtonOne.h"
- int sensorPin = A3; // select the input pin for the potentiometer
- int ledPin = 13; // select the pin for the LED
- int buttonPin = 7;
- unsigned long sensorValue = 0; // variable to store the value coming from the sensor
- unsigned long relay_millis = 0;
- // Setup a ButtonOne instance on pin 7.
- ButtonOne button(buttonPin);
- //*********************************************
- // setup function - to run once:
- //*********************************************
- void setup() {
- // enable the standard led on pin 13.
- pinMode(ledPin, OUTPUT); // sets the digital pin as output
- //initiate internal button management values
- button.begin();
- // link the Press function to be called on a button Press event.
- button.attachPress(btnPress);
- // initialize serial communications at 9600 bps:
- Serial.begin(9600);
- Serial.println( F("Hello") );
- } // setup
- //*********************************************
- // main code - to run repeatedly:
- //*********************************************
- void loop() {
- button.check(); // keep watching the push button:
- sensorValue = analogRead(sensorPin);
- updateDisplay();
- time_out_relay();
- } // loop
- //*********************************************
- void time_out_relay() {
- if(millis() - relay_millis >= 0) {
- turn_off_relay();
- }
- }
- //*********************************************
- void turn_on_relay(){
- digitalWrite(ledPin, HIGH);
- Serial.println( F("Relay ON") );
- }
- //*********************************************
- void turn_off_relay(){
- digitalWrite(ledPin, LOW);
- Serial.println( F("Relay OFF") );
- }
- //*********************************************
- // this function will be called when the button is pressed
- //*********************************************
- void btnPress() {
- Serial.print("Off in ");
- Serial.print(sensorValue);
- Serial.print(" seconds");
- relay_millis = millis() + (sensorValue * 1000);
- turn_on_relay();
- } // btnPress
- //*********************************************
- // update display
- //*********************************************
- void updateDisplay() {
- static unsigned long last_millis = 0;
- if(millis() - last_millis > 1000) { //update once every second
- last_millis = millis(); //ready for next update
- Serial.println(sensorValue);
- }
- }
- // End
Advertisement
Add Comment
Please, Sign In to add comment