Notifications
Clear all

[Solved] Help needed with Arduino Library

2 Posts
1 Users
0 Likes
1,826 Views
(@pugwash)
Noble Member
Joined: 4 years ago
Posts: 1012
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)
Noble Member
Joined: 4 years ago
Posts: 1012
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