I want to make a DC motor turn on while an LED is on but make it turn off when a Second LED is on with the click of a button

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

== is for a compare, = is an assignment.

Why do you want to read the LEDs? The usual approach is to keep a variable that is set when a condition is true and is cleared when another condition is true. Your motor can run based on that variable.

This doesn't look correct

image

Assuming that the device is a MOSFET, there is no positive connection.

what was so useful? what do you expect the code to do

when i strip out the lcd operations and questionable while loops i get

void loop ()
{
    count = 0;  // set count to zero
    digitalWrite (motor1,LOW);

    if (millis()-timestamp > 1000)  {
        state++;
        timestamp = millis ();
    }

    // alternate timing between study and breaks
    while (count < repeats)  {
        digitalWrite (led1, HIGH);
        digitalWrite (led2, LOW);

        digitalWrite (led1,LOW);
        digitalWrite (led2,HIGH);
    }
}

this code toggle the motor and LED pins on/off with each button press

const byte led1 = 6;
const byte led2 = 7;
const byte button = 8;
const byte motor1 = 9;

byte butState;

enum { LedOn   = LOW,  LedOff   = HIGH };   // active LOW
enum { MotorOn = HIGH, MotorOff = LOW };    // turn on MOSFET
enum { Off, On };
int state = Off;

char s [90];

// -----------------------------------------------------------------------------
void motorOff (void)
{
    digitalWrite (motor1, MotorOff);
    digitalWrite (led1,   LedOff);
    digitalWrite (led2,   LedOn);
    state = Off;
}

// -------------------------------------
void motorOn  (void)
{
    digitalWrite (motor1, MotorOn);
    digitalWrite (led1,   LedOn);
    digitalWrite (led2,   LedOff);
    state = On;
}

// -----------------------------------------------------------------------------
void loop ()
{
    byte but = digitalRead (button);
    if (butState != but)  {
        butState = but;
        delay (20);         // debounce

        if (LOW == but)  {
            if (Off == state)
                motorOn ();
            else
                motorOff ();
        }
    }
}

// -----------------------------------------------------------------------------
void setup () // setup code that only runs once
{
    pinMode (led1,   OUTPUT);
    pinMode (led2,   OUTPUT);
    pinMode (motor1, OUTPUT);

    pinMode (button, INPUT_PULLUP);
    butState = digitalRead (button);

    motorOff ();
}

If possible, can you give a public link to your project in Tinkercad, so other can make a copy and tinker with it ?
Don't give a link to share your project, because then others can alter your project.
I don't understand the difference between the two links in Tinkercad, but perhaps you can figure it out.

The Flow Chart of Fig-1 has described the solution to the problem of turing On/Off the Motor when the Button is pressed. Convert it into programming codes and execute on Arduino UNO.


Figure-1:

Yiu seem to have a certain picture of how programming works.

Well it does not work like entering the right cheat-code into a computer-game to directly reach platinum-level.

There are a quadrillion - this is a 1 with 15 zeros (1.000.000.000.000.000) ways to write code
Still for just writing code with a functionality of

For this subset of all possible codes there are still 10.000 ways to do it.
And this is the reason why it is pure luck to find exactly such a code.

The solution is:
learning programming.
Your code consists of

  • read in a button
  • switch on / off an LED
  • switch on / off a DC-motor

very basic stuff.

You can invest another 200 hours of searching for exact your code. Very unlike to find it
You can invest a tenth of this time = 20 hours to learn basic programming and you will have a solution.

So what do you prefer?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.