I just got my arduino uno today and I’m going through the Make:Projects Getting started with arduino, book. And I’m doing the project where you can press and hold one button to set the brightness of the led. but when I go to compile the code it gives me an error.
Here’s the code:
#define LED 9
#define BUTTON 7
int val = 0; //state of input pin
int old_val = 0; //preivous value of val
int state = 0; //0=off 1=on
int brightness = 128; // stores brightness value
unsigned long startTime = 0; //when did we start pressing the button?
void setup() {
pinMode(LED, OUTPUT); //tells the arduino LED is output
pinMode(BUTTON, INPUT); //tells that button is input
}
void loop() {
val = digitalRead(BUTTON); //read input value and store it
// check if there was a trasistion
if ((val == HIGH && (old_val == LOW)) {
state = 1 - state; //change the state from off to on
// or vice-versa
startTime = millis(); // millis() is the arduino clock it returns how many milliseconds have passed since the board has been reset.
//this line remebers when the button was last pressed
delay(10);
}
//check wheather the buttion is being held down
if ((val == high) && (old_val == HIGH)) {
//if the button is held for more than 500ms
if (state == 1 && (millis() - startTime) < 500) {
brightness++; //increment brighness by 1
delay(10); //delay to aviod brightness going up too fast.
if (brightness > 255 ) { // 255 is max brightness
brightness = 0; // if we go over 255 go back to 0
}
}
}
old_val = val; // val is now old, store it
if (state == 1) {
analogWrite(LED, brightness); // turn on the led at the current brightness level
} else {
analogWrite(LED, 0); // turn led off
}
}
the error is:
sketch_dec23b.cpp: In function ‘void loop()’:
sketch_dec23b:20: error: expected `)’ before ‘{’ token
I could have missed something in the code from the book but I’m pretty sure I didn’t.
So if you could help me, that would be great!