Pages: [1]   Go Down
Author Topic: Help for a simple for loop  (Read 469 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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

            }
          }
}


« Last Edit: March 26, 2013, 05:46:12 am by FaTaL » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How to use this forum

Code tags please, not quote tags.
Logged

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

Thanks for your answer smiley 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 smiley-grin). Can you make a proper synstax on this if(readString.indexOf("on"i) >0) with i being the int? Thanks again
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int num;

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

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

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

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

SOLVED
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 4702
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

 
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 4702
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

LOL, postponed till the next Help! thread.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: [1]   Go Up
Jump to: