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