OOP Basic Arduino Multitasking - TopicsExpress



          

OOP Basic Arduino Multitasking โดยใช้ฐานเวลาจาก millis() class Flasher { // Class Member Variables // These are initialized at startup int ledPin; // the number of the LED pin long OnTime; // milliseconds of on-time long OffTime; // milliseconds of off-time // These maintain the current state int ledState; // ledState used to set the LED unsigned long previousMillis; // will store last time LED was updated // Constructor - creates a Flasher // and initializes the member variables and state public: Flasher(int pin, long on, long off) { ledPin = pin; pinMode(ledPin, OUTPUT); OnTime = on; OffTime = off; ledState = LOW; previousMillis = 0; } void Update() { // check to see if its time to change the state of the LED unsigned long currentMillis = millis(); if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime)) { ledState = LOW; // Turn it off previousMillis = currentMillis; // Remember the time digitalWrite(ledPin, ledState); // Update the actual LED } else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime)) { ledState = HIGH; // turn it on previousMillis = currentMillis; // Remember the time digitalWrite(ledPin, ledState); // Update the actual LED } } }; Flasher led1(2, 1000, 2000); Flasher led2(3, 300, 500); Flasher led3(4, 100, 200); void setup() { Serial.begin(9600); } void loop() { led1.Update(); led2.Update(); led3.Update(); }
Posted on: Sat, 27 Dec 2014 01:39:48 +0000

Trending Topics



t:30px;">
After ‘land to tiller’, Omar’s ‘power to people’

© 2015