Go Down

Topic: Light-up and if(), problems together (Read 749 times) previous topic - next topic

bongoboia

Hi  everybody,
i'm at my first experience with Arduino and i was thinking to try a program that light a specific led taking the number of the pin from serial.
Everything works until the digitalWrite, the leds are ok (i tried with the blink program and it works), the serial comunication is working too and i don't understand where the problem is, to me it seems all good my code...

A little explanation on how this program have to work: i want to send a message via serial monitor like A-1-2-A:
A = char that have to open and close the string, if none so i have an error
1 = command (1=HIGH) i don't have his part dont jet
2 = pin number that i want to light-up

Do you see the error in the code? the compiler says that all is good...
Thank you for your help and scuse me for my english mistakes

Code: [Select]

char inData[7];
char inChar;
byte index = 0;
int j,i;
char indirizzo;
int indirizzoInt;

void setup() 
{
 
  Serial.begin(115200);
  pinMode(indirizzoInt, OUTPUT);
//  pinMode(2, OUTPUT);
  }

   
void loop()
{
  while(Serial.available() > 0)
  {
    inChar = Serial.read();
    inData[index] = inChar;
    if(index==6)                                             
    {
      for(i=0; i<index+1; i++)
      {
        Serial.print(inData[i]);
      }
      Serial.println();
      if(inData[0]==inData[6])
      {
      Serial.println("OK1");
      if(inData[0]=='A')
      {
      Serial.println("OK2");
      }
      else
        Serial.println("KO2");
    }
    else
      Serial.println("KO1");
      indirizzo=inData[4];
      indirizzoInt=atoi(&indirizzo);
      Serial.print("indirizzo:");
      Serial.println(indirizzoInt);
      digitalWrite(indirizzoInt, HIGH);
    }
    index++;
    inData[index] = '\0';
    if(index>6){index=0;}
  }
}



michinyon

does it work ?   Is there a problem with it ?

bongoboia

the debug messages are correct but the led doesn't light-up

AWOL

Code: [Select]
indirizzoInt=atoi(&indirizzo);

atoi accepts a null terminated string.
You are not passing a null terminated string, except perhaps by extreme good fortune.

waski

Code: [Select]
int indirizzoInt;

void setup()  
{
 
 Serial.begin(115200);
 pinMode(indirizzoInt, OUTPUT);
//  pinMode(2, OUTPUT);
 }


This is not how you declare pins. You have to declare the pin number.

So  :

Code: [Select]
const byte LED_1  = 13 ;

...

setup()
{
pinMode( LED_1 , OUTPUT);
...
}



Then in your code you need to look for your commands  :

Code: [Select]
if ( received_command == 'LED_1_ON' )
{
digitalWrite(LED_1, HIGH);
}

if (received_command ==  ' LED_1_OFF' )
{
digitalWrite(LED_1, LOW);
}



AWOL

Code: [Select]
if ( received_command == 'LED_1_ON' )
Which seems pretty unlikely.

bongoboia

thank you, i'll try immediately to correct the code

bongoboia

ok, i've done the corrections and all works, tomorrow i'll put online the code. I only have a question, i've noticed that if i begin my serial comunication with a Serial.print, the program ignores it and the, after i insert a character, on the monitor appear two times the initial message. Is it correct thi skip of line if i start with a print or println?

PaulS


ok, i've done the corrections and all works, tomorrow i'll put online the code. I only have a question, i've noticed that if i begin my serial comunication with a Serial.print, the program ignores it and the, after i insert a character, on the monitor appear two times the initial message. Is it correct thi skip of line if i start with a print or println?

I'd suggest, then, that you need to fix your mysterious code.
The art of getting good answers lies in asking good questions.

Go Up