Ignore sensor/button input signals during execution of project.

I am working on a project for the blackboard cleaning system. I am using Limit Switches to stop and start direction motors using relays.

I am facing an issue with limit switches. I want to read switch value only once to trigger my condition but switches send continuous signals during contact. serial monitors write the same output repeatedly till the switch is HIGH.

Also, I want to ignore any key press, sensor inputs during the system going back to the docking system.

I want to read only one pulse from button and ignore input whenever I want please suggest me how to do this.

I am very new to this Arduino environment please help me out.

Attaching code, sample simulation board attached with push buttons instead of limit switches and LEDs instead of relays.
and concept diagram.

blackboard_revised_UNO.ino (2.45 KB)

Sensors, switches and buttons do not SEND data to you. You only get information from them when your code specifically reads them with a digitalRead() or analogRead().

To ignore something simply do not read it! You may need to do something like set a flag that tells you you've read it so you don't read it again.

Steve

akashchavan03:
I am facing an issue with limit switches. I want to read switch value only once to trigger my condition but switches send continuous signals during contact.

See IDE->file/examples/digital/statechangedetection

I am kinda new into all this can you please guide me how to stop reading digital pin after using it once.

in my code, I don't want to read Switch1 and Switch2 input during execution of Switch3 and Switch4 conditions.
During returning to home position when MCU reads switch1 or switch2 input it starts my other unwanted relays at that time.

when switch 3 triggered I don't want any other switches reading except final switch 4

please help me out I have to complete it on time.

akashchavan03:
I want to ignore any key press, sensor inputs during the system going back to the docking system.

I want to read only one pulse from button and ignore input whenever I want please suggest me how to do this.

The only way I ever do that kind of thing, is to use a state machine as described for example here or here.

In such a system, in any state you explicitly only read the inputs you want, thus implicitly ignoring the ones you don't. If you for instance (and I haven't opened any of your attachments yet so these are hypotheticals) are in say state_idle, you would read say pin x letting loop() take you through that many many times doing nothing but reading pin x and taking no action it if it's say high. Then if pin x is read as low, that would take you to state_run_forward and in there, you would not read pin x, thus ignoring it even if it's bouncing up and down like a yoyo, and only in this new state, read maybe pin y.

Here's OP's pix:

And here's his or her code:

int set = 0;

int i = 0;


const int Master_Switch = 2;
const int switch1 = 3;
const int switch2 = 4;
const int switch3 = 5;
const int switch4 = 6;

const int relay1 = 8;
const int relay2 = 9;
const int relay3 = 10;
const int relay4 = 11;
const int relay5 = 12;
const int relay6 = 13;


void setup()
{
  Serial.begin (9600);
  pinMode(switch1, INPUT);
  pinMode(switch2, INPUT);
  pinMode(switch3, INPUT);
  pinMode(switch4, INPUT);
  pinMode(Master_Switch, INPUT);
  pinMode(relay1,OUTPUT);
  pinMode(relay2,OUTPUT);
  pinMode(relay3,OUTPUT);
  pinMode(relay4,OUTPUT);
  pinMode(relay5,OUTPUT);
  pinMode(relay6,OUTPUT);
}

void loop()
{

// Powering on system using Master Switch
  
  set = digitalRead(Master_Switch);    // 2 pin read
  if (set == HIGH) {
    digitalWrite(relay1, HIGH);     // 8 pin high
    Serial.println("Moving: UP");
    digitalWrite(relay5, HIGH);   // 12 pin high
    Serial.println("Duster Spin On");
    } 

    
// Switch 1

    set = digitalRead(switch1);
    if (set == HIGH) {
    digitalWrite(relay1, LOW);
    Serial.println("UP Stop");
    delay (200);
    digitalWrite(relay3, HIGH);
    Serial.println("Moving: Right");
    delay (4000); // delay 2 sec
    digitalWrite(relay3, LOW);
    Serial.println("Right Stop");
    delay (200);
    digitalWrite(relay2, HIGH);
    Serial.println("Moving: Down");
  }


// Switch2

    set = digitalRead(switch2);
    if (set == HIGH) {
    digitalWrite(relay2, LOW);
    Serial.println("Down: Stop");
    delay (200);
    digitalWrite(relay3, HIGH);
    Serial.println("Moving: Right");
    delay (4000); // delay 2 sec
    digitalWrite(relay3, LOW);
    Serial.println("Right Stop");
    delay (200);
    digitalWrite(relay1, HIGH);
    Serial.println("Moving: Up");
    }

// Switch3
    set = digitalRead(switch3);
    if (set == HIGH) {
    digitalWrite(relay1, LOW);
    Serial.println("UP: Stop");
    digitalWrite(relay2, LOW);
    Serial.println("Down: Stop");
    delay (200);
    digitalWrite(relay4, HIGH);
    Serial.println("Returning to Base Position");
    }
    

// Switch4
    set = digitalRead(switch4);
    if (set == HIGH) {
    digitalWrite(relay4,LOW);
    Serial.println("Reached Base Position");
    delay (200);
    digitalWrite(relay5, LOW);
    Serial.println("DUSTER OFF");
    delay (700);
    Serial.println("Cycle Completed : System Off");
    }
    
    return;

    }

So in your case, and this is very much off the top of my head, ymmv, you could have states such as st_idle (the start state), st_going_up, st_moving_right, st_going_down and st_return.

Look at switch...case to see how to separate out all the stuff you have in ifs at the moment.

In st_idle when Arduino fires up, monitor only the master switch, and if it's pressed, set the state to st_going_up, turn on the relay to go up and spin the duster. In that state, monitor only switch1 which (I think :wink: ) says it's at the top. If it triggers, turn off the up relay (and btw, it will help a huge amount if you give these things meaningful human-friendly names...) turn on the right move relay, capture the time that move started, and set the state to st_moving_right.

In there, monitor millis() to see if whatever time it takes to move to the next zone has elapsed, and if not, do nothing. (The relay is still on so it's moving.) If the time has elapsed, turn that relay off, turn on the move down relay, and set the state to st_going_down...

Etc etc, you should see the idea.

You will find this a lot easier if you map the whole thing out in a state diagram as in the previous simple example, or look at this for a more complex one.

This random example I found on the web shows the principal of explicitly monitoring certain inputs and ignoring others.

Legitimate exits from Idle are the insertion of a card (goes to Active), and some kind of service code, perhaps a hardware key turning a switch (goes to Out of Service). But you can't go from Active to Out of Service: the program code in the Active state, will just simply not be looking at the switch on the lock to go Out of Service. But in Active it will be looking at the Cancel button all the time, to abort if the user hits the button before the transaction declares itself Done.

If you invest some time now in drawing such a diagram for your project, the programming will pretty much fall out of the diagram.

fishboneDiagram:
So in your case, and this is very much off the top of my head, ymmv, you could have states such as st_idle (the start state), st_going_up, st_moving_right, st_going_down and st_return.

Look at switch...case to see how to separate out all the stuff you have in ifs at the moment.

In st_idle when Arduino fires up, monitor only the master switch, and if it's pressed, set the state to st_going_up, turn on the relay to go up and spin the duster. In that state, monitor only switch1 which (I think :wink: ) says it's at the top. If it triggers, turn off the up relay (and btw, it will help a huge amount if you give these things meaningful human-friendly names...) turn on the right move relay, capture the time that move started, and set the state to st_moving_right.

In there, monitor millis() to see if whatever time it takes to move to the next zone has elapsed, and if not, do nothing. (The relay is still on so it's moving.) If the time has elapsed, turn that relay off, turn on the move down relay, and set the state to st_going_down...

Etc etc, you should see the idea.

You will find this a lot easier if you map the whole thing out in a state diagram as in the previous simple example, or look at this for a more complex one.

Thanks for reply and sharing all the information.

but can you be more specific what should i do. What should i add to my code and where. I am learning this stuff now. but cant understand all this stuff can you guide me whats wrong in my code . It will be great help cause i have to complete this project today only.

but can you be more specific what should i do.

To be more specific I'd have to draw your full state diagram out and write the full code I think: I find it's difficult in these projects to find a middle ground between "here's my thinking" and "here's your code".

akashchavan03:
i have to complete this project today only.

Alas I'm at work right now, middle of the day here, and I can't spend any more time on that than I did already.

@akashchavan03, IMHO you are going about this all wrong.

If this was my project I would read all the switch values at the start of loop() and save them. Then the other decisions in that iteration of loop would use the saved values.

You also need to keep track of what your machine is doing (what state it is in) - whether idle, going up, down, right or left - because that will influence which switch is relevant. You could manage that with a char variable - let's call it currentAction which could have any of the values 'I' (capital eye), 'U', 'D', 'R' or 'L'

Then you could have code something like this pseudo code

if (currentAction == 'D') {
   if (botttomLimitSwitchState == LOW) { // assumes low when triggered
       currentAction = 'R';
   }
   else {
      // code to move motor down
   }
}

and similar code for the other states.

I am assuming that the next move when it gets to the bottom is to move to the right. If I am wrong I presume you can figure out the revision that would be needed

...R