I found a template online which I found very useful to use. I am new to using an Arduino overall and I've tried my best to search for the code that can help my project work. My main issue is finding the code that can make a DC motor turn off while a Green LED is on and make the motor turn on while a Red LED is on. All of this is turned on by a button.
The following is the way my circuit is wired and the code
The lines of code that have /* */ were lines of code I found that I tried to make the DC motor work but didn't.
#include <LiquidCrystal.h>
// declare variables for keeping track of time
// and the number of study sessions
int seconds = 0;
int minutes = 0;
int count = 0;
// declare variables for the length of each
// period and the number of sessions before
// a longer break. Change these to customize
// your timer!
const int study_seconds = 15;
const int short_break_seconds = 5;
const int long_break_seconds = 15;
const int repeats = 4;
// a variable used to time the breaks later
int break_duration;
// declare pins used for hardware
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int led1 = 6;
const int led2 = 7;
const int button = 8;
const int motor1 = 9;
int state = 0;
unsigned long timestamp = 0;
void setup() // setup code that only runs once
{
lcd.begin(16, 2); // Set up the number of columns and rows on the LCD.
// set LED pins as outputs and button pin as input
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(motor1,OUTPUT);
pinMode(button,INPUT);
// display initial message to user
lcd.print("Ready to");
lcd.setCursor(0,1);
lcd.print("BEGIN :D!");
//wait for button press to start
while(digitalRead(button)==LOW);
/*(digitalRead(motor1)==LOW);*/{
//do nothing
}
state == digitalRead(led1);
//if (state == LOW){
//digitalWrite(motor1,HIGH);
//}
//if(state == High){
// digitalWrite(motor1,LOW);
//}
}
void loop()
{
count = 0; // set count to zero
digitalWrite(motor1,LOW);
if(millis()-timestamp > 1000){
state++;
timestamp = millis();
}
while(count<repeats){ // alternate timing between study and breaks
// print message and set LEDs for study time
lcd.clear();
lcd.print("What are we doing?");
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
/*lcd.setCursor(0, 0);
delay(500);
lcd.setCursor(16, 1);
lcd.autoscroll();
delay(500); */
// this is a good place to add your own code to control
// other hardware like buzzers or motors (make sure you
// also set the appropriate pins in the setup function)
/*switch(state){
case 0:
digitalWrite(motor1,!digitalRead(motor1));
digitalWrite(motor1, LOW);
digitalWrite(led1, HIGH);
break;*/
// reset minutes and seconds to zero
seconds = 0;
minutes = 0;
// count up and display the timer during study period
while(seconds<study_seconds){ // keep counting until we've reached the time limit for a study session
seconds = 0;
// print out the timer value in mm:ss format
while(seconds<16){
lcd.setCursor(0, 0);
lcd.setCursor(16, 1);
lcd.autoscroll();
if(minutes<10){ // if minutes is less than 10, we need to print an extra 0 to the display
lcd.print("0");
}
lcd.print(minutes);
lcd.print(":");
if(seconds<10){ // if seconds is less than 10, we need to print an extra 0 to the display
lcd.print("0");
}
lcd.print(seconds);
// wait for one second then increment the second counter
delay(1000);
seconds++;
}
// increment the minute counter after 60 seconds have elapsed
seconds++;
lcd.noAutoscroll();}
// now repeat the process for a study break
lcd.clear();
lcd.setCursor(0, 0);
digitalWrite(led1,LOW);
digitalWrite(led2,HIGH);
// this is a good place to add your own code to control
// other hardware like buzzers or motors (make sure you
// also set the appropriate pins in the setup function)
/*state == digitalRead(led2);
if (state == HIGH){
digitalWrite(motor1,LOW);
}
else{
digitalWrite(motor1,HIGH);
}*/
/* case 1:digitalWrite(motor1, !digitalRead(motor1));
digitalWrite(motor1, HIGH);
digitalWrite(led2, HIGH);
break;
}*/
if(count==(repeats-1)){ // do a long break on the last repetition
break_duration = long_break_seconds;
lcd.print("Long break!");
}
else{ // otherwise do a short break
break_duration = short_break_seconds;
lcd.print("BOOM!");
lcd.setCursor(0, 0);
delay(500);
lcd.setCursor(16, 1);
lcd.autoscroll();
delay(500);
}
lcd.setCursor(0, 1);
seconds = 0;
minutes = 0;
while(seconds<break_duration){
seconds = 0;
while(seconds<15){
lcd.setCursor(0, 1);
if(minutes<10){
lcd.print("0");
}
lcd.print(minutes);
lcd.print(":");
if(seconds<10){
lcd.print("0");
}
lcd.print(seconds);
delay(1000);
seconds++;
}
minutes++;
}
count++; // increment the counter for the number of study sessions
}
}


