const int ledPin = 13; // the pin that the LED is attached to long previousMillis=0; //previous millisecond unsigned long currentMillis = 0;//current millisecond int SerialValue=0; void setup(){ // initialize the serial communication: Serial.begin(9600); // initialize the ledPin as an output: pinMode(ledPin, OUTPUT);}void loop() { SerialValue=0; previousMillis =0; currentMillis =0; if (Serial.available()) { SerialValue = Serial.read(); if (SerialValue == '1') //if SerialValue = 1, turn LED on for 3 seconds { previousMillis = millis(); currentMillis = millis(); while(currentMillis - previousMillis <= 3000) { digitalWrite(ledPin,HIGH); currentMillis = millis(); } digitalWrite(ledPin, LOW);//turn LED off after 3 seconds } else //if SerialValue is not = 1, LED off { digitalWrite(ledPin, LOW); } }}
unsigned long previousMillis=0; //previous millisecondunsigned long currentMillis = 0;//current millisecond
while(currentMillis - previousMillis <= 3000)
const int ledPin = 13; // the pin that the LED is attached tounsigned long previousMillis=0; //previous millisecondunsigned long currentMillis = 0;//current millisecondint SerialValue=0; void setup(){ // initialize the serial communication: Serial.begin(9600); // initialize the ledPin as an output: pinMode(ledPin, OUTPUT);}void loop() { SerialValue=0; if (Serial.available()) { SerialValue = Serial.read(); if (SerialValue == '1') //if SerialValue = 1, turn LED on for 3 seconds { previousMillis = millis(); // save the time the LED was turned ON. digitalWrite(ledPin,HIGH); } else //if SerialValue is not = 1, LED off { digitalWrite(ledPin, LOW); } } currentMillis = millis(); if ((currentMillis - previousMillis) <= 3000) { digitalWrite(ledPin, LOW);//turn LED off after 3 seconds from previousMillis }}
const int ledPin = 13; // the pin that the LED is attached tounsigned long previousMillis=0; //previous millisecondunsigned long currentMillis = 0;//current millisecondint SerialValue=0; void setup(){ // initialize the serial communication: Serial.begin(9600); // initialize the ledPin as an output: pinMode(ledPin, OUTPUT);}void loop() { SerialValue=0; if (Serial.available()) { SerialValue = Serial.read(); if (SerialValue == '1') //if SerialValue = 1, turn LED on for 3 seconds { previousMillis = millis(); // save the time the LED was turned ON. digitalWrite(ledPin,HIGH); } else //if SerialValue is not = 1, LED off { digitalWrite(ledPin, LOW); } } currentMillis = millis(); if ((currentMillis - previousMillis) >= 3000) { digitalWrite(ledPin, LOW);//turn LED off after 3 seconds from previousMillis }}
pero ndi po nia napapailaw ung LED ng 3 seconds
ahmm..continuous po..ska nailaw lng sya pag continuous din ung "1" sa serial port. gusto ko po sana kasi parang momentary lang ung pag send ko ng "1"
void setup(){ // initialize the serial communication: Serial.begin(9600); // initialize the ledPin as an output: pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); <---- singit mo ito para sure na OFF ang LED sa umpisa}
ahmm..ganto po..pag on ng arduino, off ung LED. mag ON lang sya pag nagsend na ko ng "1" then, off na kgad pag inalis ko na ung "1".. pag continuous lang ako nag send ng "1", continuous ON lang din sya.
const int ledPin = 13; // the pin that the LED is attached tounsigned long previousMillis=0; //previous millisecondunsigned long currentMillis = 0;//current millisecondint SerialValue=0; void setup(){ // initialize the serial communication: Serial.begin(9600); // initialize the ledPin as an output: pinMode(ledPin, OUTPUT);}void loop(){ SerialValue=0; if (Serial.available()>0) { SerialValue = Serial.read(); Serial.flush(); if (SerialValue == '1') //if SerialValue = 1, turn LED on for 3 seconds { digitalWrite(ledPin,HIGH); } else if (SerialValue == '0') { digitalWrite(ledPin, LOW); } }}
nag ON siya pag my "1" at OFF pag "0". ayun na po un. possible po ba n lagyan un ng time. ung pag ON nia my time na 3 secs. saka pwede ako my press ng switch para mahinto ung pg ON nia ng 3 secs? halimbawa po, nakaka 1 sec pa lang sya n ON pwede na ko mg switch para OFF na kagad siya?
const int ledPin = 13; // the pin that the LED is attached tounsigned long previousMillis=0; //previous millisecondunsigned long currentMillis = 0;//current millisecondint SerialValue=0; void setup(){ // initialize the serial communication: Serial.begin(9600); // initialize the ledPin as an output: pinMode(ledPin, OUTPUT);}void loop(){ SerialValue=0; if (Serial.available()>0) { SerialValue = Serial.read(); Serial.flush(); if (SerialValue == '1' && previousMillis==0) { previousMillis = millis(); // save the time the LED was turned ON. digitalWrite(ledPin,HIGH); } else if (SerialValue == '0') { previousMillis = 0; //Rearm the timer digitalWrite(ledPin, LOW); } } currentMillis = millis(); if ((currentMillis - previousMillis)>=3000 && previousMillis!=0) { previousMillis = 0; //Rearm the timer digitalWrite(ledPin, LOW);//turn LED off after 3 seconds from previousMillis }}