I'm new to Arduino, and am looking for a solution to only run my "Else" command one time...
I have an IR Sensor wired up with a stepper motor, and want the program to make the stepper motor make two revolutions when the sensor is reading HIGH.
The problem I run into is that as the sensor is reading high, the motor does it's 2 revolutions, and then continues back through the loop again and will continue to rotate until the sensor is switched to LOW.
What I want is for when the sensor reads HIGH, the motor does 2 revolutions, and will not rotate again until the sensor switches to LOW and then back to HIGH again.
Another way to explain it, is that I want the motor to rotate twice when I pass my hand in front of the sensor, and then to sit there until I pass my hand in front of it again. If I hold my hand in front of it, I still want ONLY two rotations.
Here's my code. I tried playing with some If statements to now avail, and currently have a Boolean command that also isn't working.
// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int sensor = 2;
boolean hasrun = false;
void setup() {
Serial.begin(9600);
pinMode(sensor,INPUT);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}
void loop() {
if(digitalRead(sensor)==LOW) {
Serial.println("No Product Detected");
}
else
{
Serial.println("Motor Activated");
if(hasrun == false) {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
for(int x = 0; x < 400; x++) // 200 pulses = one full cycle rotation
{ digitalWrite(stepPin,HIGH);
delayMicroseconds(700);
digitalWrite(stepPin,LOW);
delayMicroseconds(700);
boolean (hasrun == true);
Serial.println("1True");}}
else
{
(hasrun == true);
Serial.println("True");}
}
}
Thanks for any help in advance