Browse Source

Add LED class for managing LED output

Robert Marshall 5 years ago
parent
commit
3887540703
2 changed files with 31 additions and 4 deletions
  1. 26 0
      Receiver/src/LED.cpp
  2. 5 4
      Receiver/src/main.cpp

+ 26 - 0
Receiver/src/LED.cpp

@@ -0,0 +1,26 @@
+#include <Arduino.h>
+
+#define PWMRANGE 255
+
+class LED{
+	unsigned int _pin;
+	bool _on;
+
+public:
+	LED(unsigned int pin){
+		_pin = pin;
+		pinMode(pin, OUTPUT);
+	}
+
+	void on(){
+		_on = true;
+	}
+
+	void off(){
+		_on = false;
+	}
+
+	void loop(){
+		analogWrite(_pin, PWMRANGE * _on);
+	}
+};

+ 5 - 4
Receiver/src/main.cpp

@@ -1,9 +1,10 @@
 #include <Arduino.h>
 #include <Receiver.cpp>
+#include <LED.cpp>
 
-unsigned int _output = 12;
 void messageCallback(char *message);
-Receiver _receiver(11, &messageCallback, 9);
+Receiver _receiver(2, &messageCallback, 9);
+LED _led(11);
 
 bool validateMessage(char* message){
 	return strncmp("LED On: ", message, 8) == 0; //returns 0 on match
@@ -17,11 +18,10 @@ void messageCallback(char* message){
 	Serial.print("Message: ");
 	Serial.println(message);
 
-	digitalWrite(_output, parseMessage(message));
+	parseMessage(message) ? _led.on() : _led.off();
 }
 
 void setup(){
-	pinMode(_output, OUTPUT);
 	Serial.begin(9600);
 	Serial.println("Starting...");
 	_receiver.setup();
@@ -30,4 +30,5 @@ void setup(){
 
 void loop(){
 	_receiver.loop();
+	_led.loop();
 }