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-(255openCases/500));
Serial.println(255-(255openCases/500));
}
else {
analogWrite(redPin, 1);
analogWrite(greenPin, 255*(openCases-500)/500);
Serial.println(255*(openCases-500)/500);
}
delay(50);
}
}
}