Code is not working

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);
    }
	}
}

At what point in your program development did you discover the first error and decide not to fix it right then, but continue to add code.

1 Like

"Kitchen timer"

1 Like

I could look it up, but what’s a pomodoro ?

You should explain in your post if it’s something you found.

18 * 10^5 ms-per-pomo / 6 * 10^4 ms-per-minute
= 18/6 * 10^ (5-4) = 3 * 10^1
= 30 minutes/pomodoro

(because it is a function - sorry)
You never give "c" or "wait" a value... default is zero (wipe with delay(0)):

void colorWipe(uint32_t c, uint8_t wait)
    strip.setPixelColor(i, c);

: )

Pomodoro

I learned something today, but highly unlikely to use it as explained !

Does every kitchen not have a wind-up timer in the shape of a tomato? Would you expect uglifruit or celery? : )

It does.

i dont really get this? what does this do? do i just replace int Pomodoro = 1800000 with that?

Post #5... I was just figuring out how long pomodoro timer is... and maybe thought: If the timer is 30 minutes, maybe "nothing works" because you did not wait for 30 minutes...

What is:

this

and what is:

that

might i ask what app this is?

The image in post #9 is from your code (plus edits) in wokwi.com simulator. It has a limited device resource... but for many projects it is good enough.

the LCD display also only display 0, and does not show how long the LED will be on for

An int type variable can only store numbers between −32,768 to 32,767 so that is very wrong.

1 Like

Yes. A few people have been giving advice to you to make your sketch work. Start with the first advice, try to understand it, or ask questions, resolve that question, then move to the next advice. I gave some, too. Your turn.

[edit] START WITH @Grumpy_Mike advice. The int is making your sketch count negatively.

What may show underlines to you shows strikeout to me, lines through the text, not under.

Strikeout is right. I made mistakes.

1 Like

ouch!

...and I say "tom-ay-to", you say "potato'...