1

I am trying to use a class in .ino file. The code is:

.ino file

#include <LED.h>

int Pin1 = 13;
int Pin2 = 12;
int Pin3 = 11;
LED led;
void setup() {
  pinMode(Pin1,OUTPUT);
  pinMode(Pin2,OUTPUT);
  pinMode(Pin3,OUTPUT);
  digitalWrite(Pin1,LOW);
  digitalWrite(Pin2,LOW);
  digitalWrite(Pin3,LOW);
}

void loop() {
  led.on(Pin1);
  delay(2);
  led.on(Pin2);
  delay(2);
  led.on(Pin3);
  delay(2);
}

.h file

#ifndef LED_h
#define LED_h

class LED{
public:
    void on(int);
    void off(int);
};

#endif

.cpp file

#include <stdio.h>
#include <Arduino.h>
#include "LED.h"    

void LED::on(int PIN){
    digitalWrite(PIN,HIGH);
}

void LED::off(int PIN){
    digitalWrite(PIN,LOW);
}

Arduino compiler picks the object declaration error:

LEDC:6: error: 'LED' does not name a type
LEDC.ino: In function 'void loop()':
LEDC:17: error: 'led' was not declared in this scope

How should I declare objects in Arduino then?

The way that I am putting the files in folders is like the attached image:

enter image description here

3
  • There is nothing syntactically wrong with your program - it compiles perfectly in UECIDE. My guess is it's something to do with the Arduino IDE. Which version of the IDE are you using? I know that recently they have made some changes to how the compilation works (or maybe doesn't work). Commented Nov 19, 2015 at 19:35
  • It is Arduino 1.5.6 Commented Nov 19, 2015 at 20:05
  • Your LED.cpp and LED.h files are in an LEDD subfolder. In your LEDC.ino file you should use #include "LEDD/LED.h" Commented Mar 28, 2019 at 12:39

2 Answers 2

1

That version of the IDE (it seems to change from version to version) differentiates between includes with <...> and includes with "...".

If you use <...> it only looks in the system and library areas. If you use "..." it also looks inside your sketch.

So in your main INO file change the LED include from:

#include <LED.h>

To:

#include "LED.h"

and it should compile.

2
  • Still does not compile. Maybe the way that I am putting the files in folders is not right? See the image in main question. Commented Nov 19, 2015 at 20:23
  • I did it by adding two new tabs - LED.cpp and LED.h and pasting your code into them. Commented Nov 19, 2015 at 20:24
0

All you need to do is this in the (.ino)

#include <LED.h>

int Pin1 = 13;
int Pin2 = 12;
int Pin3 = 11;
LED led1;
LED led2;
LED led3;
void setup() {
  pinMode(Pin1,OUTPUT);
  pinMode(Pin2,OUTPUT);
  pinMode(Pin3,OUTPUT);
  digitalWrite(Pin1,LOW);
  digitalWrite(Pin2,LOW);
  digitalWrite(Pin3,LOW);
}

void loop() {
  led1.on(Pin1);
  delay(2);
  led2.on(Pin2);
  delay(2);
  led3.on(Pin3);
  delay(2);
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.