Arduino Trick Switch Sketch Errors

Compiling the routine from the "Make Basic Aduino Projects book, I get these errors below. The routine is shown first. Please advise a NEW USER how to correct this. Using Arduino UNO:

ROUTINE: */ // 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 = 12; // the number of the LED pin // variables will change: int buttonStatus = 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 status of the pushbutton value: buttonStatus = digitalRead(buttonPin); // check if the pushbutton is pressed // if it is, the buttonEvent is HIGH: if (buttonStatus == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); }

Errors:

Arduino: 1.6.0 (Windows 7), Board: "Arduino Uno"

Build options changed, rebuilding all

sketch_Trick_switch.ino:1:2: error: expected unqualified-id before '/' token sketch_Trick_switch.ino:1:2: error: expected constructor, destructor, or type conversion before '/' token sketch_Trick_switch.ino: In function 'void setup()': sketch_Trick_switch.ino:12:9: error: 'buttonPin' was not declared in this scope sketch_Trick_switch.ino: In function 'void loop()': sketch_Trick_switch.ino:16:28: error: 'buttonPin' was not declared in this scope sketch_Trick_switch.ino:26:1: error: expected '}' at end of input Error compiling.

This report would have more information with "Show verbose output during compilation" enabled in File > Preferences.

You have opened a comment block /* but have not closed it with */

Weedpharma

...and the main loop isnt closed with a }

const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 12; // the number of the LED pin
// variables will change:
int buttonStatus = 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 status of the pushbutton value:
  buttonStatus = digitalRead(buttonPin);
  // check if the pushbutton is pressed
  // if it is, the buttonEvent is HIGH:
  if (buttonStatus == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }

}

compiling code

AMPS-N: ``` const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 12; // the number of the LED pin // variables will change: int buttonStatus = 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 status of the pushbutton value:   buttonStatus = digitalRead(buttonPin);   // check if the pushbutton is pressed   // if it is, the buttonEvent is HIGH:   if (buttonStatus == HIGH) {     // turn LED on:     digitalWrite(ledPin, HIGH);   }   else {     // turn LED off:     digitalWrite(ledPin, LOW);   }

}


compiling code

Thanks. I used the code verbatim from the book, which appears to be incorrect. No errors compiling, but nothing happens when I press the switch. I'll check schematic again, closely.

I am working in the same project, and when I push the button it the variable buttonStatus change to 1, but when I releas the button the variable has the same value, it doesn’t change to 0, so the led never turn off. The code is:

// 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 = 12; // the number of the LED pin
// variables will change:
int buttonStatus = 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 status of the pushbutton value:
buttonStatus = digitalRead(buttonPin);
// check if the pushbutton is pressed
// if it is, the buttonEvent is HIGH:
if (buttonStatus == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

and the diagram is the attached one.

You have reopened an old thread. Start your own.

|500x436

Carlicos,

I tried this project, and for me the led never turned off as well. I tried troubleshooting the problem, but I have practically no knowledge on capacitors, so the results are a mystery to me.

To troubleshoot, I connected the button also to an analog input (i.e. there are two wires after the button’s resistor: one going to digital input 2 (as in your diagram), and one to analog input 0). I then updated the code to plot the values of the analog input:

const int buttonPin = 2;
const int ledPin = 12;

int buttonStatus = 0;
void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);

  
}

void loop() {
  // put your main code here, to run repeatedly:
  buttonStatus = digitalRead(buttonPin);

  if (buttonStatus == HIGH) {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }

 Serial.println(analogRead(0));
  delay(1000);
}

I performed tests with two capacitors: 104pF and 22pF.
With 104pF, when pressing the button, the analog input shows 1023 (5V). When releasing the button, I can see the voltage drop slowly (the capacitor discharging), but even after a few minutes the digital input is still considered “on”. See attached “analog_curve_104pF.png” (each point is taken after 1s in this graph)

With 22pF the results were much different. After releasing the button, the voltage dropped very fast (much, much faster than the 104pF capacitor), and instead of keeping low, the voltage oscillates. See attached “analog_curve_2pF.png” (each point is taken after 100ms in this graph - 10x faster than the other curve)

I cannot understand what is happening here. How can the voltage in input2/analog0 get higher if there is nothing connected to 5V (i.e. when the button is not pressed)?

Hi,
Try this.

Do you have a DMM?
Tom... :slight_smile:

Tom,

Thank you very much for the reply. I tried your suggestion and it worked very well. I tried using different capacitors/resistors, and got some nice results: I tried with a 10uF capacitor and 10K resistor, but the led turned off almost immediately. My understanding is that the resistor limits the current from the capacitor to ground, and therefore the larger the resistance, the longer it should take to discharge the capacitor. I tried with larger resistors (1M and 2M), and indeed the led was on for much longer (7 and 15s respectively).

I think I understand what was happening before (the fluctuations in the analog pin input): when the button was off, it was not connected neither to 5v nor to ground. I thought "not connected" meant 0v, but after a little research I learned that this is not the case (the value on the input pin keeps fluctuating). I now understand the reason to connect to a "pull-down" resistor.

On the other hand, I am not sure if I understand why the circuit you proposed is working - my confusion is because when the button is pressed, the capacitor is connected to 5v in both ends. My understanding is as follows: when the button is not pressed, one end of the capacitor receives 5V, and the other is connected to ground through the resistor, so the capacitor charges. When the button is pressed, both ends of the capacitor are connected to 5V, and since there is no difference in potential in the two ends, the capacitor discharges. The resistance (of the resistor) regulates both the current for charging and discharging the capacitor. Is this the general idea?

Btw, I do have a multimeter - is there something interesting I could be measuring in this example?

Thanks again for the help!

Luiz Gustavo