#define led1 7
#define led2 10
int count = 0;
int switchPin = 4; // button pin
int switchState = HIGH; // button value
int pinA = 2; // Rotary encoder Pin A / DT
int pinB = 3; // Rotary encoder Pin B / CLK
int pinAstateCurrent = LOW; // Current state of Pin A
int pinAStateLast = pinAstateCurrent; // Last read value of Pin A
void setup() {
Serial.begin (9600); // Initialise the serial monitor
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode (switchPin, INPUT); // Enable the switchPin as input with a PULLUP resistor
digitalWrite(switchPin, HIGH);
pinMode (pinA, INPUT); // Set PinA as input
pinMode (pinB, INPUT); // Set PinB as input
//digitalWrite(pinA, HIGH);
//digitalWrite(pinB, HIGH);
// Atach a CHANGE interrupt to PinB and exectute the update function when this change occurs.
attachInterrupt(digitalPinToInterrupt(pinB), update, CHANGE);
}
void loop() {
// BUTTON
switchState = digitalRead(switchPin); // Read the digital value of the switch (LOW/HIGH)
// If the switch is pressed (LOW), print message
if (switchState == LOW) {
count = 0;
Serial.println((String)"Reset strobe. Count = " + count);
}
switch (count) {
case 1: str1();
break;
case 2: str2();
break;
case 3: str3();
break;
case 4: str4();
break;
case 5: str5();
break;
case 6: str6();
break;
case 7: str7();
break;
}
}
void update() {
// ROTATION DIRECTION
pinAstateCurrent = digitalRead(pinA); // Read the current state of Pin A
// If there is a minimal movement of 1 step
if ((pinAStateLast == LOW) && (pinAstateCurrent == HIGH)) {
if (digitalRead(pinB) == HIGH) { // If Pin B is HIGH
if (count == 0 || count == 1)
count = 7;
else
count--;
Serial.println((String)"Count = " + count); // Print on screen
} else {
if (count == 7)
count = 1;
else
count++;
Serial.println((String)"Count = " + count); // Print on screen
}
}
pinAStateLast = pinAstateCurrent; // Store the latest read value in the currect state variable
}
// Mode strobe
void str1() {
setL1(150);
setL2(150);
}
void str2() {
setL1(150);
delay(700);
setL2(150);
}
void str3() {
for (int i = 0; i < 3; i++)
setL1(150);
for (int i = 0; i < 3; i++)
setL2(150);
}
void str4() {
for (int i = 0; i < 3; i++)
{
setL1(150);
delay(150);
}
for (int i = 0; i < 3; i++)
{
setL2(150);
delay(150);
}
}
void str5() {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(500);
}
void str6() {
for (int i = 0; i < 5; i++)
{
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
delay(100);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(100);
}
delay(500);
}
void str7() {
str2();
str4();
str3();
}
void setL1(int time) {
digitalWrite(led1, HIGH);
delay(time);
digitalWrite(led1, LOW);
}
void setL2(int time) {
digitalWrite(led2, HIGH);
delay(time);
digitalWrite(led2, LOW);
}