motor control help

Hello everyone, I am kind of new to programming. I do know electronics fairly well though. I am trying to make a motor turn on for a period of time for example 30 seconds when a button is pressed. Then I want the motor to turn do nothing until the button is pressed again. Once it is pressed I want the motor to reverse itself for 30 seconds and stop.

So far I have got the motor to turn in both forward and reverse direction, however its for an indefinite amount of time. I cant figure out how to make it stop. I also cant figure out what kind of if statement to use to make the motor to go forward and then reverse while waiting for a signal from the button before doing either of those.

If someone could help me out with one of those problems I would be really grateful.

Thank you in advance,

JBate

// PWM is connected to pin 3.
const int pinPwm = 3;

// DIR is connected to pin 2.
const int pinDir = 2;

void Forward();
void Reverse();

void setup()
{
pinMode(pinPwm, OUTPUT);
pinMode(pinDir, OUTPUT);
}

void loop()
{
Forward();
delay(30000);
Reverse();

}

void Forward()
{
analogWrite(pinPwm, 100); //Establishes forward direction of Channel A
digitalWrite(pinDir, LOW); //Spins the motor on Channel A at full speed
}

void Reverse()
{
analogWrite(pinPwm, -100);
digitalWrite(pinDir, HIGH);
}

When you post your code on the forum, use the "#" CODE TAGS button (above just to the left of the " QUOTE" button)
so it will create a scroll window to scroll through the code.

  // PWM is connected to pin 3.
const int pinPwm = 3;

// DIR is connected to pin 2.
const int pinDir = 2;

void Forward();
void Reverse();


void setup() 
{
  pinMode(pinPwm, OUTPUT);
  pinMode(pinDir, OUTPUT);
}

void loop()
{
    Forward();
    delay(30000);
    Reverse();
     
}

void Forward()
{
 analogWrite(pinPwm, 100); //Establishes forward direction of Channel A  
 digitalWrite(pinDir, LOW);   //Spins the motor on Channel A at full speed
 }

void Reverse()
{
  analogWrite(pinPwm, -100);
  digitalWrite(pinDir, HIGH);
}

Look at this:

and the BlinkwithoutDelay IDE Example

There is a lot wrong with that sketch. The comments and instructions do not match.
It is the digital wrote that sets the direction and the analogue write that sets the speed. Also analogWrite does not take negitave values. An analogWrite of a zero value will stop the motor.

Thank you for your help, I figured out most of everything that I needed to.

I still don't really understand millis, other then it is like delay but you can still have other signals being sent.

As for the analogWrite and digitalWrite thank you for your help I fixed the problems that I had and then I also made a Stop() function that just does analogWrite(3, 0) and that stopped the motor like you said.

Thank both of you for your help.

Ok look at the attached example:

      #include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
int garagesensorpin =10;
int blpin =9;

//  DEFINERE VARIABLER
//  ------------------
    // Kode for å få æ, ø, å, Æ, Ø, Å i display
    byte Lae[8] = {B00000,B00000,B11010,B00101,B01111,B10100,B11111,B00000}; // æ 
    byte Loe[8] = {B00000,B00001,B01110,B10101,B10101,B01110,B10000,B00000}; // ø 
    byte Laa[8] = {B00100,B00000,B01110,B00001,B01111,B10001,B01111,B00000}; // å 
    byte Sae[8] = {B01111,B10100,B10100,B11110,B10100,B10100,B10111,B00000}; // Æ 
    byte Soe[8] = {B00001,B01110,B10011,B10101,B11001,B01110,B10000,B00000}; // Ø 
    byte Saa[8] = {B00100,B00000,B01110,B10001,B11111,B10001,B10001,B00000}; // Å
    // æ, ø, å, Æ, Ø, Å er plassert i RAMadresse 0, 1, 2, 3, 4, 5



void setup()
{
    lcd.init();                      // initialize the lcd 
    lcd.begin(16,2);   // stille inn displayet for 16x2 tegn, og slå på bakbelysningen
    lcd.backlight();
    pinMode(garagesensorpin, INPUT_PULLUP);      // Bryter til garasjen
    pinMode(blpin, INPUT_PULLUP);           // Bryter til backlight
    
    // Laste opp seks selvdefinerende bokstaver
    lcd.createChar(0, Lae); // æ 
    lcd.createChar(1, Loe); // ø 
    lcd.createChar(2, Laa); // å 
    lcd.createChar(3, Sae); // Æ 
    lcd.createChar(4, Soe); // Ø 
    lcd.createChar(5, Saa); // Å 
    lcd.clear();            // Nødvendig etter Upload

}

void loop()
{
    
    // Text on display  
    int garagesensorpinStatus = 0;                      
    garagesensorpinStatus = digitalRead(garagesensorpin); 
    
    if (garagesensorpinStatus == HIGH) 
    {
        lcd.setCursor(0,0);  
        lcd.print("Garage");        
        lcd.setCursor(0,1);   
        lcd.print("is  ");                  
        lcd.print("open   ");         
      } 
      else 
      {                
        lcd.setCursor(0,0); 
        lcd.print("Garage");         
        lcd.setCursor(0,1);          
        lcd.print("is closed");            
    }

    // Backlight on - ten seconds - then off again   
    int blpinStatus = 0;
    blpinStatus = digitalRead(blpin);
    static unsigned long blTime;
    static boolean blOn;
    
    if (blpinStatus == LOW) 
    {
      delay(20);        // Debounce
      blOn = true;
      blTime = millis();
      lcd.backlight();
     } 
    if (blOn == true && millis() - blTime > 10000)
    {
      lcd.noBacklight();
      blOn = false;
    }
}

Look at this example.
See the variable :

  static unsigned long blTime;

Now look at this:

 if (blpinStatus == LOW) 
    {
      delay(20);        // Debounce
      blOn = true;
      blTime = millis();
      lcd.backlight();
   }
    if (blOn == true && millis() - blTime > 10000)
    {
      lcd.noBacklight();
      blOn = false;
    }

See how the flag is set , the variable reads the millis time to get the start time , executes the command to turn on the backlight
and then begins checking for an ELAPSED TIME of TEN SECONDS. (10000 mS)
You MUST SEE THAT , right ?

Now here it says "IF ELAPSED TIME = 10 SECONDS, , RESET FLAG AND TURN OFF BACKLIGHT .
(The whole purpose of this entire program is just to turn ON the backlight, wait 10 seconds , then turn it off.)

if (blOn == true && millis() - blTime > 10000)
    {
      lcd.noBacklight();
      blOn = false;

Now look at your first post and tell me if the lightbulb goes on…

I am trying to make a motor turn on for a period of time for example 30 seconds when a button is pressed. Then I want the motor to turn do nothing until the button is pressed again. Once it is pressed I want the motor to reverse itself for 30 seconds and stop.

Now tell me you still don’t “get” millis.

Backlight_Delayed_TurnOFF.ino (2.49 KB)

raschemmel,

Thank you so much for that explanation the way you broke it down made a lot of sense. I will definately incorporate that into my program now that I understand the use of millis.

Thank you!

Did you look at the program I attached ?