Notifications
Clear all

[Solved] Help needed with Arduino Library

2 Posts
1 Users
0 Likes
2,077 Views
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

I have turned my attention to Arduino Libraries and how to create them, but I am stuck.

The following works but it is not quite what I want, here is the very simple code.

LED_KEY.h

#ifndef LED_KEY_H
#define LED_KEY_H

#include "Arduino.h"

class Led_Key  {
 public:
   // Constructor
   Led_Key(int a);    

   // Methods
   void ledON();
   void ledOFF();    

   int _a;
};
#endif

LED_KEY.cpp

# include "LED_KEY.h"

Led_Key::Led_Key(int a){
 _a = a;
}

void Led_Key::ledON(){
 Serial.print(_a);
 Serial.println(" LED is on");
}

void Led_Key::ledOFF(){
 Serial.print(_a);
 Serial.println(" LED is off");
}

Arduino Code

#include <LED_KEY.h>

Led_Key keyLed(4);

void setup() {
Serial.begin(9600);
pinMode(strb, OUTPUT);
pinMode(clk, OUTPUT);
pinMode(data, OUTPUT);
}

void loop() {
delay(2000);
keyLed.ledON();
delay(2000);
keyLed.ledOFF();
}

What I want to do is not have "4" constantly being sent to the ledON and ledOff functions but to send a variable example ledOFF(4).

Passing a variable to a library function instead of the class.

I hope someone has got some idea of what I am talking about, Please!


   
Quote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

Took a while but I figured it out!

@dronebot-workshop Bill, still can't post .cpp files!

#include "LED_KEY.h"

Led_Key::Led_Key(){
}

void Led_Key::ledON(int a){
 Serial.print(a);
 Serial.println(" LED is on");
}

void Led_Key::ledOFF(int a){
 Serial.print(a);
 Serial.println(" LED is off");
}

Have attached the working files in case anyone else wants to follow me down this rabbit hole!


   
ReplyQuote