Use Switch to change Variable

I have a Touchswitch which basically works as an toggle switch. It has an High & Low state and remembers in which state it currently is(independent from the Arduino). Based on when the state of the switch is changing, I want to toggle a variable from 1 to 0 or 0 to 1, depending on it's current value.

This is my current code, which makes that my program always starts at 0. What I would like to achieve is that I should be able to put the variable touchValue somewhere in my code to 1, and the next time I touch the switch, the variable should go to 0. Or the otherway around of course.

I hope it's clear this way.

Thanks in advance!

// Touchsensor
int touchPin = 12;
int touchValue = 0;
int touchBegintState = 0;
int touchState = 1;
void setup(){
  
  // Serial Output
  Serial.begin(9600);  // output
// Touch
   pinMode(touchPin, INPUT);
   touchBegintState = digitalRead(touchPin);
}
void loop(){
  touchState = digitalRead(touchPin);
 
  if (touchState != touchBegintState){
     touchValue = 1;
    }
    else {
      touchValue = 0;
    }
    
 Serial.println(touchValue);
}

Hi ybrouwer

What I would like to achieve is that I should be able to put the variable touchValue somewhere in my code to 1, ...

The quick way is this :slight_smile: Change your declaration to:

int touchValue = 1;

But I guess this is not what you meant.

Do you want touchValue always to be the same as touchPin? So if digitalRead(touchPin) starts at 1, touchValue starts at 1, and when the touch switch goes to 0, touchValue follows, and so on?

If so, your loop can be just one line, plus what you need to do with the variable after it is updated.

touchValue = digitalRead(touchPin);

You say the actual touch switch remembers its state. So you don't need to remember its previous state in your code.

Please say if I have misunderstood.

Regards

Ray

Hi Ray,
thanks for your reply. Unfortunately that is not what I meant.

To explain it's use:
I use the touchswitch for different modes in my program. Those modes I select with a rotary encoder.
This sometimes means that my touchValue is set to 1 from it's use in a different mode in the program. When I then move to a different mode I would like to be able to reset the touchValue to 0. The next time I touch the switch it should start switching from the last set value of Touchvalue. In case of a 0 it will become 1 and in case of a 1 it will become a 0.
I hope this explains what I would like to achieve!

// Touchsensor
int touchPin = 12;
int touchValue = 0;

int touchBegintState = 0;
int lastTouchState = 0;
int touchState = 0;
int onStart = 0;
void setup(){
  
  // Serial Output
  Serial.begin(9600);  // output
// Touch
   pinMode(touchPin, INPUT);
   touchBegintState = digitalRead(touchPin);
}
void loop(){
  touchState = digitalRead(touchPin);
 if (onStart == 0){
  if (touchState != touchBegintState){
    if (touchValue == 0){
     touchValue = 1;
    }
    else {
      touchValue = 0;
    }
      onStart = 1;
    }
  
  }
  

   
  if (onStart == 1){
      if (touchState != lastTouchState) {
         if (touchValue == 0){
           touchValue = 1;
         }
         else{
            touchValue = 0;
         }
      }
  }
  
   Serial.println(touchValue);
   lastTouchState = touchState;
}

This is my new try, the only thing which isn't working is that the first time I have to press the switch, I have to press twice. Anyone knows how I can do this more conveniently? I have the feeling that I overlook a pretty simple solution.
Thanks in advance!

Can you post the complete program including the mode changing? What you want to do is simple, I think, but it needs to be linked to your code that changes mode.