I'm making this project that includes sensors. I have two sensors, one at left and right. I have succeeded detecting left and right motions, but when I just spam "sense" a sensor, let's say left sensor, it reads as i have swiped left. Can you tell me what's the problem?
to understand my code I have drawn the "required" sequence to say that I have swiped. (0 detected, 1 nothing detected)
For left 1. L=1, R=1 2. L=0, R=1 3. L=0, R=0 4. L=1, R=0
For right 1. L=1, R=1 2. L=1, R=0 3. L=0, R=0 4. L=0, R=1
Here's my code
int rs=7,ls=6,r,l,x,xx,xxx,z; //rs right sensor, ls left sensor
void setup() {
Serial.begin(9600);
pinMode(rs, INPUT);
pinMode(ls, INPUT);
pinMode(13,OUTPUT);
}
void loop() {
digitalWrite(12,LOW);
digitalWrite(13,LOW);
r= digitalRead(rs);
l= digitalRead(ls);
if(r==1&&l==1)
x=1;
else x=0;
//RIGHT MOTION
if(x==1){
for(z=1;z<10000;z++){
if(digitalRead(ls)<digitalRead(rs)){
z=10000;xx=1;}}
if(xx=1){
for(z=1;z<10000;z++){
if(digitalRead(ls)==0&&digitalRead(rs)==0){
z=10000;xxx=1;}}}
if(xxx==1){
for(z=1;z<10000;z++){
if(digitalRead(ls)>digitalRead(rs)){
z=10000;Serial.println("Right motion");digitalWrite(12,HIGH);}}}}
//LEFT MOTION
if(x==1){
for(z=1;z<10000;z++){
if(digitalRead(ls)>digitalRead(rs)){
z=10000;xx=1;}}
if(xx=1){
for(z=1;z<10000;z++){
if(digitalRead(ls)==0&&digitalRead(rs)==0){
z=10000;xxx=1;}}}
if(xxx==1){
for(z=1;z<10000;z++){
if(digitalRead(ls)<digitalRead(rs)){
z=10000;Serial.println("Left motion");digitalWrite(13,HIGH);}}}}
}
I set my loop 10000 times because 1 whole loop is so fast to detect a motion.
Thanks in advance.