Go Down

Topic: Help for a simple for loop (Read 599 times) previous topic - next topic

FaTaL

Mar 26, 2013, 11:26 am Last Edit: Mar 26, 2013, 11:46 am by FaTaL Reason: 1
Hi guys. I want to make my code a bit more robust and light because I have a little bit of trouble with the amount of data the ethernet shield can handle. If I am near 16kb of code, it crashes, although the arduino has 32kb. Anyway... To the point :)

I want to substitute the following piece of code and make a "for loop" for the pins 2-9  but I don't know how to make it read the ("on1"), ("off1), with 1 being an int i.

Quote

     if(readString.indexOf("on1") >0)//checks for on
         {
           digitalWrite(2, HIGH);    // set pin 2 high
//            Serial.println("Led On");
           //client.println("Light 1 Is On");
           //client.println("<br />");
         }
         else{
           if(readString.indexOf("off1") >0)//checks for off
           {
             digitalWrite(2, LOW);    // set pin 2 low
//              Serial.println("Led Off");
             //client.println("Light 1 Is Off");
             //client.println("<br />");
           }
         }

         if(readString.indexOf("on2") >0)//checks for on
         {
           digitalWrite(3, HIGH);    // set pin 3 high
//            Serial.println("Led On");
         }
         else{
           if(readString.indexOf("off2") >0)//checks for off
           {
             digitalWrite(3, LOW);    // set pin 3 low
//              Serial.println("Led Off");
           }
         }




I want it to be something like but obviously doesn't work because its wrong syntax.


Code: [Select]
for (int i=1; i<9;i++)
if(readString.indexOf("on"i) >0)//checks for on
         {
           digitalWrite(i+1, HIGH);    // set pin i+1 high

         }
         else{
           if(readString.indexOf("off"i) >0)//checks for off
           {
             digitalWrite(i+1, LOW);    // set pin i+1 low

           }
         }
}




PaulS

Quote
If I am near 16kb of code, it crashes, although the arduino has 32kb.

It has 32K of program space, but only 2K of SRAM, which you are pissing away using the String class.

sprintf() can be used to make "on1", "on2", "on3", etc.

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

FaTaL

Thanks for your answer :) The on1, on2, on3 etc are address bar queries for switches so that's what I m trying to read. I m not sure how to use sprintf() to achieve that (noob alert :D). Can you make a proper synstax on this if(readString.indexOf("on"i) >0) with i being the int? Thanks again

majenko

For that you would use sscanf.  Drop the String class and use C strings (null-terminated character arrays).  Then you can use something like
Code: [Select]

int num;

if (sscanf(incomingString, "on%d", &num) == 1) {
  switchOnLED(num);
}

FaTaL

I did it by using the String(n) function to read the integer as a string. Thanks everyone for your help

SOLVED

GoForSmoke

Do a search on Port Manipulation. With an UNO you have 6 pins open on 2 ports and 4 on the other by default. You could pack your data bits into 2 ports and get your 8 bits out in just 2 writes. With a MEGA I am guessing you'd have a full port open to use, 1 write would do.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

AWOL

Quote
SOLVED
sp. "postponed".
Please, try not to use String.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

GoForSmoke


Quote
SOLVED
sp. "postponed".
Please, try not to use String.


LOL, postponed till the next Help! thread.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up