#include "Compass.h"
#define GPIO_POT 2// GPIO INPUT D2 Potentiometre
#define ENCODER_BTN 4
#define ENCODER_CLK 32
#define ENCODER_DT 33
Compass compass;
//*********************************
//****** SETUP *************
//*********************************
void setup() {
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING);
pinMode(ENCODER_BTN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ENCODER_BTN), setSpeed, FALLING);
compass.setup(0, 0);
}
//*********************************
//****** LOOP *************
//*********************************
int lastClk = HIGH;
int counter = 0;
bool speedBtn = false;
void loop() {
// put your main code here, to run repeatedly:
//delay(10); // this speeds up the simulation
//compass.update(analogRead(GPIO_POT));
compass.draw(counter);
delay(200);
}
void readEncoder() {
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == LOW) {
Serial.println("Rotated counterclockwise ⏪");
counter=counter+speedBtn*10+!speedBtn*1; // Clockwise
if (counter >= 3600) {
counter = counter-3600;
}
}
if (dtValue == HIGH) {
Serial.println("Rotated clockwise ⏩");
counter=counter-speedBtn*10-!speedBtn*1; // Counterclockwise
if (counter < 0) {
counter = 3600+counter;
}
}
//Serial.print("Counter: ");
//Serial.println(counter);
}
void setSpeed() {
speedBtn=!speedBtn;
}