Hello
I am needing some help, I have been racking my brain on this for 2 weeks now. What I have basically if it was an LED I want the LED to be turned off when a proximity sensor detects an object, then after 2 seconds I want the LED to come back on while the prox is still sensing the object, so that it can clear the prox and start the code over for the next object to be picked up by the prox. I have changed my code so many times, and all I can come up with is the prox senses the object and turns off led but with object in front of prox i cant get nothing else to happen.
I was just useing led as an example, it is actually a motor that is turning an arm that is going in front of prox to stop motor at 12:00 position, but once arm is in front of the prox motor stops. I need something to turn on motor after a 2 second delay to clear prox and restart again. I want motor to stop for 2 seconds at 12:00 position then turn motor on till it reaches 12:00 again. I have learned a lot in this Venture but I am stuck and I need to get this project going. I am sure it’s quite easy but I am new to this. Below is where I have got on my code, I can get the prox to stop the motor, but that’s all it does until I move my hand out of the way.
int proxPin = 2; // signal pin
int proxVal; //reading from proxPin
int motorPin = 12; // pin controlling motor
unsigned long lastTimeProxTriggered = 0; //the last time the prox was triggered
unsigned long interval = 2000; // how long the motor will delay
int motorPinState = HIGH;
void setup(){
//configure pin2 as an input and enable the internal pull-up resistor
pinMode(proxPin, INPUT_PULLUP);
pinMode(motorPin, OUTPUT);
}
void loop(){
unsigned long currentMillis = millis();
proxVal = digitalRead (proxPin);
digitalRead (motorPin);
// The Logic is inverted a low on pin 2 means a sinking switch is activated
// and a high on pin 2 means the switch is unactivated and pulled up by the internal resistor
// this is not a problem since the controller can interpret the data any way we tell it to
if (proxVal == HIGH){
digitalWrite (motorPin, HIGH);
}
if (proxVal == LOW ){
digitalWrite (motorPin, LOW);
}
else if (proxVal == LOW && currentMillis-lastTimeProxTriggered >= interval ){
lastTimeProxTriggered = currentMillis;
digitalWrite (motorPin, HIGH);
}
}