So i got this thing going, where i am trying to turn on a motor for x amount of time CW then CCW when i get an input from a sensor. The code seems to work, but i happens way to fast and i cant get my head around on why. When that is done, i'm gonna try to figure out a way to block it from happening again for around 120 sec
leds without current limiting resistors is always a bad start
I know that {} come cheap by the dozen... but it's not a reason to have them everywhere... but you need them (and not a semicolon) where it's needed like
what are you requirements (what should the code do)?
I want a motor (thats on a separate circuit, it only needs high input and it will turn the motor CW or CCW on two separate wires) to turn when it gets a signal from the sensor. It need to run CW for amount of tim then CCW for x amount of time when. But all of that needs to hapen x amount of time after the first triger from the sensor.
I was considering to use a statment to count triger from the sensor and when i have two triggers to set of the millis things.
so you want to detect a trigger on the sensor and wait a bit and then do the CW and CCW movement regardless of the status of the sensor?
the code coud be
loop
await sensor trigger
wait a bit
move CW for some time
move CCW for some time
no need for millis()
if you want to use millis, then you need to maintain the state (a state machine approach) - are you waiting for the trigger or in the CW or CCW part of the movement?
Change the code, but on my serial monitor the IR sensor counts up as long as some thing is in front. So if i pass my finger in front of it, it will count of to about 50 or so. It doesn't see each pass as one signal. So I'm not sure if i should change sensor or do something with the code. Any suggestions?
#define outoftheBox 2
int motorCw = 3; //Pushes Plunger down
int motorCcw = 4; //Pulls Plunger back
int sensorPin = 6; //IR sensor, Activated = LOW
void setup() {
pinMode (sensorPin, INPUT);
pinMode (motorCw, OUTPUT);
pinMode (motorCcw, OUTPUT);
Serial.begin(9200);
}
void loop(){
int catjumpsCount = 0; //Counts how many times the cat passed the IR sensor
int catJumps = false; // if true the cat jumped in and out of the box
if (digitalRead(sensorPin) == LOW && catJumps == false){
catjumpsCount =+ 1;
Serial.println(catjumpsCount);
if (catjumpsCount >= outoftheBox){
delay(3000);
digitalWrite(motorCw, HIGH);
delay(400);
digitalWrite(motorCw, LOW);
digitalWrite(motorCcw, HIGH);
delay(450);
digitalWrite(motorCcw, LOW);
catJumps = true;
if (catJumps = true){
catjumpsCount = 0;
catJumps = false;
}
}
}
}
In this case no harm done because no current will flow through the LEDs anyway! The breadboard ground rails are not connected. Same goes for the 5V rail, so the sensor will not work and the input pin will float.
I read more about how this specific sensor worked, so i fixed it and i leaned a lot of things about coding as well, so thank you:)!
As for the Az delivery IR sensor you just need to use the enable function on one of the pins and do a delay of 210ms because of the clock speed on it.
And if ant one wondering in the future what this is. Is one of those scented sprays with this 3 setting on how frequent it will spray. I by passed the circuit and and use this code to detect when the cat had been in the cat toilet and then spray.
I might implement a counter in the future to tell me when the spray is low so i can replace it.
Here is the final code
#define outoftheBox 2
int motorCw = 3; //Pushes Plunger down
int motorCcw = 4; //Pulls Plunger back
int sensorRead = 6; //IR sensor, Activated = LOW
int sensorEnable = 5;
void setup() {
pinMode (sensorRead, INPUT);
pinMode (sensorEnable, OUTPUT);
pinMode (motorCw, OUTPUT);
pinMode (motorCcw, OUTPUT);
}
void loop(){
int catjumpsCount = 0; //Counts how many times the cat passed the IR sensor
int catJumps = false; // if true the cat jumped in and out of the box
digitalWrite (sensorEnable, HIGH);
delay(210);
if (digitalRead(sensorRead) == LOW && catJumps == false){
catjumpsCount ++;
if (catjumpsCount == outoftheBox){
catJumps = true;
delay(2000);
digitalWrite(motorCw, HIGH);
delay(800);
digitalWrite(motorCw, LOW);
digitalWrite(motorCcw, HIGH);
delay(600);
digitalWrite(motorCcw, LOW);
if (catJumps = true){
catjumpsCount = 0;
catJumps = false;
digitalWrite (sensorEnable, LOW);
}
}
}
}