DC Motor stair climb car (would be the last..hope)

I tried my best to solve this by myself and posted few questions.
I really appreciate all of your help. really really nice people
However, when I combine all the information and replies.
It’s not working… I’m kinda lost.

So this would be my last post!

Here is my project.

It’s just a car that climbs the stairs

My goal:

Motor moves forward as soon as I power up the Arduino.
then climbs, reach top floor and inductive sensor triggered, then descend to the bottom.

I used :

four DC motor (12V)
two H-bridge L298N multiwatt 15
one inductive sensor (12v-24v)
12V power supply for the motors
6V power supply for L298N
1 relay for the sensor (since sensor gives 8v, I used it to lower down to 5V. it’s working well)
everything works well by itself.

This is the code I tried.
Motor is not moving at all loool
and also, I tried with other code, and motor moves only during the sensor is triggered.
couldn’t make the motor start right after I turn on the arduino.

//set pin number
  int Sensor = 13;
  int pwm1 = 5;
  int pwm2 = 6;
  int pwm3 = 9;
  int pwm4 = 10;
  
  const int i1 = 0;
  const int i2 = 4;
  const int i3 = 2;
  const int i4 = 3;
  const int i5 = 7;
  const int i6 = 8;
  const int i7 = 11;
  const int i8 = 12;
  
  // Variables
  unsigned long lastDebounce = 0;
  unsigned long debounce = 600;
  int sensorState;
  int x;
  int sensor = LOW;
  int previousState = LOW;  // previous state of sens

void setup() {
//Set pin I/O
  pinMode(Sensor, INPUT);

  pinMode(pwm1, OUTPUT);
  pinMode(pwm2, OUTPUT);
  pinMode(pwm3, OUTPUT);
  pinMode(pwm4, OUTPUT);
  pinMode(i1, OUTPUT);
  pinMode(i2, OUTPUT);
  pinMode(i3, OUTPUT);
  pinMode(i4, OUTPUT);
  pinMode(i5, OUTPUT);
  pinMode(i6, OUTPUT);
  pinMode(i7, OUTPUT);
  pinMode(i8, OUTPUT);
}


void loop() {
  
  int state = digitalRead(Sensor);
  
  if (state != previousState) {
  lastDebounce = millis();
}
  if ((millis() - lastDebounce) > debounce) {
    if (state != sensorState) {
      sensorState = state;  // prevent debouning of sensor trigger using timer
        if (sensorState == HIGH) {
          x=1;
        }
    }
  }

   if ((millis() - lastDebounce) > debounce) {
    if (state != sensorState) {
      sensorState = state;  //prevent debouncing 
        if (sensorState == LOW) {
          x=0;
}
    }
   }

   // if sensor is LOW, Forward
    if (x == 0); {
  analogWrite(pwm1, 255);
  analogWrite(pwm2, 255);
  analogWrite(pwm3, 255);
  analogWrite(pwm4, 255);
      
  digitalWrite(i1,HIGH);
  digitalWrite(i3,HIGH);
  digitalWrite(i5,HIGH); 
  digitalWrite(i7,HIGH);
  digitalWrite(i2,LOW);
  digitalWrite(i4,LOW);
  digitalWrite(i6,LOW);
  digitalWrite(i8,LOW); 
          }  


          // if sensor is HIGH(reached top floor), Reverse 
           if (x == 1); {
  analogWrite(pwm1, 255);
  analogWrite(pwm2, 255);
  analogWrite(pwm3, 255);
  analogWrite(pwm4, 255);
      
  digitalWrite(i2,HIGH);
  digitalWrite(i4,HIGH);
  digitalWrite(i6,HIGH); 
  digitalWrite(i8,HIGH);
  digitalWrite(i1,LOW);
  digitalWrite(i3,LOW);
  digitalWrite(i5,LOW);
  digitalWrite(i7,LOW);
          }
}

Project wiring.jpg

Anyone?? Please...
I thought Arduino is cool... Should've used PLC...

jaebungs:
Anyone?? Please...
I thought Arduino is cool... Should've used PLC...

Lololol and you think a PLC would be any easier???

I'd love to help, but you need to add some serious comments in your code. The logic seems rather strange. Also, how exactly are you using the inductive sensor?

As a first improvement give your pins meaningful names.

What's the PWM output for? Your code outputs a constant HIGH level, no PWM at all.

Hi,
Can you post some pictures of your stair climber so we can see your setup, especially the sensors.
Can you DRAW a circuit diagram, showing your relay, power supply, how you have the Arduino connected with label.
This;
Project wiring.jpg
Looks all over the place, draw the circuit with positive at the top of the page and gnd at the bottom, everything in between, clearly layed out.

Why is Vss, your logic supply to the L298N 7.2V???

Thanks … Tom… :slight_smile:

Power_Broker:
Lololol and you think a PLC would be any easier???

I'd love to help, but you need to add some serious comments in your code. The logic seems rather strange. Also, how exactly are you using the inductive sensor?

I know.... I'm just crying haha
But PLC is more familiar to me

DrDiettrich:
As a first improvement give your pins meaningful names.

What's the PWM output for? Your code outputs a constant HIGH level, no PWM at all.

ohh.. I thought I need PWM to control the motor speed.

I'll change to pin name that has some meaning
sorry about it

TomGeorge:
Hi,
Can you post some pictures of your stair climber so we can see your setup, especially the sensors.
Can you DRAW a circuit diagram, showing your relay, power supply, how you have the Arduino connected with label.
This;
action=dlattach;topic=467574.0;attach=206812[/img]
Looks all over the place, draw the circuit with positive at the top of the page and gnd at the bottom, everything in between, clearly layed out.

Why is Vss, your logic supply to the L298N 7.2V???

Thanks … Tom… :slight_smile:

sorry Tom
L298N Vss is 6V

this is the full drawing


project wiring final.jpg

also I labelled more easy to understand (I don’t know I did properly though…)

//set pin number
  int Sensor = 13;

  int EnM1 = 5;
  int EnM2 = 6;
  int EnM3 = 9;
  int EnM4 = 10;
  
  const int motor1A= 0;
  const int motor1B = 4;
  const int motor2A = 2;
  const int motor2B = 3;
  const int motor3A = 7;
  const int motor3B = 8;
  const int motor4A = 11;
  const int motor4B = 12;
  
  // Variables
  unsigned long lastDebounce = 0;
  unsigned long debounce = 600;
  int sensorState;
  int x;
  int sensor = LOW;
  int previousState = LOW;  // previous state of sens

void setup() {
//Set pin I/O
  pinMode(Sensor, INPUT_PULLUP);

  //enable the motors
  pinMode(EnM1, OUTPUT);
  pinMode(EnM2, OUTPUT);
  pinMode(EnM3, OUTPUT);
  pinMode(EnM4, OUTPUT); 

  pinMode(motor1A, OUTPUT);
  pinMode(motor1B, OUTPUT);
  pinMode(motor2A, OUTPUT);
  pinMode(motor2B, OUTPUT);
  pinMode(motor3A, OUTPUT);
  pinMode(motor3B, OUTPUT);
  pinMode(motor4A, OUTPUT);
  pinMode(motor4B, OUTPUT);
}


void loop() {
  
  int state = digitalRead(Sensor);
  
  if (state != previousState) {
  lastDebounce = millis();
}
  if ((millis() - lastDebounce) > debounce) {
    if (state != sensorState) {
      sensorState = state;  // prevent debouning of sensor trigger using time
      
        if (sensorState == HIGH) {
          x=1;
        }
    }
  }

   if ((millis() - lastDebounce) > debounce) {
    if (state != sensorState) {
      sensorState = state;  //prevent debouncing 
      
        if (sensorState == LOW) {
          x=0;
}
    }
   }

   // sensor is not triggered yet. keep move forward(climb stairs).
   // so... start right after the arduino powered up
    if (x == 0); {

  analogWrite(EnM1, 255);
  analogWrite(EnM2, 255);
  analogWrite(EnM3, 255);
  analogWrite(EnM4, 255);
  
  digitalWrite(motor1A,HIGH);
  digitalWrite(motor2A,HIGH);
  digitalWrite(motor3A,HIGH); 
  digitalWrite(motor4A,HIGH);
  digitalWrite(motor1B,LOW);
  digitalWrite(motor2B,LOW);
  digitalWrite(motor3B,LOW);
  digitalWrite(motor4B,LOW); 
          }  


          // there is a piece of metal that triggers sensor
          // sesnor triggered, change motor direction so desend the stairs
           if (x == 1); {
            
  analogWrite(EnM1, 200);
  analogWrite(EnM2, 200);
  analogWrite(EnM3, 200);
  analogWrite(EnM4, 200);
            
  digitalWrite(motor1B,HIGH);
  digitalWrite(motor2B,HIGH);
  digitalWrite(motor3B,HIGH); 
  digitalWrite(motor4B,HIGH);
  digitalWrite(motor1A,LOW);
  digitalWrite(motor2A,LOW);
  digitalWrite(motor3A,LOW);
  digitalWrite(motor4A,LOW); 
          }
}

It's so hard...
I tried with this new code
and it's not changing the direction and also motor moves only during the sensor is HIGH.
How to m motor constantly moving?

//set pin number
  int Sensor = 13;
  
  int EnM1 = 5;
  int EnM2 = 6;
  int EnM3 = 9;
  int EnM4 = 10;
  
  const int motor1A= 0;
  const int motor1B = 4;
  const int motor2A = 2;
  const int motor2B = 3;
  const int motor3A = 7;
  const int motor3B = 8;
  const int motor4A = 11;
  const int motor4B = 12;
  // Variables

  boolean direct;
  boolean forward1;

  
void setup() {
//Set pin I/O
  pinMode(Sensor, INPUT_PULLUP);
  
  pinMode(EnM1, OUTPUT);
  pinMode(EnM2, OUTPUT);
  pinMode(EnM3, OUTPUT);
  pinMode(EnM4, OUTPUT); 

  pinMode(motor1A, OUTPUT);
  pinMode(motor1B, OUTPUT);
  pinMode(motor2A, OUTPUT);
  pinMode(motor2B, OUTPUT);
  pinMode(motor3A, OUTPUT);
  pinMode(motor3B, OUTPUT);
  pinMode(motor4A, OUTPUT);
  pinMode(motor4B, OUTPUT);
}


void loop() {
  
  boolean direct= digitalRead(Sensor);
  forward(direct); 
  }
 
 
void forward(boolean forward1) 
{
  analogWrite(EnM1, 255);
  analogWrite(EnM2, 255);
  analogWrite(EnM3, 255);
  analogWrite(EnM4, 255);
  
  digitalWrite(motor1A,direct);
  digitalWrite(motor2A,direct);
  digitalWrite(motor3A,direct); 
  digitalWrite(motor4A,direct);
  digitalWrite(motor1B,!direct);
  digitalWrite(motor2B,!direct);
  digitalWrite(motor3B,!direct);
  digitalWrite(motor4B,!direct); 
          }

The variable "direct" is of type boolean, yet you use it to store the digital input values of either HIGH or LOW? I'm not saying that this technique is not valid, it just doesn't make any sense to me. Why not have two cases in the form of an if-else statement with the digital-read as the argument? Each case would cause the motors to behave differently. Google "finite state machine."

Might not be necessary, but I wouldn't set the PWM pins as outputs. AnalogWrite() will automatically take care of that for you.

Lastly, I'd check the wiring and actual output of the sensor. Did you check BOTH?

Power_Broker:
The variable "direct" is of type boolean, yet you use it to store the digital input values of either HIGH or LOW? I'm not saying that this technique is not valid, it just doesn't make any sense to me. Why not have two cases in the form of an if-else statement with the digital-read as the argument? Each case would cause the motors to behave differently. Google "finite state machine."

Might not be necessary, but I wouldn't set the PWM pins as outputs. AnalogWrite() will automatically take care of that for you.

Lastly, I'd check the wiring and actual output of the sensor. Did you check BOTH?

Thank you! Power_Braker

I tried all the motor and sensor by it self. It works pretty well.
I think the program is the problem. I think....

and yeah I tried if...else statement, but didn't work.
so I just tried another concept which is using boolean.

I looked up Finite state machine and made this code.
However, motor is not moving at all...

void setup() {
//Set pin I/O
  pinMode(Sensor, INPUT_PULLUP);
  
  pinMode(EnM1, OUTPUT);
  pinMode(EnM2, OUTPUT);
  pinMode(EnM3, OUTPUT);
  pinMode(EnM4, OUTPUT); 

  pinMode(motor1A, OUTPUT);
  pinMode(motor1B, OUTPUT);
  pinMode(motor2A, OUTPUT);
  pinMode(motor2B, OUTPUT);
  pinMode(motor3A, OUTPUT);
  pinMode(motor3B, OUTPUT);
  pinMode(motor4A, OUTPUT);
  pinMode(motor4B, OUTPUT);
}


void loop() {
  
sensordetect = digitalRead(13);
  
if (sensordetect == HIGH) {
  casemode = ++casemode%2;
  switch(casemode){
    case 0: forward();
    case 1: reverse();
  }
}

  
}

void forward() 
{
  analogWrite(EnM1, 255);
  analogWrite(EnM2, 255);
  analogWrite(EnM3, 255);
  analogWrite(EnM4, 255);
  
  digitalWrite(motor1A,HIGH);
  digitalWrite(motor2A,HIGH);
  digitalWrite(motor3A,HIGH); 
  digitalWrite(motor4A,HIGH);
  digitalWrite(motor1B,LOW);
  digitalWrite(motor2B,LOW);
  digitalWrite(motor3B,LOW);
  digitalWrite(motor4B,LOW);
          }  

void reverse()  
{
  analogWrite(EnM1, 200);
  analogWrite(EnM2, 200);
  analogWrite(EnM3, 200);
  analogWrite(EnM4, 200);
            
  digitalWrite(motor1B,HIGH);
  digitalWrite(motor2B,HIGH);
  digitalWrite(motor3B,HIGH); 
  digitalWrite(motor4B,HIGH);
  digitalWrite(motor1A,LOW);
  digitalWrite(motor2A,LOW);
  digitalWrite(motor3A,LOW);
  digitalWrite(motor4A,LOW); 
}

If you had everything work by itself before, why don't you use just that working code for the stepper motors?

If you had everything work by itself before, why don't you use just that working code for the stepper motors?

Hi, DrDiettrich

I just made the code to check the motor and sensor is broken or not and how it works. The codes are not the thing I want.

I just want the motor keep moving. But motor only moves when I put some metal to the sensor. Motor stops right after I take out the metal piece. I just want the motor keep the motion.

struggling so hard...

Then extend the motor example with forward and backward movement. You can use a button to switch between both. If that works, replace the button code by the sensor code.

DrDiettrich:
Then extend the motor example with forward and backward movement. You can use a button to switch between both. If that works, replace the button code by the sensor code.

I’m working on it as you said DrDiettrich.

Questions!

If I make my void loop() like that
shouldn’t it move forward and reverse for 400 millisec??
I expected moving right after I give a power but not moving hmm…

//set pin number
  int Sensor = 13;
  
  int EnM1 = 5;
  int EnM2 = 6;
  int EnM3 = 9;
  int EnM4 = 10;
  
  const int motor1A= 0;
  const int motor1B = 4;
  const int motor2A = 2;
  const int motor2B = 3;
  const int motor3A = 7;
  const int motor3B = 8;
  const int motor4A = 11;
  const int motor4B = 12;
  // Variables

int sensordetect;

  
void setup() {
//Set pin I/O
  pinMode(Sensor, INPUT_PULLUP);
  
  pinMode(EnM1, OUTPUT);
  pinMode(EnM2, OUTPUT);
  pinMode(EnM3, OUTPUT);
  pinMode(EnM4, OUTPUT); 

  pinMode(motor1A, OUTPUT);
  pinMode(motor1B, OUTPUT);
  pinMode(motor2A, OUTPUT);
  pinMode(motor2B, OUTPUT);
  pinMode(motor3A, OUTPUT);
  pinMode(motor3B, OUTPUT);
  pinMode(motor4A, OUTPUT);
  pinMode(motor4B, OUTPUT);
}

// forward direction
void forward() 
{
  analogWrite(EnM1, 255);
  analogWrite(EnM2, 255);
  analogWrite(EnM3, 255);
  analogWrite(EnM4, 255);
  
  digitalWrite(motor1A,HIGH);
  digitalWrite(motor2A,HIGH);
  digitalWrite(motor3A,HIGH); 
  digitalWrite(motor4A,HIGH);
  digitalWrite(motor1B,LOW);
  digitalWrite(motor2B,LOW);
  digitalWrite(motor3B,LOW);
  digitalWrite(motor4B,LOW);
          }  

//reverse direction
void reverse()  
{
  analogWrite(EnM1, 255);
  analogWrite(EnM2, 255);
  analogWrite(EnM3, 255);
  analogWrite(EnM4, 255);
            
  digitalWrite(motor1B,HIGH);
  digitalWrite(motor2B,HIGH);
  digitalWrite(motor3B,HIGH); 
  digitalWrite(motor4B,HIGH);
  digitalWrite(motor1A,LOW);
  digitalWrite(motor2A,LOW);
  digitalWrite(motor3A,LOW);
  digitalWrite(motor4A,LOW); 
}


void loop() {
  forward();
  delay(400);
  reverse();
  delay(400);
  }

However, if I make my loop like this

  sensordetect = digitalRead(Sensor);
  if (sensordetect == HIGH) {
  forward();
  delay(400);
  reverse();
  delay(400);
}

works well

can anyone tell me why???

Will the motors really stop and reverse their direction within 400ms?

DrDiettrich:
Will the motors really stop and reverse their direction within 400ms?

I can see motors move forward and reverse in very short time, continuously.

Today, I found that motor is not getting enough voltage.
I’m supplying 12V to Vs but I’m only getting 3V across all the motor.

I don’t know why… (I assume) I didn’t use 100nF capacitor on Vs and Vss?? like link below??
https://www.ikalogic.com/4a-h-bridge-motor-driver-using-the-l298-ic-schematic/

I still suspect a missing Gnd wire.

If you still use the last posted circuit diagram, add a wire from Arduino Gnd to the L298 Gnd. Also connect the Arduino 5V to Vss, not 6V nor 12V.

Hi,
I must be blind, do not use pins 0 and 1 for control, they are the programming pins and can cause performance problems if used for another function.
You are using pin 0.

Use A0, the analog pins can be used as digital.

Also check Vss and Vs pins;

Vss is pin9 and is the logic 5V supply pin.
Vs is pin4 and is the Motor Power Supply pin, that is 12V.

Tom.... :slight_smile: