I am making a lamp that utilises the pomodoro study method, I have a arduino uno thats controlling a LED strip and a LCD display thats connected to a rotary encoder to adjust the duration of each pomodoro time period, and another arduino uno thats controlling ultrasonic sensor and a motor that turns and locks a drawer whenever the ultrasonic sensor detects that a phone has been placed in the drawer, this arduino is also connected to the same rotary encoder to control the pomodoro time period. The LED strip and motor is supposed to activate when the button on the rotary encoder is pressed. However, the motor is acting out and is turning sometimes even when no phone is present, and the rotary encoder is also not increasing or decreasing the value of the variable for the pomodoro period as the LCD is not displaying the increments or decrements.
Code for LCD, LED, and rotary arduino
#include <Adafruit_NeoPixel.h>
#include <LiquidCrystal_I2C.h>
//Rotary_Encoder:
#define CLK 2
#define DT 3
#define SW 4
int Pomodoro = 1800000;
int P;
int currentStateCLK;
int lastStateCLK;
String currentDir ="";
unsigned long lastButtonPress = 0;
//LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
//LED
#define PIN 11
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void setup() {
//LCD
lcd.init(); // initialize the lcd
lcd.backlight();
//LED
strip.begin();
strip.show(); // Initialize all pixels to 'off'
//Rotary_Encoder
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW, INPUT_PULLUP);
lastStateCLK = digitalRead(CLK);
}
void loop() {
//Rotary
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != currentStateCLK) {
Pomodoro = Pomodoro - 60000;
P = Pomodoro/60000;
lcd.clear();
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Time:");
lcd.print(P);
currentDir ="CCW";
} else {
// Encoder is rotating CW so increment
Pomodoro = Pomodoro + 60000;
P = Pomodoro/60000;
lcd.clear();
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Time:");
lcd.print(P);
currentDir ="CW";
}
}
lastStateCLK = currentStateCLK;
int btnState = digitalRead(SW);
//If we detect LOW signal, button is pressed
if (btnState == LOW) {
//LED
colorWipe(strip.Color( 255, 125, 125), 0);
delay(Pomodoro);
colorWipe(strip.Color( 0, 0, 0), 0);
}
}
Code for motor, Rotary and ultrasonic sensor
#include <Servo.h>
//Rotary_Encoder:
#define CLK 2
#define DT 3
#define SW 4
int Pomodoro = 5;
int currentStateCLK;
int lastStateCLK;
String currentDir ="";
unsigned long lastButtonPress = 0;
//Servo:
Servo myservo;
int pos = 0;
//Ultrasonic:
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;
void setup() {
//Servo:
myservo.attach(11);
//Ultrasonic:
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
//Rotary_Encoder
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW, INPUT_PULLUP);
lastStateCLK = digitalRead(CLK);
}
void loop() {
//Rotary
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != currentStateCLK) {
Pomodoro - 60000;
currentDir ="CCW";
} else {
// Encoder is rotating CW so increment
Pomodoro + 60000;
currentDir ="CW";
}
}
lastStateCLK = currentStateCLK;
int btnState = digitalRead(SW);
//Ultrasonic
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
//If we detect LOW signal, button is pressed
if (btnState == LOW) {
if (distance <= 5) {
myservo.write(180);
} else {
myservo.write(0);
}
}
}

