Stair Climber, Programming and sensor wiring Qestion

Hi,
First time using Arduino Uno
I'm making a stair climber for the school project ( it's just a 4-wheel car climbs the stairs) and having problems about programming and sensor wiring

There is a photo eye sensor that sense the stair front of the vehicle
when sensor activated, count 1 (counter become odd number) and motor move forward to the top floor.
Once it reaches top, there is a wall trigger the sensor. So count 1 again ( counter become even number) then motor move reverse direction.

I used 4 DC motor and it worked with other program. but couldn't check the reverse
H-bridge is L298n multiwatt [u]https://www.sparkfun.com/datasheets/Robotics/L298_H_Bridge.pdf[/u]

My sensor is Omron E3S-LS3RC4
[u]https://www.fa.omron.com.cn/data_pdf/closed/cat/e3s-ls3c1d_-ls3rc4_ds_e_3_1_csm1307.pdf?id=1166[/u]

I connect like
blue-0V
Brown, Pink - 12V
Black - Arduino pin 13

Motor is not even starting with the sensor...
Can you tell me if my program or other thing is wrong?
save me... Thank you

//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 = 1;
  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
  boolean state = LOW;
  int Val = 0;
  int Counter = 0;

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

  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);
  Serial.begin(9600);
}


void loop() {
  state = digitalRead(Sensor);
  if (state = HIGH){
  Counter++;
  Serial.println(Counter);     
  }
  //Descend if counter is odd
  if ( (Counter % 2) == 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);
   delay(400);   
 }

else {
  // Climb when sensor activates and even
   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);
   delay(400);

For a start you need == here not =:

if (state = HIGH){

But then I think since you read the sensor all the time (or at least at the top of loop() which is pretty much continuous) your counter will just race away and you'll toggle from odd to even and back all the time. But check the serial print after you fix the ==.

Hi,
Welcome to the Forum.

Before you added the sensor did you have control of your motors?

Tom... :slight_smile:

ardy_guy:
For a start you need == here not =:

if (state = HIGH){

But then I think since you read the sensor all the time (or at least at the top of loop() which is pretty much continuous) your counter will just race away and you'll toggle from odd to even and back all the time. But check the serial print after you fix the ==.

Thanks for the reply
I changed it and still does not work

I just want to see the motor move... lol
so I changed my program (only loop part) like this but still not working..
And
I just found that I cannot use serial communication since I'm using pin 0and pin 1 as output for the motor

void loop() {
  int PushButtonState = digitalRead(13);
   
 if (PushButtonState == HIGH) {
  current1 = 1;
 }
 else {
  current1 = 0;
 }
 
  // Climb untill reach turninterval time set
  if (current1 == 1); {
   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);
 }
}

TomGeorge:
Hi,
Welcome to the Forum.

Before you added the sensor did you have control of your motors?

Tom... :slight_smile:

Thank you Tom
It's same program but used push button
Motor moved only bushbutton is pressed, did not go other direction when button is not pressed (motors just stopped).
I want the motor move even there is no signal on push botton

this is the program that I used to test the motor
is there any wrong?

//set pin number
  int PushButton = 13;
  int pwm1 = 5;
  int pwm2 = 6;
  int pwm3 = 9;
  int pwm4 = 10;
  
  const int i1 = 0;
  const int i2 = 1;
  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
  int PushButtonState = 0;

void setup() {
//Set pin I/O
  pinMode(PushButton, 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 PushButtonState = digitalRead(13);
   
 if (PushButtonState == HIGH) {
  // Climb untill reach turninterval time set
   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);
 }
   //Descend
else
{
   analogWrite(pwm1, 255);
   analogWrite(pwm2, 255);
   analogWrite(pwm3, 255);
   analogWrite(pwm4, 255);
   
   digitalWrite(i1,LOW);
   digitalWrite(i2,HIGH);
   
   digitalWrite(i3,LOW);
   digitalWrite(i4,HIGH);
   
   digitalWrite(i5,LOW);
   digitalWrite(i6,HIGH);

   digitalWrite(i7,LOW);
   digitalWrite(i8,HIGH);   
 }

Hi,

I just found that I cannot use serial communication since I'm using pin 0 and pin 1 as output for the motor

Do not use pins 0 or 1 at all, edit your code to use two other pins.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

This is wiring drawing

Have to use pin 0,1
don't have extra pin..

Project Wiring.pdf (31.6 KB)

Hi
You can use the Analog pins A0 to A5 as digital?

If you are driving ALL the motors exactly the same directions and speeds, you can halve the Arduino outputs needed by putting the inputs of the driver board in parallel.

Like wire enA and enB together and use PWM6 on one driver and do the same for PWM10 on the other, that way you have saved 2 pins.

Do the same with the I5 and I6.

So you will have saved 4pins in all.

Tom.... :slight_smile:
PS. if you attach as jpg, it will load into the post properly.

Like this way?

Hi,

Yes and the same for the the IN2, IN4, IN6 and IN8. to a single Arduino output pin.

Tom... :slight_smile:

Thank you so much Tom

Great Idea!

and one more question

Motors are only moving when pin 13 (sensor pin) is getting signal
if I take out pin 13, motor stops.
How can I keep the motor motion even if I lose pin 13 signal???
any idea??

Thank you!