#include <LiquidCrystal_I2C.h>
#include "DS3231.h"
#include "RotaryEncoder.h"
#include <Servo.h>
// Define servo
Servo myservo;
//Define LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
// Define pins
// 3 positions Switch
#define pinSwitch_manual 12
#define pinSwitch_auto 13
//Rotary encoder
RotaryEncoder encoder(A2, A3);
static int pos = 0;
int newPos;
// Define variables
int8_t Counter = 0;
uint8_t TimeChoice = 0;
int PrevPosition; // Previous Rotary position Value to check accuracy
int StepsToTake; // How much to move Stepper
volatile boolean TurnDetected; // need volatile for Interrupts
volatile boolean rotationdirection; // CW or CCW rotation
void setup() {
Serial.begin(9600);
Serial.println("Code Start");
Serial.println(pos);
//Initialize servo
myservo.attach(5);
myservo.write(90);
// Initialize Switch
pinMode(pinSwitch_manual,INPUT);
pinMode(pinSwitch_auto,INPUT);
//Initialize LCD
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.clear(); // clear display
//Initialize Current Time
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
// Initialize timer2
noInterrupts(); //deactivation of interrupt
TCCR2A = 0b00000000; // WGM11=0 and WGM10=0 to set the comparison
TCCR2B = 0b00001111; // WGM12=1 and WGM13=1 to set the comparison / CS22=2 and CS21=1 and CS20=1 to set a division by 1024 on the prescaler of timer2
TIMSK2 = 0b00000010; // OCF1A=1 to activate of interrupts "A" (equality between timer and value in register OCR1A)
TCNT2 = 0; // start timer to 0
OCR2A = 255; // value max attain by timer
interrupts(); //activation of interrupt
}
// initialization interrupt
ISR(TIMER2_COMPA_vect)
{
Counter = Counter + 1;
}
void loop() {
/*
encoder.tick();
newPos = encoder.getPosition();
*//*
if (pos != newPos) {
Serial.println(newPos);
pos = newPos;
}
*/
if (digitalRead(pinSwitch_auto)==HIGH){
Serial.println("auto");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Auto");
lcd.setCursor(0, 1);
if
myservo.write(0);
delay(1000);
}
else{
if (digitalRead(pinSwitch_manual)==HIGH){
//manual
pos = pos + 1;
Serial.println("Manuel");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Manuel");
myservo.write(90);
lcd.setCursor(0, 1);
lcd.print("Position : ");
lcd.print(pos);
delay(1000);
}
else{
//nothing
Serial.println("Rien");
delay(1000);
}
}
// myservo.write(0);
}