Use Switch to change Variable


i have touchswitch works toggle switch. has high & low state , remembers in state is(independent arduino). based on when state of switch changing, want toggle variable 1 0 or 0 1, depending on it's current value.

this current code, makes program starts @ 0. achieve should able put variable touchvalue somewhere in code 1, , next time touch switch, variable should go 0. or otherway around of course.

i hope it's clear way.

thanks in advance!

code: [select]
// 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

quote
what achieve should able put variable touchvalue somewhere in code 1, ...


the quick way :-)  change declaration to:

code: [select]
int touchvalue = 1;

but guess not meant. 

do want touchvalue same touchpin? if digitalread(touchpin) starts @ 1, touchvalue starts @ 1, , when touch switch goes 0, touchvalue follows, , on?

if so, loop can 1 line, plus need variable after updated.

code: [select]
touchvalue = digitalread(touchpin);

you actual touch switch remembers state.  don't need remember previous state in code.

please if have misunderstood.

regards

ray


Arduino Forum > Using Arduino > Sensors > Use Switch to change Variable


arduino

Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support