Pages: [1]   Go Down
Author Topic: ERRO: invalid types 'char[int]' for array subscript  (Read 980 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

Boa noite,

Estou a tentar fazer um sistema para acender luzes atraves de uma pagina web, no entanto estou com problemas no codigo do arduino.
O meu objectivo é o PHP enviar um array com 5 numeros (5 leds), em que 1 representa ligado, 0 representa desligado.

Ou seja, PHP envia 00001, o que significa que apenas o 5º led está ligado.

Experimentei fazer o codigo arduino, no entanto aparece-me um erro "invalid types 'char[int]' for array subscript" na linha "if(caracter[counter] == 1)".

Alguem me pode ajudar, pf?


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

central nervous system
Offline Offline
Full Member
***
Karma: 0
Posts: 135
Causing havoc inside your brain since 1980
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Faz assim:

Code:
#define LEDPIN13 13
#define LEDPIN11 11
#define LEDPIN9  9
#define LEDPIN7  7
#define LEDPIN5  5

void setup() {
    Serial.begin( 9600 );
    pinMode( LEDPIN13, OUTPUT );
    pinMode( LEDPIN11, OUTPUT );
    pinMode( LEDPIN9,  OUTPUT );
    pinMode( LEDPIN7,  OUTPUT );
    pinMode( LEDPIN5,  OUTPUT );
}

void loop() {
    int buffer[ 5 ];
    int pinos[ 5 ] = { LEDPIN13, LEDPIN11, LEDPIN9, LEDPIN7, LEDPIN5 };
    int indice = 0;

    if ( Serial.available() > 0 ) {
        int input = Serial.read();
        if ( input != '0' && input != '1' ) {
            indice = 0;
        } else {
            buffer[ indice++ ] = input;
        }
    }

    if ( indice == 5 ) {
        for ( int i = 0; i < 5; i++ ) {
            if ( buffer[ i ] == '0' ) {
                digitalWrite( pinos[ i ], LOW );
            } else {
                digitalWrite( pinos[ i ], HIGH );
            }
        }
        indice = 0;
    }
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 202
Posts: 8711
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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();
   
    if(caracter == 1)
    (
      estado = HIGH;
    ) else (
      estado = LOW;
    )
    digitalwrite(caracter[counter], estado)
  }
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

central nervous system
Offline Offline
Full Member
***
Karma: 0
Posts: 135
Causing havoc inside your brain since 1980
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

John, there's a flaw in your sketch

The array caracter was not declared
Logged

Pages: [1]   Go Up
Jump to: