how to interupt a input?

Hi i have one input that depends on another, like when you push button 1 (input D3) and button 2 (input D2) is low, output (D10) is turned high, but if button 2 goes high, output have to go low, but like my sketch the output just standing high... how can i change that?

my comlete (unfinished) code:

/* 
 car lifter
 Made by Håvard Gjøsund

thanks to the arduino forum*/

const int pot1 = 0;
const int pot2 = 1;
const int brEND = 2;
const int brOPP = 3;
const int brNED = 4;
const int brLAS = 5;
const int brLYS = 6;
const int ledAC = 7;
const int ledDC = 13;
const int ledLYS = 8;
const int solLAS = 9;
const int solOPP = 10;
const int solNED = 11;
int brStateOPP = 0;
int brStateNED = 0;
int brStateLAS = 0;
int brStateEND = 0;

void setup() {
  
    
   
  pinMode(brEND, INPUT); 
  pinMode(brOPP, INPUT);
  pinMode(brNED, INPUT);
  pinMode(brLAS, INPUT);
  pinMode(brLYS, INPUT);
  pinMode(ledAC, OUTPUT);
  pinMode(ledDC, OUTPUT);
  pinMode(ledLYS, OUTPUT);
  pinMode(solLAS, OUTPUT);
  pinMode(solOPP, OUTPUT);
  pinMode(solNED, OUTPUT);


}

void loop (){

  

  // read the end switch
  brStateEND = digitalRead(brEND);
  
  //lift
  brStateOPP = digitalRead(brOPP);
  if (brStateOPP == HIGH)
  {
    if (brStateEND != HIGH)
    {
      digitalWrite(solOPP, HIGH);
    }
  } 
  else
  {
    digitalWrite(solOPP, LOW);
  }

  //lower
  brStateNED = digitalRead(brNED);
  if (brStateNED == HIGH)
  {
        digitalWrite(solNED, HIGH);
  }
    else
  {
    digitalWrite(solNED, LOW);
  }

//retract the lock
  brStateLAS = digitalRead(brLAS);
  if (brStateLAS == HIGH)
  {
        digitalWrite(solLAS, HIGH);
  }
    else
  {
    digitalWrite(solLAS, LOW);
  }



}

frustrated :-?

We can’t see where brStateEnd got a value, of the relationship between D2, D3, and D10 and brUp and solUP.

Can we see all of your code?

edited so all the code are there =)

A few Serial.print statements, to verify that the switches are all working would not be amiss.

Debouncing the switches would be a good idea, too.

Not to be picky, but uniform indenting is good, too.

have another version of the code with serial.print statements and it shows clearly that the switches are working.

if you mean with uniform identing, better marking of the different inputs/outputs then i have originaly written this code in norwegian. if you can wait until i come back from work i can edit the code a bit...

Hi Håvard

I think the best way for you is to write down the different logical conditions like this: input1,input2,input3,,output1,putput2 and the corrosponding logic: 1,0,0,,1,0,0 ?,?,?,,?,?,?

It would probaly be much better to work with switch case instead of if-then. The problem with if-then is that non specified conditions keeps the old output values. It can easealy get "langhåret".

I had an old sketch with logics and switch case and reading your code this was remade into this. Feel free to use or discharge ;-)

// Carlift
// in1 = brOPP
// in2 = brNED
// in3 = brEND
// in4 = brLAS
// out1 = solOPP
// out2 = solNED
// out3 = solLAS
// Logic:
// in1,in2,in3,in4,,out1,out2,out3
// 1,0,0,0,,1,0,0
// 0,1,0,0,,0,0,1
// 0,0,0,1,,0,0,1
// "anything else",,0,0,0

const int in1 = 3;
const int in2 = 4;
const int in3 = 2;
const int in4 = 5;
const int out1 = 10;
const int out2 = 11;
const int out3 = 9;

byte caseStep = 0;

void setup() {
  pinMode(in1, INPUT); digitalWrite(in1,HIGH); //Enable pull-up
  pinMode(in2, INPUT); digitalWrite(in2,HIGH); //Enable pull-up
  pinMode(in3, INPUT); digitalWrite(in3,HIGH); //Enable pull-up
  pinMode(in4, INPUT); digitalWrite(in4,HIGH); //Enable pull-up
  pinMode(out1, OUTPUT);
  pinMode(out2, OUTPUT);
  pinMode(out3, OUTPUT);
}

void loop(){
  caseStep = 0; // set caseStep to default value
  
  // Logical conditions:
  if ((digitalRead(in1) == LOW) and (digitalRead(in2) == HIGH) and (digitalRead(in3) == HIGH) and (digitalRead(in4) == HIGH)) caseStep = 1;
  if ((digitalRead(in1) == HIGH) and (digitalRead(in2) == LOW) and (digitalRead(in3) == HIGH) and (digitalRead(in4) == HIGH)) caseStep = 2;
  if ((digitalRead(in1) == HIGH) and (digitalRead(in2) == HIGH) and (digitalRead(in3) == HIGH) and (digitalRead(in4) == LOW)) caseStep = 3;


  // Different outputs:
  switch (caseStep) {
    case 0: //default settings
      digitalWrite(out1, LOW);
      digitalWrite(out2, LOW);
      digitalWrite(out3, LOW);
      break;
      
    case 1:
      digitalWrite(out1, HIGH);
      digitalWrite(out2, LOW);
      digitalWrite(out3, LOW);
      break;
      
    case 2:
      digitalWrite(out1, LOW);
      digitalWrite(out2, HIGH);
      digitalWrite(out3, LOW);
      break;
      
    case 3: 
      digitalWrite(out1, LOW);
      digitalWrite(out2, LOW);
      digitalWrite(out3, HIGH);
      break;
  }
}

It compiles but is not testet live.

-Fletcher

Thanks a lot I can't imagine how much work you have saved me =)

I did use your code, but modified it so it would suit the other functions also. (emergency down)

the next problem now is how i can program a soft switch (?) like an impulse relay. i have one button that is going to trigger a little 5v relay, but i can't seem to get a program that holds the relay... the only thing happening is that the relay is on only when i push the button... any idea?

here is my code so far...

/*
 Carlift

 Made by Håvard Gjøsund / Fletcer Chr

I will give a HUGE thanks to Fletcher Chr 
 on the Arduino: Forum, for the great help on this sketch
*/

const int in1 = 3;    //switch up
const int in2 = 4;    //switch down
const int in3 = 2;    //end switch
const int in4 = 5;    //safety lock switch
const int in5 = 6;    //light switch 
const int out1 = 10;  //solenoid up
const int out2 = 11;  //solenoid down
const int out3 = 9;   //solenoid - retract safety lock
const int out4 = 8;   //lights on the switch-panel

int ledOn;
boolean LedOn = false;

byte caseStep = 0;
byte caseStep2 = 0;

void setup() {
  pinMode(in1, INPUT); digitalWrite(in1,HIGH); //Enable pull-up
  pinMode(in2, INPUT); digitalWrite(in2,HIGH); //Enable pull-up
  pinMode(in3, INPUT); digitalWrite(in3,HIGH); //Enable pull-up
  pinMode(in4, INPUT); digitalWrite(in4,HIGH); //Enable pull-up
  pinMode(in5, INPUT); digitalWrite(in5,HIGH); //Enable pull-up
  pinMode(out1, OUTPUT);
  pinMode(out2, OUTPUT);
  pinMode(out3, OUTPUT);
  pinMode(out4, OUTPUT);
 }

 void loop(){
  caseStep = 0; // set caseStep to default value
  
  // Logical conditions:
  // probably much needless code here =S
  
  if ((digitalRead(in1) == LOW)  and (digitalRead(in2) == HIGH) and (digitalRead(in3) == HIGH) and (digitalRead(in4) == HIGH)) caseStep = 1;  // up/increase
  if ((digitalRead(in1) == HIGH) and (digitalRead(in2) == LOW)  and (digitalRead(in3) == HIGH) and (digitalRead(in4) == HIGH)) caseStep = 2;  // down/decrease
  if ((digitalRead(in1) == HIGH) and (digitalRead(in2) == HIGH) and (digitalRead(in3) == HIGH) and (digitalRead(in4) == LOW))  caseStep = 3;  // open lock
  if ((digitalRead(in1) == HIGH) and (digitalRead(in2) == LOW)  and (digitalRead(in3) == LOW)  and (digitalRead(in4) == HIGH)) caseStep = 4;  // emergency down
  if ((digitalRead(in1) == HIGH) and (digitalRead(in2) == LOW)  and (digitalRead(in3) == HIGH) and (digitalRead(in4) == LOW))  caseStep = 5;  // down with lock open
  if ((digitalRead(in1) == HIGH) and (digitalRead(in2) == LOW)  and (digitalRead(in3) == LOW)  and (digitalRead(in4) == LOW))  caseStep = 5;  // emergency down with lock open
  if ((digitalRead(in1) == HIGH) and (digitalRead(in2) == HIGH) and (digitalRead(in3) == LOW)  and (digitalRead(in4) == LOW))  caseStep = 3;  // emergency open lock


  // Different outputs:
  switch (caseStep) {
    case 0: //default settings
      digitalWrite(out1, LOW);
      digitalWrite(out2, LOW);
      digitalWrite(out3, LOW);
      break;

    case 1:
      digitalWrite(out1, HIGH);//solenoid up
      digitalWrite(out2, LOW);
      digitalWrite(out3, LOW);
      break;

    case 2:
      digitalWrite(out1, LOW);
      digitalWrite(out2, HIGH);//solenoid down
      digitalWrite(out3, LOW);
      break;

    case 3:
      digitalWrite(out1, LOW);
      digitalWrite(out2, LOW);
      digitalWrite(out3, HIGH);//solenoid safety lock
      break;

    case 4:
        digitalWrite(out1, LOW);
      digitalWrite(out2, HIGH);//solenoid down
      digitalWrite(out3, LOW);
      break;
    
    case 5:
        digitalWrite(out1, LOW);
      digitalWrite(out2, HIGH);//solenoid down
      digitalWrite(out3, HIGH);//solenoid safety lock
      break;
  }
 /* 
 ** lights on the switch-panel  
 */
   if(digitalRead(in5) == HIGH)
   {
      ledOn = !ledOn;
   }

   if(ledOn)
   {
      digitalWrite(out4, HIGH);
   }
   else
   {
      digitalWrite(out4, LOW);
   }
}

offtopic i don't know where you have learned norwegian, but "langhåret" means long haired... :P which maybe not was what you meant...

troublesome == tullete

  // probably much needless code here =S
  
  if ((digitalRead(in1) == LOW)  and (digitalRead(in2) == HIGH) and (digitalRead(in3) == HIGH) and (digitalRead(in4) == HIGH)) caseStep = 1;  // up/increase
  if ((digitalRead(in1) == HIGH) and (digitalRead(in2) == LOW)  and (digitalRead(in3) == HIGH) and (digitalRead(in4) == HIGH)) caseStep = 2;  // down/decrease
  if ((digitalRead(in1) == HIGH) and (digitalRead(in2) == HIGH) and (digitalRead(in3) == HIGH) and (digitalRead(in4) == LOW))  caseStep = 3;  // open lock
  if ((digitalRead(in1) == HIGH) and (digitalRead(in2) == LOW)  and (digitalRead(in3) == LOW)  and (digitalRead(in4) == HIGH)) caseStep = 4;  // emergency down
  if ((digitalRead(in1) == HIGH) and (digitalRead(in2) == LOW)  and (digitalRead(in3) == HIGH) and (digitalRead(in4) == LOW))  caseStep = 5;  // down with lock open
  if ((digitalRead(in1) == HIGH) and (digitalRead(in2) == LOW)  and (digitalRead(in3) == LOW)  and (digitalRead(in4) == LOW))  caseStep = 5;  // emergency down with lock open
  if ((digitalRead(in1) == HIGH) and (digitalRead(in2) == HIGH) and (digitalRead(in3) == LOW)  and (digitalRead(in4) == LOW))  caseStep = 3;  // emergency open lock

Lots of (slow) code, there. Each pin is read 7 times, when it only needs to be read once.

int val1 = digitalRead(in1);
int val2 = digitalRead(in2);
int val3 = digitalRead(in3);
int val4 = digitalRead(in4);
if (val1 == LOW  && val2 == HIGH && val3 == HIGH && val4 == HIGH) caseStep = 1;

the next problem now is how i can program a soft switch (?) like an impulse relay. i have one button that is going to trigger a little 5v relay, but i can't seem to get a program that holds the relay... the only thing happening is that the relay is on only when i push the button... any idea?

Treat the relay like the LED:

   if(digitalRead(in5) == HIGH)
   {
      ledOn = !ledOn;
   }

   if(ledOn)
   {
      digitalWrite(out4, HIGH);
   }
   else
   {
      digitalWrite(out4, LOW);
   }

Activate the relay with one button push. Deactivate it with the next push of the same button.

By the way, you might want to think about debouncing the buttons (using a short delay after reading the button, so you don't read it again too soon. Or look at the button libraries where this feature is incorporated.

Hi again

I just happy to contribute :-) I did mean long haired ... Danish is my native language.

Please be carefull with this part:

int ledOn;
boolean LedOn = false;

Have a look at this for you button problem: http://arduino.cc/en/Tutorial/Debounce http://arduino.cc/en/Tutorial/ButtonStateChange

PoulS: You are absolut right about the speed issue.

-Fletcher

thanks for all your help :)

my program is now completed :D

/*
 Carlift

 Made by Håvard Gjøsund / Fletcer Chr
 I will give a HUGE thanks to Fletcher Chr and PaulS
 on the Arduino: Forum, for the great help on this sketch
*/

const int in1 = 3;    //switch up
const int in2 = 4;    //switch down
const int in3 = 2;    //end switch
const int in4 = 5;    //safety lock switch
const int in5 = 6;    //light switch 
const int out1 = 10;  //solenoid up
const int out2 = 11;  //solenoid down
const int out3 = 9;   //solenoid - retract safety lock
const int out4 = 8;   //lights on the switch-panel


int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;

int ledOn;
boolean LedOn = false;

byte caseStep = 0;
byte caseStep2 = 0;

void setup() {
  pinMode(in1, INPUT); digitalWrite(in1,HIGH); //Enable pull-up
  pinMode(in2, INPUT); digitalWrite(in2,HIGH); //Enable pull-up
  pinMode(in3, INPUT); digitalWrite(in3,HIGH); //Enable pull-up
  pinMode(in4, INPUT); digitalWrite(in4,HIGH); //Enable pull-up
  pinMode(in5, INPUT); digitalWrite(in5,HIGH); //Enable pull-up
  pinMode(out1, OUTPUT);
  pinMode(out2, OUTPUT);
  pinMode(out3, OUTPUT);
  pinMode(out4, OUTPUT);
 }

 void loop(){
  caseStep = 0; // set caseStep to default value
  
  
  val1 = digitalRead(in1);
  val2 = digitalRead(in2);
  val3 = digitalRead(in3);
  val4 = digitalRead(in4);
  
  // Logical conditions:
  // probably much needless code here =S
  
  if ((val1 == LOW)  and (val2 == HIGH) and (val3 == HIGH) and (val4 == HIGH)) caseStep = 1;  // up/increase
  if ((val1 == HIGH) and (val2 == LOW)  and (val3 == HIGH) and (val4 == HIGH)) caseStep = 2;  // down/decrease
  if ((val1 == HIGH) and (val2 == HIGH) and (val3 == HIGH) and (val4 == LOW))  caseStep = 3;  // open lock
  if ((val1 == HIGH) and (val2 == LOW)  and (val3 == LOW)  and (val4 == HIGH)) caseStep = 4;  // emergency down
  if ((val1 == HIGH) and (val2 == LOW)  and (val3 == HIGH) and (val4 == LOW))  caseStep = 5;  // down with lock open
  if ((val1 == HIGH) and (val2 == LOW)  and (val3 == LOW)  and (val4 == LOW))  caseStep = 5;  // emergency down with lock open
  if ((val1 == HIGH) and (val2 == HIGH) and (val3 == LOW)  and (val4 == LOW))  caseStep = 3;  // emergency open lock


  // Different outputs:
  switch (caseStep) {
    case 0: //default settings
      digitalWrite(out1, LOW);
      digitalWrite(out2, LOW);
      digitalWrite(out3, LOW);
      break;

    case 1:
      digitalWrite(out1, HIGH);//solenoid up
      digitalWrite(out2, LOW);
      digitalWrite(out3, LOW);
      break;

    case 2:
      digitalWrite(out1, LOW);
      digitalWrite(out2, HIGH);//solenoid down
      digitalWrite(out3, LOW);
      break;

    case 3:
      digitalWrite(out1, LOW);
      digitalWrite(out2, LOW);
      digitalWrite(out3, HIGH);//solenoid safety lock
      break;

    case 4:
        digitalWrite(out1, LOW);
      digitalWrite(out2, HIGH);//solenoid down
      digitalWrite(out3, LOW);
      break;
    
    case 5:
        digitalWrite(out1, LOW);
      digitalWrite(out2, HIGH);//solenoid down
      digitalWrite(out3, HIGH);//solenoid safety lock
      break;
  }
 /* 
 ** lights on the switch-panel  
 */
   if(digitalRead(in5) == LOW)
   {
      ledOn = !ledOn;
        delay (400);
   }

   if(ledOn)
   {
      digitalWrite(out4, HIGH);
   }
   else
   {
      digitalWrite(out4, LOW);
   }
}

@Fletcher: aahh... i haven't heard about the therm "langhåret" as troblesome... but thanks for the help anyway :) you did save me for a lot of time :D

@PaulS: thanks for the speed tweak ;) and letting me know about the debouncing part....