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:
2 Answers 2
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.
-
Still does not compile. Maybe the way that I am putting the files in folders is not right? See the image in main question.Amin– Amin2015年11月19日 20:23:58 +00:00Commented 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.Majenko– Majenko2015年11月19日 20:24:36 +00:00Commented Nov 19, 2015 at 20:24
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);
}
#include "LEDD/LED.h"