I am trying to use face detection(by processing) and distance sensor, that means if someone face's detected, and the distance is close enough, the motors will move and make the pen drawing. And when there is no one face detect(let's assume that people finish watching the machine, and he/she walk away), the motors stop. The motor shield that I am using is this(http://www.ladyada.net/make/mshield/index.html). And my stepper motors are unipolar.
The distance sensor and the face detection parts I have already done, the problem is I don't know how to control the stepper motors stop and start and stop and start, looping. Now my coding can just let the motor run, but it won't stop until I cut off the power supply.
Here is my code, please help me:(
The main problem is in the void loop() part.
Code: Select all
#include <AFMotor.h>
int incoming;
unsigned int EchoPin = 2;
unsigned int TrigPin = 13;
unsigned long Time_Echo_us = 0;
unsigned long Len_mm = 0;
AF_Stepper motor(400, 1);
void setup(){
Serial.begin(9600);
pinMode(EchoPin, INPUT);
pinMode(TrigPin, OUTPUT);
motor.setSpeed(10); // 10 rpm
}
void loop(){
digitalWrite(TrigPin, HIGH);
delayMicroseconds(50);
digitalWrite(TrigPin, LOW);
Time_Echo_us = pulseIn(EchoPin, HIGH);
if((Time_Echo_us < 60000) && (Time_Echo_us > 1))
{
Len_mm = (Time_Echo_us * 34/100)/2;
}
if (Serial.available()>0){
incoming = Serial.read();
//here is the main problem
//incoming mean the value that processing passed to arduino,
//and 'H' mean someone face has been detected,
//and the Len_mm is mean the distance sensor value.
//I can do the motor.step here, but I just don't know how to make it stop...
if(incoming == 'H' && Len_mm < 400){
motor.step(4000, FORWARD, DOUBLE);
}
if(incoming == 'L'){
//suppose here is let the motor stop,
//but I don't know what should I type in
}
}
}