caramoan tour package

caramoan tour package

Author Topic: programming arduino  (Read 847 times)

Offline veronica

  • CR2032 Battery
  • **
  • Posts: 13
  • Pogi/Ganda Points: 0
programming arduino
« on: February 20, 2011, 06:34:10 AM »
i am trying to light up an LED for 3 seconds when there is a value "1" in the serial port and turn it off afterward. when there is no value, then the LED is off. i've made this code but it only turns the LED on constantly.

please help.:)
Code: [Select]
  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);
    }
  }
}

Philippine Electronics Forum

programming arduino
« on: February 20, 2011, 06:34:10 AM »

Offline motion55

  • Technical People
  • Diesel Generator
  • *****
  • Posts: 1881
  • Pogi/Ganda Points: 244
  • Gender: Male
  • Been at this longer than you've been alive.
Re: programming arduino
« Reply #1 on: February 20, 2011, 09:10:28 AM »
I have a suggestion and a question.

Try changing "long previousMillis" to "unsigned long previousMillis".

Quote
unsigned long previousMillis=0;  //previous millisecond
unsigned long currentMillis = 0;//current millisecond

In the ff. statement:

Quote
while(currentMillis - previousMillis <= 3000)

You are subtracting (signed)previousMillis from (unsigned)currentMillis. I don't know the behavior when millis() is returning a large number. It could be interpreted as a negative number on previousMillis and positive on currentMillis even if they are almost at the same time. Performing the subtraction (currentMillis - previousMillis) will result in a large value greater than 3000. The program will immediately turn OFF the LED.

As a recommendation for better programming practice, you should not lock the program within a blocking loop inside the function loop(). The following code will allow the Arduino board to do something else while waiting for a serial port character to arrive.

Code: [Select]
const int ledPin = 13;      // the pin that the LED is attached to
unsigned 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;

    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
    }
}

My next question is:

Are you sure low is OFF and high is ON on the LED? On some circuits, low is ON (active low) and high is OFF for driving LEDs. Just being sure.
 
"Set your mind free!"

Philippine Electronics Forum

Re: programming arduino
« Reply #1 on: February 20, 2011, 09:10:28 AM »

Offline motion55

  • Technical People
  • Diesel Generator
  • *****
  • Posts: 1881
  • Pogi/Ganda Points: 244
  • Gender: Male
  • Been at this longer than you've been alive.
Re: programming arduino
« Reply #2 on: February 20, 2011, 09:31:59 AM »
Oops.. Sorry. Please modify the suggested code above as follows:

Quote
const int ledPin = 13;      // the pin that the LED is attached to
unsigned 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;

    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
    }
}
"Set your mind free!"

Philippine Electronics Forum

Re: programming arduino
« Reply #2 on: February 20, 2011, 09:31:59 AM »

Offline veronica

  • CR2032 Battery
  • **
  • Posts: 13
  • Pogi/Ganda Points: 0
Re: programming arduino
« Reply #3 on: February 20, 2011, 11:43:21 AM »
thanks for the reply.. ung code po na bngay nio sinubukan ko po sya i-run..pero ndi po nia napapailaw ung LED ng 3 seconds..hinahanap ko san ng ung mali pero tama naman sya dapat..

Philippine Electronics Forum

Re: programming arduino
« Reply #3 on: February 20, 2011, 11:43:21 AM »

Offline motion55

  • Technical People
  • Diesel Generator
  • *****
  • Posts: 1881
  • Pogi/Ganda Points: 244
  • Gender: Male
  • Been at this longer than you've been alive.
Re: programming arduino
« Reply #4 on: February 20, 2011, 11:47:39 AM »
Quote
pero ndi po nia napapailaw ung LED ng 3 seconds

Ilang seconds siya umiilaw?
"Set your mind free!"

Philippine Electronics Forum

Re: programming arduino
« Reply #4 on: February 20, 2011, 11:47:39 AM »

Offline veronica

  • CR2032 Battery
  • **
  • Posts: 13
  • Pogi/Ganda Points: 0
Re: programming arduino
« Reply #5 on: February 20, 2011, 12:00:59 PM »
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" :)

Offline toasted siopao

  • She loves me, she loves me not, she loves me...
  • Global Moderator
  • Gas Turbine
  • *****
  • Posts: 2574
  • Pogi/Ganda Points: 107
  • Gender: Female
    • TeknoBlogger
Re: programming arduino
« Reply #6 on: February 20, 2011, 12:09:36 PM »
di kaya dapat i-clear mo buffer ng serial mo?

wala ako experience pa sa arduino.

Offline veronica

  • CR2032 Battery
  • **
  • Posts: 13
  • Pogi/Ganda Points: 0
Re: programming arduino
« Reply #7 on: February 20, 2011, 12:28:24 PM »
pno po icclear ung buffer?

Offline motion55

  • Technical People
  • Diesel Generator
  • *****
  • Posts: 1881
  • Pogi/Ganda Points: 244
  • Gender: Male
  • Been at this longer than you've been alive.
Re: programming arduino
« Reply #8 on: February 20, 2011, 12:43:18 PM »
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" :)

Linawin natin ha. Importante ito in diagnosing the cause. Pag ON ng Arduino board, patay ang LED. As soon as nasend ang '1' character, mag-oON ang LED at ayaw na mamatay. Please confirm.
"Set your mind free!"

Offline toasted siopao

  • She loves me, she loves me not, she loves me...
  • Global Moderator
  • Gas Turbine
  • *****
  • Posts: 2574
  • Pogi/Ganda Points: 107
  • Gender: Female
    • TeknoBlogger
Re: programming arduino
« Reply #9 on: February 20, 2011, 12:46:44 PM »
Quote
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

}

Offline motion55

  • Technical People
  • Diesel Generator
  • *****
  • Posts: 1881
  • Pogi/Ganda Points: 244
  • Gender: Male
  • Been at this longer than you've been alive.
Re: programming arduino
« Reply #10 on: February 20, 2011, 12:53:40 PM »
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" :)

Kung nag send ka ng ibang character, like a '0' for example, namamatay ba ang LED? Pag ayaw baka naman maling port nakakabit ang LED?
"Set your mind free!"

Offline veronica

  • CR2032 Battery
  • **
  • Posts: 13
  • Pogi/Ganda Points: 0
Re: programming arduino
« Reply #11 on: February 20, 2011, 01:26:16 PM »
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.

Offline motion55

  • Technical People
  • Diesel Generator
  • *****
  • Posts: 1881
  • Pogi/Ganda Points: 244
  • Gender: Male
  • Been at this longer than you've been alive.
Re: programming arduino
« Reply #12 on: February 20, 2011, 01:46:21 PM »
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.

Try this modified code. Describe the result as clear or more detailed than above reply.

Code: [Select]
const int ledPin = 13;      // the pin that the LED is attached to
unsigned 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;

    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);
        }
    }
}

Dapat if you send a '1' mag ON and if you send a '0' mag OFF.
"Set your mind free!"

Offline veronica

  • CR2032 Battery
  • **
  • Posts: 13
  • Pogi/Ganda Points: 0
Re: programming arduino
« Reply #13 on: February 20, 2011, 01:55:56 PM »
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?

Offline motion55

  • Technical People
  • Diesel Generator
  • *****
  • Posts: 1881
  • Pogi/Ganda Points: 244
  • Gender: Male
  • Been at this longer than you've been alive.
Re: programming arduino
« Reply #14 on: February 20, 2011, 02:38:08 PM »
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?

That is actually the next step. Here is the modified code:

Code: [Select]
const int ledPin = 13;      // the pin that the LED is attached to
unsigned 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;

    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
    }
}



"Set your mind free!"

Offline veronica

  • CR2032 Battery
  • **
  • Posts: 13
  • Pogi/Ganda Points: 0
Re: programming arduino
« Reply #15 on: February 20, 2011, 04:48:34 PM »
i uploaded the code but still the same results.:(

Offline motion55

  • Technical People
  • Diesel Generator
  • *****
  • Posts: 1881
  • Pogi/Ganda Points: 244
  • Gender: Male
  • Been at this longer than you've been alive.
Re: programming arduino
« Reply #16 on: February 20, 2011, 06:05:44 PM »
Same results with what? I getting tired of this. Bahala ka na lang.
"Set your mind free!"

Offline RiDdLeR???

  • Hydroelectric
  • ***
  • Posts: 3032
  • Pogi/Ganda Points: 208
Re: programming arduino
« Reply #17 on: February 20, 2011, 06:20:15 PM »
naku, kulang kulang kasi kung sumagot, pinapahulaan pa kung ano ang ibig sabihin.  Tutal ayan lang naman ang ginagawa ng code mo, kalimutan mo na yang millis na yan at gumamit ka na lang ng delay() routine, tapos na ang problema mo.

Offline veronica

  • CR2032 Battery
  • **
  • Posts: 13
  • Pogi/Ganda Points: 0
Re: programming arduino
« Reply #18 on: February 20, 2011, 06:27:49 PM »
i'm sorry..:( but thanks for all your help..

Offline motion55

  • Technical People
  • Diesel Generator
  • *****
  • Posts: 1881
  • Pogi/Ganda Points: 244
  • Gender: Male
  • Been at this longer than you've been alive.
Re: programming arduino
« Reply #19 on: February 20, 2011, 07:51:56 PM »
One final advice. Malamang may problema ang millis(). Guess ko that is why namamatay ang LED kaagad. I think sobrang mabilis ang clock. Perhaps mali ang hardware. But since there is no information that is only a best guess ko. Good luck na lang.
"Set your mind free!"

Philippine Electronics Forum

Re: programming arduino
« Reply #19 on: February 20, 2011, 07:51:56 PM »

 

Privacy Policy

Contact Us: elabph@yahoo.com