Hi,
I've been using C for my Arduino programs, but now I've learnt C++, and would like to transition over to using it in my sketches. I want to start with the basic blink sketch.
I've modified it to this code just to make an LED blink on and off:
/*
- Blinks an LED
- Code is adapted from C to C++
*/
//int led = 13; // LED connected at pin 13
class LED_Blink
{
public:
void blinking()
{
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
private:
static const int led = 13; // LED connected at pin 13
};
void setup()
{
pinMode(led, OUTPUT);
}
void loop()
{
// FORM: Class_Name object (Class_Name [space] object)
LED_Blink blinker; // creates an object, "blinker", which will point to a function within the class "LED_Blink"
// FORM: object.member_function() (object [dot] class member_function)
blinker.blinking();
}
My problem is that the with the line of code,
static const int led = 13; // LED connected at pin 13
declared in the private access specifier, it will not run.
However, if I delete this line, and use the global variable instead for the led,
int led = 13; // LED connected at pin 13
, it will work as it should.
I'm confused because I thought that the variable led = 13 is specific to only to the class LED_Blink, and I should put it there in the private.
What am I doing wrong?