-1

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.

Nico Dioso
  • 195
  • 2
  • 15

1 Answers1

0

In short: You never reset your vars x, xx, xxx back to 0. Also you use both "Sets" of variables for both kinds of Motion. So the digitalRead(ls) < digitalRead(rs) sets xx = 1 and digitalRead(ls) > digitalRead(rs) Also sets xx = 1.

The first thing to do would be to name the "first" (everything after //RIGHT MOTION to //LEFT MOTION) x, xx, xxx to something like: rightMotionStep1 rightMotionStep2 and rightMotionStep3 and the later x, xx, xxx to: leftMotionStep1 leftMotionStep2 and leftMotionStep3.

of course you also have to declare these Vars.

The next thing is that you have to reset all of the vars so after your

Serial.println("Right motion");

there should be

rightMotionStep1 = 0;
rightMotionStep2 = 0;
rightMotionStep3 = 0;

same thing for left motion.

reineckm
  • 56
  • 5
  • ohh yes, I missed that! the x, xx, xxx are stages, and yes I forgot to reset them. Alright thanks! I'll see if it would work fine now. – Nico Dioso Nov 13 '15 at 11:54
  • alright sir, I've eradicated that error I told. now, it seems to be unable to detect any motion for some time. but all direction readings are correct now. you have any idea why this is happening – Nico Dioso Nov 13 '15 at 12:07
  • As I have observed sir, it just requires a certain speed of swipe, in order to properly detect.. – Nico Dioso Nov 13 '15 at 15:57
  • I think there is no real need for the Loops but even with them it should work most of the time. Please post your updated code but use the Automatic Formating function first (CTRL + T is the Shortcut). Also: consider upvoting my answer if it helped you. – reineckm Nov 16 '15 at 08:07