Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Time.h>
- // Simple macro for inverted pwm output (since my circuit will turn the lamp OFF whenever the output is HIGH)
- #define _analogWrite(a, b) analogWrite(a, 255-(b))
- // Globals
- const PROGMEM int pwmPin = 3;
- time_t lastUpdate = 0;
- int pwmValue = 0;
- void setup() {
- Serial.begin(9600);
- Serial.setTimeout(250);
- pinMode(pwmPin, OUTPUT);
- _analogWrite(pwmPin, pwmValue);
- }
- void loop() {
- // Get current time
- time_t currentTime = now();
- // If there's data available, process it
- if (Serial.available()) {
- // Read all the incoming data
- String str = Serial.readString();
- // Extract only the numbers from it
- String str2 = "";
- for (int i=0; i<str.length(); i++) {
- if (str[i] >= '0' && str[i] <= '9') str2 += str[i];
- }
- // Convert it to an int, and store it
- pwmValue = str2.toInt();
- // Save the current time
- lastUpdate = currentTime;
- }
- // Safety mechanism (if we haven't received anything in the last 30 seconds, set the output to 0)
- if (currentTime - lastUpdate > 30) pwmValue = 0;
- // Write the value we have to the output
- _analogWrite(pwmPin, pwmValue);
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement