Skip to main content
image added
Source Link
Amin
  • 113
  • 1
  • 6

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

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?

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

Source Link
Amin
  • 113
  • 1
  • 6

Including Class with .h and .cpp files

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?