hi, i m building automatic railway gate control as a project, i use 2 ir sensor and 1 stepper and arduino. now i want to sense train coming by one sensor and stepper rotate clock wise, and train pass through 2nd sensor stepper rotate anticlockwise. now i did my programming bt the problem is it is working if train is rotating in left side direction. what i want is no matter which side my train rotate when it is sense by any 1 sensor stepper rotate clockwise, and when exit is sense by other sensor stepper rotate anti clockwise.i dont have any guidance who can tell me where is my fault. so i found this way i may get some help so guys/seniors pls help me. here is my programming:
#include <Stepper.h>
const int stepsPerRevolution = 550;
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
void setup() {
pinMode(A0, INPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, INPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
digitalWrite(A2, HIGH);
digitalWrite(A1, LOW);
digitalWrite(A5,HIGH);
digitalWrite(A4,LOW);
// set the speed at 60 rpm:
myStepper.setSpeed(60);
Serial.begin(9600);
}
void loop() {
delay(50);
Serial.println(analogRead(A0));
if (analogRead(A0) < 500){
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(1000);
}
delay(50);
Serial.println(analogRead(A3));
if(analogRead(A3) < 500){
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(1000);
}
}
when it is sense by any 1 sensor stepper rotate clockwise, and when exit is sense by other sensor
If what you say is true then you can rotate the stepper clockwise whichever sensor is triggered first then rotate it anticlockwise when the other sensor is triggered.
If so, then the system will be in one of two states, either waiting for the first sensor to be triggered or waiting for the second one to be triggered. Create a boolean variable, let's name it waitingForSecondSense, and set it to false.
When you sense the train with either sensor and waitingForSecondSense is false, move the stepper clockwise and set waitingForSecondSense to true.
When you sense the train with either sensor and waitingForSecondSense is true, move the stepper anticlockwiese and set waitingForSecondSense to false.
okk i will do it.. if its work then i will inform u. and thank u so much for noticing me.. u have no idea how much it really matter. thank u.
#include <Stepper.h>
const int stepsPerRevolution = 550;
boolean state = 0;
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
void setup() {
pinMode(A0, INPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, INPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
digitalWrite(A2, HIGH);
digitalWrite(A1, LOW);
digitalWrite(A5,HIGH);
digitalWrite(A4,LOW);
// set the speed at 60 rpm:
myStepper.setSpeed(60);
Serial.begin(9600);
}
void loop() {
delay(50);
Serial.println(analogRead(A0));
Serial.println(analogRead(A3));
if (((analogRead(A0) < 500))||((analogRead(A3) < 500)) && (state = 0))
{
state =! state;
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
}
if (((analogRead(A0) < 500))||((analogRead(A3) < 500)) && (state = 1))
{
state =! state;
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}
}
not working what did i wrong in this? help mee!!!!
if (((analogRead(A0) < 500)) || ((analogRead(A3) < 500)) && (state = 0))
Look at the last condition ? Are you testing the value of state ?
no i m not try to test the value i m trying to make condition like u said it boolean state is 0 then it will rotate clockwise, change the value state = 1 , then condition then rotate anti clockwise.
sensor 1 sensor 2 state rotate
0 1 0 clock
1 0 0 clock
0 1 1 anti - clock
1 0 1 anti - clock
this logic is what i mean.... can u help me?
I will put the question in a simpler way.
What does
state = 0
do ?
what u mean is boolean state = 0 change it to only state = 0 ?
state = 0;
sets the state variable to zero, which is equivalent to false.
What I am querying is your use of that as part of an if statement. Did you mean to use
state == 0;[/code
oho.. now i understand, yes u r right i mean the state signal is equivalent to 0
. okk i will try again ur code.. lets see what happend
hmm.. it is not working with boolean condition.. i dont know what did i made bt it suppose to work correctly.
#include <Stepper.h>
const int stepsPerRevolution = 550;
boolean state = 0;
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
void setup() {
pinMode(A0, INPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, INPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
digitalWrite(A2, HIGH);
digitalWrite(A1, LOW);
digitalWrite(A5,HIGH);
digitalWrite(A4,LOW);
// set the speed at 60 rpm:
myStepper.setSpeed(60);
Serial.begin(9600);
}
void loop() {
delay(50);
Serial.println(analogRead(A0));
Serial.println(analogRead(A3));
if (((analogRead(A0) < 500))||((analogRead(A3) < 500)) && (state = 0))
{
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
state =! state;
delay(500);
}
if (((analogRead(A0) < 500))||((analogRead(A3) < 500)) && (state = 1))
{
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
state =! state;
delay(500);
}
}
if (((analogRead(A0) < 500))||((analogRead(A3) < 500)) && (state = 0))
if (((analogRead(A0) < 500))||((analogRead(A3) < 500)) && (state = 1))
You did not fix these two lines. You need to use == for comparison and not = which is assignment.
if (((analogRead(A0) < 500))||((analogRead(A3) < 500)) && (state == 0))
if (((analogRead(A0) < 500))||((analogRead(A3) < 500)) && (state == 1))
thx for ur reply.. i will try this and get back to u if it is work or not...
i change the state = o to state == 0, and state == 1. its still not working ..
its still not working ..
What is the sketch not doing that you want it to do? What is the sketch doing that you do not want it to do?
What are our serial print statements indicating?
found it... finally .. thx guys for boolean suggestion and thank u for state == 0, state == 1 suggestion. its working guy..
sagardutta:
found it... finally .. thx guys for boolean suggestion and thank u for state == 0, state == 1 suggestion. its working guy..
If you are going to use a boolean then it would make sense to use true and false as its values and to give the variable a sensible name to make the code more readable.