#define BUTTON_A 2
#define BUTTON_B 4
#define LED_1 11
#define LED_2 10
#define LED_3 9
const int BUTTON_PIN = 4; // the number of the pushbutton pin
const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds
const int LONG_PRESS_TIME = 1000; // 1000 milliseconds
// Variables will change:
int lastState = LOW; // the previous state from the input pin
int currentState; // the current reading from the input pin
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
bool isPressing = false;
bool isLongDetected = false;
// VARS regarding reading the input
byte input_command = 0; // Sets which input is given (See input_table)
byte button_pressed = 0;
// VARS regarding the light/LEDs
byte light_mode = 0; // Define what light mode is active (See light_table)
byte intensity = 100; // The intensity of the light
byte weights[3] = {100, 155, 120}; // Weight to make the intensity f the different LEDs equal(-ish)
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);
pinMode(BUTTON_A, INPUT_PULLUP);
pinMode(BUTTON_B, INPUT_PULLUP);
}
void loop() {
// read the state of the switch/button:
currentState = digitalRead(BUTTON_PIN);
if(lastState == HIGH && currentState == LOW) { // button is pressed
pressedTime = millis();
isPressing = true;
isLongDetected = false;
} else if(lastState == LOW && currentState == HIGH) { // button is released
isPressing = false;
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if( pressDuration < SHORT_PRESS_TIME )
Serial.println("B short press is detected");
}
if(isPressing == true && isLongDetected == false) {
long pressDuration = millis() - pressedTime;
if( pressDuration > LONG_PRESS_TIME ) {
Serial.println("A long press is detected");
isLongDetected = true;
}
}
// save the the last state
lastState = currentState;
}
// Controls the light. Sets the light depending on input
// and the light_table
void light_control(byte mode) {
switch (mode) {
// Mode 0 is lights off
case 0:
analogWrite(LED_1, 0);
analogWrite(LED_2, 0);
analogWrite(LED_3, 0);
break;
// Mode 1 is lights on
case 1:
analogWrite(LED_1, intensity * (weights[0]/100));
break;
case 2:
analogWrite(LED_2, intensity * (weights[1]/100));
break;
};
}
// True = a button is pressed
// False = a button is NOT pressed
byte scan_button() {
if (digitalRead(BUTTON_A) == LOW) {
return 1;
}
else if (digitalRead(BUTTON_B) == LOW) {
return 2;
}
else {
return 0;
};
}
// Runs a while loop while a button is pressed.
// Reads the time passed while button is pressed,
// and changes the value of light_mode
void read_input() {
unsigned long start_time; // Stores the time for when the function is called and a button is pressed, the first time
unsigned long wait_time; // Stores the time waited while running loop
start_time = millis();
analogWrite(LED_3, 0);
while (scan_button() > 0) {
}
delay(500);
analogWrite(LED_3, 10);
delay(500);
wait_time = millis();
if (wait_time - start_time > 2000) {
light_mode = 1;
} else {
light_mode = 0;
}
}