Pages: [1]   Go Down
Author Topic: LED Code help  (Read 944 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Forum! I'm in need of some help.

I'm writing code for a Piranha LED and I believe I've run into an issue with the limit of my Arduino Uno. Basically I'm trying to have my LED run from Green to Red as a number goes from 1-1000. I have the code laid out but at a few key points when the code runs the LED freaks out and displays incorrect colors; I believe 129 and 629 are the two primary troublesome numbers. It looks like the math is too large for the for the Arduino to compute? Is there some way around this? Sorry my code is such a mess.. Thanks for the help!

Here is the code: The part causing the issue is the For statement down at the bottom.

int serInLen=500;
int ledPin = 13; // LED connected to digital pin 13
int redPin = 9;
int greenPin = 10;
char serInString[500];
String totalstr;
int postotal;
char totalchars[500];
bool fileOk=false;

void setup()                    // run once, when the sketch starts
{
  pinMode(redPin, INPUT);
  pinMode(greenPin, INPUT);
  pinMode(ledPin, OUTPUT);  // sets the digital pin as output
  Serial.begin(9600);
  delay(1000);
 
   // Ask Gobetwino to run the PNG PING commando to check that the web server is responding before downloading a file
  // Serial.println("#S|PNG|[]#");       
  // Wait for the answer from Gobetwino 
  // readSerialString (serInString, 10000); 
  // check return code from DLFIL command from Gobetwino, if it is 0 the the server responded to the ping
  // if (serInString[0]=='0')  {           
    // Ask Gobetwino to run the DLBLINK command - downloading a file
    // Serial.println("#S|DLBLINK|[]#");       
    // Wait for the answer from Gobetwino 
    // readSerialString (serInString, 10000); 
     // check return code from DLFIL command from Gobetwino, if it is 0 the file is downloaded
    // if (serInString[0]=='0')  {           
        fileOk=true;
    //}
  //}
}

void loop()     // run over and over again
 
{
  char buffer[5];
  int openCases=0;
  int lineNr=0;
  // If the file was downloaded, Send the RDBLINK command asking Gobetwino to read line nr. 1 - 3 from
  // the file and blink LED the number of times read from each line
   if (fileOk) {
     /*for(lineNr=1;lineNr<=1;lineNr++) {
      Serial.print("#S|RDDESK|[");
      Serial.print(itoa((lineNr), buffer, 10));
      Serial.println("]#");
      // Wait for answer from Gobetwino and convert the returned answer to an integer, and call the blink function
      // An error check for a negative return code should be done here, but is ommited to keep the example as simple as posible
      //readSerialString (serInString, 10000);
      Serial.println(serInString);
        totalstr = serInString;
        postotal = totalstr.lastIndexOf("total");
        totalstr.substring(postotal+7).toCharArray(totalchars,serInLen);
      Serial.println(totalchars);
        openCases=atoi(totalchars);
      Serial.println(openCases);
     }*/
      for (openCases = 0; openCases < 1000; openCases++) {
        Serial.print("openCases=");
        Serial.println(openCases);
        if(openCases <= 499) {
          analogWrite(greenPin, 1);
          analogWrite(redPin, 255-(255*openCases/500));
          Serial.println(255-(255*openCases/500));
        }
        else {
         analogWrite(redPin, 1);
         analogWrite(greenPin, 255*(openCases-500)/500);
         Serial.println(255*(openCases-500)/500);
        }
        delay(50);
      }
     
  }
}
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1260
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

When posting code use the # button at the top of the display. It makes sure that code will be read correctly.

I am not near a pin reference, but are all the nominated pins PWM capable? The problem with sudden flips like you see often is due to the pin just being a digital output.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Full Member
***
Karma: 1
Posts: 135
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

first glance, your redpin, greenpin are configured as INPUT . . . and you are writing to them, I am new to Arduino, but does that make sense -- writing to an input pin?
Logged

Search for "mjkzz" on eBay :-)

Offline Offline
Newbie
*
Karma: 4
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
You are specifying algorithms that perform calculations on your variables that exceed their standard limits.  For example, in this statement:

Code:
  analogWrite(redPin, 255-(255*openCases/500));

You have declared the "openCases" as an int Datatype which has a standard range of -32,768 to 32,767. Without notice, the Arduino compilier is not expecting to deal with values greater than 32,767 so when it processes "(255*openCases)" the calculation exceeds the limit at times during your loop.

A quick fix would be declare "openCases" as "long", or "unsigned long" and this should implicitly correct the calculations, but do recommend you go over the different datatypes the arduino uses, and look into Type Conversion and Type Casting.  It'll be well worth the effort to get a grasp of these concepts as it will save you alot of hair-pulling down the road.
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 135
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think Skorn has nailed it!!! I actually ran into this before, not with Arduino but with PIC :-)
Logged

Search for "mjkzz" on eBay :-)

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 525
Posts: 26523
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

analogWrite only works with 0-255 also, maybe do this calculation seperately to make sure it is in that range
255*openCases/500
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry it took so long for a reply but thanks all! And Skorn, you sir are awesome! There will be a ton of googling/reading for me tonight.
« Last Edit: March 05, 2013, 11:24:30 am by Pjames6 » Logged

Pages: [1]   Go Up
Jump to: