Pages: [1]   Go Down
Author Topic: Combining 2 Strings to a new variable  (Read 680 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
 }

« Last Edit: April 07, 2009, 10:23:44 am by upuptothesky » Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: April 07, 2009, 11:03:27 am by AlphaBeta » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi AlphaBeta,

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

Kind regards,
Sebastian
Logged

Bristol, UK
Offline Offline
Edison Member
*
Karma: 1
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
Serial.print ("hello");

And:

Code:
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:
int i;
for (i = LED1; i <= LED5; i++)
  digitalWrite (i, LOW);

And that loop will set all five LED outputs LOW.
Logged

Bristol, UK
Offline Offline
Edison Member
*
Karma: 1
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: