Go Down

Topic: Combining 2 Strings to a new variable (Read 758 times) previous topic - next topic

upuptothesky

Apr 07, 2009, 05:23 pm Last Edit: Apr 07, 2009, 05:23 pm by upuptothesky Reason: 1
Hi,
im very new to th world of arduino and i got simple a little problem i cant solve.
I got 5 LEDs and i want to do a "knight-rider"-effect.
I know, its easy to do it in a way like this:

digitalWrite(LED1, HIGH);   // sets the LED on
    delay(100);
    digitalWrite(LED1, LOW);   // sets the LED on
    digitalWrite(LED2, HIGH);   // sets the LED on
    delay(100);
    digitalWrite(LED2, LOW);   // sets the LED on
    digitalWrite(LED3, HIGH);   // sets the LED on
    delay(100);
    digitalWrite(LED3, LOW);   // sets the LED on
    digitalWrite(LED4, HIGH);   // sets the LED on
    delay(100);
    digitalWrite(LED4, LOW);   // sets the LED on
    digitalWrite(LED5, HIGH);   // sets the LED on
    delay(100);
    digitalWrite(LED5, LOW);   // sets the LED on
    digitalWrite(LED4, HIGH);   // sets the LED on
    delay(100);
    digitalWrite(LED4, LOW);   // sets the LED on
    digitalWrite(LED3, HIGH);   // sets the LED on
    delay(100);
    digitalWrite(LED3, LOW);   // sets the LED on
    digitalWrite(LED2, HIGH);   // sets the LED on
    delay(100);
    digitalWrite(LED2, LOW);   // sets the LED on

This works fine.
But i want to do it with a for-structure and by combining the
var leds from the for-structure with the word "LED" to get a new variable i can use to HIGH/LOW the LED.

But it dont work (see code below).
The LEDs doesnt do anything and from the serial i only get the letter "u" back or a DEC-value of 117?
It also doesnt work without the for-structure and with using a fixed value of 1 for leds.

What am i doing wrong?
Kind regards,
Sebastian


My code so far:

int leds;
char teil1;
char teil2;
char komplett;

void setup()                    
{
 pinMode(LED1, OUTPUT);      // sets the digital pin as output
 pinMode(LED2, OUTPUT);      // sets the digital pin as output
 pinMode(LED3, OUTPUT);      // sets the digital pin as output
 pinMode(LED4, OUTPUT);      // sets the digital pin as output
 pinMode(LED5, OUTPUT);      // sets the digital pin as output
9600baud
}

void loop()                     // run over and over again
{
for (int leds=1;leds<=5;leds++)
   {
    teil2=char(leds); //use leds as a char. if leds=1, teil2 should be 1

    teil1='LED';
    komplett=teil1+teil2;  //combining both chars, if teil1=1, komplett  
                                    //should be LED1
Serial.println(komplett);
Serial.println(komplett,DEC);
digitalWrite(komplett, HIGH);   // sets the LED on, if leds=1, it should
                                   //have the same effect like
                                           //digitalWrite(LED!,HIGH);
delay(10);
digitalWrite(komplett, LOW);   // sets the LED on
}


AlphaBeta

#1
Apr 07, 2009, 06:02 pm Last Edit: Apr 07, 2009, 06:03 pm by AlphaBeta Reason: 1
C/C++ is not an interpreted language, so can not create variablenames by combining strings and numbers, as is allowed in for instnce PHP.

Wht you need to use is array

Also see the Loop: Knight Rider code.

upuptothesky

Hi AlphaBeta,

thanks for your answer. Im used to PHP, thats why this question arised. :)

Kind regards,
Sebastian

Anachrocomputer

Code: [Select]
teil1='LED';

This line in particular is wrong because in C, a character constant should only contain a single character.  String constants may have multiple characters, but must have double quotes:

Code: [Select]
Serial.print ("hello");

And:

Code: [Select]
digitalWrite(komplett, HIGH);

The first argument to 'digitalWrite()' is an integer, which must have been named 'LED1' and so on somewhere else in your sketch code.  So you can do this:

Code: [Select]
int i;
for (i = LED1; i <= LED5; i++)
 digitalWrite (i, LOW);


And that loop will set all five LED outputs LOW.

Anachrocomputer

I realised on the way in this morning that the above code won't work!  It only works if LED1, LED2 and so on are in strict ascending numerical order with no gaps.  That's not necessarily true, maybe not even likely to be true.

Sorry for the misinformation!

Go Up