LED Code help

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

// 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’) {

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(itoa((lineNr), buffer, 10));
// 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);
totalstr = serInString;
postotal = totalstr.lastIndexOf(“total”);
for (openCases = 0; openCases < 1000; openCases++) {
if(openCases <= 499) {
analogWrite(greenPin, 1);
analogWrite(redPin, 255-(255openCases/500));
else {
analogWrite(redPin, 1);
analogWrite(greenPin, 255*(openCases-500)/500);


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.

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?

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

  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.

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

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

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.