Pages: [1]   Go Down
Author Topic: invalid types 'char [int]' for array subscript  (Read 3501 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I'm trying to make a system to turn lights through a web page, however I'm having problems with the arduino code.
My goal is to send a PHP array with five numbers (5 LEDs), where "1" is on, "0" is off.

That is, sends PHP 00001, which means that only 5 LED is on.

I tried to make the Arduino code, however it appears to me an error "invalid types 'char [int]' for array subscript" in line "if (character [counter] == 1)".

Anyone can help me, please?


Code:
int ledPin13 =  13;
int ledPin11 =  11;
int ledPin9  =  9;
int ledPin7  =  7;
int ledPin5  =  5;
int counter  =  0;
char estado  =  'LOW';

void setup()   {

  Serial.begin(9600);

  pinMode(ledPin13, OUTPUT);
  pinMode(ledPin11, OUTPUT);
  pinMode(ledPin9, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  pinMode(ledPin5, OUTPUT);

}

void loop()
{
  char caracter;
  caracter = Serial.read();

  while(counter < 5, counter++){
    
    if(caracter[counter] == 1)
    (
      estado = 'HIGH';
    ) else (
      estado = 'LOW';
    )
    digitalwrite(caracter[counter], estado)
  
  )
  }


Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3350
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You declared caracter as a single char, but are trying to index it like it's an array.
Logged

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

But how to declare array?

"character []" or "character" [5]? I've tried both ways and always gives error
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3350
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But how to declare array?

"character []" or "character" [5]? I've tried both ways and always gives error

Neither. If you want do declare caracter as an array of five chars, you would put this:
Code:
char caracter[5];
You'll also need to rethink your strategy for reading data from the serial port. Serial.read() will return a single character, not all 5 at once, so you will need to call it multiple times to get all your values. It would also be a good idea to not try and read in serial data when there is none. This is where Serial.available() comes in handy. Also, assuming you are using the serial monitor, typing "1" into it will not send 1, it will send '1'.
Logged

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

My goal is the "serial read" read a number with 5 digits, sends them to a variable (string) and a counter field of string through. If the field is "1", the light comes on, if it is "0" deletes the light.
Logged

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

I tryed with char caractere[5], now the error is: "incompatible types in assigment of 'int' to 'char [5]'" in the line "  caracter = Serial.read();"
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3350
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

More Problems:

Code:
char estado  =  'LOW';
'LOW' is not a character. If you want to assign it the defined value of LOW, remove the single quotes. Same problem down in your loop.
Code:
while(counter < 5, counter++){
counter < 5 will never get run because it will constantly be returning the result of counter++.  The ',' doesn't mean do this ever time. If you want to increment counter on every loop, put it in the body, or better yet, use a for loop. In this case, however, a while loop would be better used with Serial.available().

My goal is the "serial read" read a number with 5 digits, sends them to a variable (string) and a counter field of string through. If the field is "1", the light comes on, if it is "0" deletes the light.
Serial.available() will tell you how many chars are available to read. A good idea would be to make sure you have no less than 5 before starting to read everything into your array.
I tryed with char caractere[5], now the error is: "incompatible types in assigment of 'int' to 'char [5]'" in the line "  caracter = Serial.read();"
That's because as I said above, Serial.read() returns a SINGLE character, not everything that you sent to it. Look at some of the examples for Serial communication to see how it handles reading serial data.
« Last Edit: May 17, 2012, 05:37:26 pm by Arrch » Logged

Pages: [1]   Go Up
Jump to: