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!
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
the quick way :-) change declaration to:
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.
you actual touch switch remembers state. don't need remember previous state in code.
please if have misunderstood.
regards
ray
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
Post a Comment