Hello. I'm new to Arduino in the past week. I'm really enjoying using it, but I've got a bit stuck on one of the bits of code (where you use push buttons to make an LED fade up and down).
In the 'Making it better' section down the page, it suggests this bit of code...
int value = 0;
void loop(){
if (digitalRead(inputPin1) == LOW) { value–; }
else if (digitalRead(inputPin2) == LOW) { value++; }
value = constrain(value, 0, 255);
analogWrite(ledPin, value);
delay(10);
}
My question is, where do I actually enter this code?
FYI, this is the original code...
/*
Button
Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 7.
The circuit:
LED attached from pin 13 to ground
pushbutton attached to pin 2 from +5V
10K resistor attached to pin 2 from ground
Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 2005
by DojoDave
modified 17 Jun 2009
by Tom Igoe http://www.arduino.cc/en/Tutorial/Button
*/
// constants won’t change. They’re used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
sketch_dec19a:30: error: stray '' in program
sketch_dec19a.cpp: In function 'void loop()':
sketch_dec19a:30: error: 'inputPin1' was not declared in this scope
sketch_dec19a:30: error: expected `;' before 'u2013'
sketch_dec19a:31: error: 'inputPin2' was not declared in this scope
This is the code...
*/
// constants won’t change. They’re used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 9; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
int value = 0;
void loop(){
if (digitalRead(inputPin1) == LOW) { value–; }
else if (digitalRead(inputPin2) == LOW) { value++; }
value = constrain(value, 0, 255);
analogWrite(ledPin, value);
delay(10);
}
So, where did you define inputPin1 and inputPin2? They need to be defined and given values that correspond to the pins that connected the switches to. The need to be set as INPUT, too.
if (digitalRead(inputPin1) == LOW) { value–; }
else if (digitalRead(inputPin2) == LOW) { value++; }
Also, go back and do the on button off button example; it's a necessary precursor to the fade - there is some pin setup there that you need (That PaulS mentioned earlier)
OK. I did the on button off button push just fine, but I'm still trying to get a fade (following the instructions here http://www.oomlout.com/a/products/ardx/circ-07) - can you tell me where I'm going wrong now?!...
int ledPin = 9; // choose the pin for the LED
int inputPin1 = 3; // button 1
int inputPin2 = 2; // button 2
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin1, INPUT); // make button 1 an input
pinMode(inputPin2, INPUT); // make button 2 an input
}
int value = 0;
void loop(){
if (digitalRead(inputPin1) == LOW) { value–-; }
else if (digitalRead(inputPin2) == LOW) { value++; }
value = constrain(value, 0, 255);
analogWrite(ledPin, value);
delay(10);
}
It's still telling me...
sketch_dec19b:12: error: stray '' in program
sketch_dec19b.cpp: In function 'void loop()':
sketch_dec19b:12: error: expected `;' before 'u2013'
I've copied/pasted the code with no problems with the compiling and uploading, but the red LED lights up automatically. Then button 1 turns the LED off only while I'm holding the button. After that, the light is back on. Button 2 doesn't do anything. I pulled everything off and started all over again, but I still have the same problem. Here's the code:
/*
Button
Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 7.
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 2005
by DojoDave
modified 17 Jun 2009
by Tom Igoe
http://www.arduino.cc/en/Tutorial/Button
*/
// constants won’t change. They’re used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Also, with the "Experimenter's Guide...," the card shows there's a wire from the LED positive "to pin 13 then pin 9" but there's nothing in the code for pin 9. Not sure about that as all of the pictures/videos, etc do not have anything with pin 9. It also shows to put the wire from Button 2 (Off) to the 10Kohm to Pin 3, but there's nothing about Pin 3 in the code.
What's wrong?
Thanks.
Moderator edit: </mark> <mark>[code]</mark> <mark>
Also, with the "Experimenter's Guide...," the card shows
You are making an assumption that we are all familiar with this guide that you don't even show the complete name of. Bad assumption.
What's wrong?
You are assuming that we can see what you have wired to what. You are assuming that we can see what you are pressing. You are assuming that we can see what is happening. Three strikes; you're out.
If you are willing to address any of those erroneous assumptions, we're willing to help.
Figures. The instructables site has to have the most crap per kilobyte of published information of any site on the web.
First, the wiring of the switches is way more complicated than it needs to be. Wire one leg to ground. Wire the other to a digital pin. Turn on the internal pullup resistor. No external resistors needed.
Second, there is no debug code. How do you know that the switches are being read correctly?
Wire the switches, with no LEDs, etc. On each pass through loop, read a switch state, and print a message ("1 pressed" or "1 released"). Then, repeat for the other switch.
When you can reliably see the switches working, then wires in the LED(s) and resistor(s).
LOL...it's the same instructions from the ARDX book, and almost every other website.
ok, for the first, are you saying to go from each button (negative) to ground?
for the second, is there a "manual" for debug? There's nothing like that with the "Exptmnt Guide for Arduino."
I'm way out of the loop...I learned code with the Tandy comp, Apple 1 and Apple 2, and FORTRAN in college over 20 years ago...haven't done anything else. Trying to get back to that because, someday, I'll rule the world (muuhhaaaa). Thanks for the help. I'll try that "experiment" again this week/weekend. I did circ 8, and 9, with no problems, so maybe the code is bad or the buttons are bad.
1- pulled everything out, then used different wires. Same problem
2. different buttons, same problem
3. different LED, same problem
4. pulled off the resisters--it was a different problem.
-- both buttons worked (before, the second button didn't do anything)
-- however, the LED would turn off automatically, off and on.
-- when I turn them off or on, it would do what it was told...for a few seconds, then went back to automatically turning off the LED.
5. put the resisters back on, and it went back the same problem.
if you look at the code, toward the top, it reads "connected to digital pin 13 when pressing a push button attached to pin 7"; but there's nothing else with pin 7. No "int inputPin = 7" or anything--and there's nothing on the schematic either.