I'm having a hard time getting this simple switch to turn on an LED (don't judge me!!). Here's the code I'm running (from the Arduino website, only the pin values have been altered. I also added a println for reading):
int inPin = 10; // the number of the input pin
int outPin = 4; // the number of the output pin
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void setup()
{
Serial.begin(9600);
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
}
void loop()
{
reading = digitalRead(inPin);
Serial.println(reading);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(outPin, state);
previous = reading;
}
And here is the super weird output:
.
.
.
0
0
0
0
0
0
0+êRj¤j¤j¤j¤j¤j¤j¤j¤j¤...
Do you have a current limiting resistor on the LED?
Do you have a pull-up or pull-down resistor on the switch? If you don't, you can use the builtin pullup resistor.
Since, you write data every time through loop(), at 9600 baud I think you are stuffing bytes into the serial buffer faster than they can be transmitted.
Serial.print used to pause until the data was transmitted, but the new asynchronous implementation does not.
I don't know how the newer asynchronous implementation handles overflow, but it might be you get the garbage when the buffer is too full.
Try increasing the baud rate and/or adding a little delay in the loop.
Your coyness, sir, is very fetching, but perhaps not entirely helpful in enlightening me on the source of my errors
Are you saying that in the posted code, execution time of loop() will never be so fast that characters will be deposited in the serial buffer at a rate greater they would be transmitted at 9600 baud?
By some curious oversight of the honours system, Her Majesty has not deemed it necessary to bestow a knighthood on me.
The honorific is completely, though inexplicably, superfluous.