Toggle switch has rapid HIGH/LOW output in ON position


i'm using 3 prong toggle switch wired through 1kohm resistor suggested in this forum thread read toggle switches. had working fine yesterday, today, after doing experimenting tone() function, , reverting original code, toggle switch behaves in following manner (tested first connected lcd screen , through serial out) : in "off" position, works expected, returning "0" output. in should "on" position, should returning "1", returns "0" , "1" in uneven intervals when have printing constant stream serial, example, should return "111111111...." in on position, returns "000100010001100001". i'm curious why happening now, have reverted both code , wiring functional state. i'm including code, input pin switch digital pin 7, other lead switch wired directly ground.
code: [select]
// * lcd rs pin digital pin 12
// * lcd enable pin digital pin 11
// * lcd d4 pin digital pin 5
// * lcd d5 pin digital pin 4
// * lcd d6 pin digital pin 3
// * lcd d7 pin digital pin 2
// * lcd r/w pin ground
// * lcd vss pin ground
// * lcd vcc pin 5v
// * 10k resistor:
// * ends +5v , ground
// * pot lcd vo pin (pin 3)
// *toggle switch on digital pin 7
// *analog joystick on analog pins 0,1, , 4.
#include <liquidcrystal.h>

liquidcrystal lcd(12, 11, 5, 4, 3, 2);
int xpotpin = 0;
int ypotpin = 1;
int bpotpin = 4;
int spin = 7;
int xval = 0;
int yval = 0;
int bval = 0;
int switchstate = 0;

void setup() {
  lcd.begin(16, 2);
  pinmode(spin, input);
  serial.begin(9600);
  delay(1000);
 
}

void loop() {


xval = analogread(xpotpin);
yval = analogread(ypotpin);
bval = analogread(bpotpin);
switchstate = digitalread(spin);
lcd.clear();
lcd.setcursor(0,0);
lcd.print("x:");
lcd.print(xval);
lcd.setcursor(7,0);
lcd.print("y:");
lcd.print(yval);
lcd.setcursor(11,1);
lcd.print(switchstate);


if (bval<50){
  lcd.setcursor(0,1);
  lcd.print("button on");
} else { lcd.setcursor(0,1);
         lcd.print("button off");}         

delay(100);
}

use pinmode(spin, input_pullup);
or
put 10k resistor pin 7 5 volts.


Arduino Forum > Using Arduino > General Electronics > Toggle switch has rapid HIGH/LOW output in ON position


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code