Hello

Im building a project for school. Thats a robot. And we want to drive through the whole school by TCP/IP (client to server with Visual Basic). We got a Visual basic program running and now we want to sent a value to the arduino and split it in an array. I have the following code so far, just to make it a bit basic for me and if this works i will expand it with analog values.

``````char array1[4];
int i;

void setup()
{
Serial.begin(9600);
pinMode(8, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}

void loop(){

if (Serial.available() > 0) {
for (i = 0; i < 4; i++) {
}

Serial.print(array1[0]);
Serial.print(array1[1]);
Serial.print(array1[2]);
Serial.print(array1[3]);
Serial.println();

if(array1[0] = 1){
digitalWrite(8,1);
}else{
digitalWrite(8,0);
}

if(array1[1] = 1){
digitalWrite(11,1);
}else{
digitalWrite(11,0);
}

if(array1[2] = 1){
digitalWrite(12,1);
}else{
digitalWrite(12,0);
}

if(array1[3] = 1){
digitalWrite(13,1);
}else{
digitalWrite(13,0);
}

array1[0] = 0;
array1[1] = 0;
array1[2] = 0;
array1[3] = 0;
}

}
``````

So now the big question. Why doesn’t this work?
If i change the array to int the values will change in 48 and 49 (48 if 0 is sent and 49 if 1 is sent).

Thanks for helping

``````if (Serial.available() > 0) {
for (i = 0; i < 4; i++) {
}
``````

No, no, NO!

`````` if(array1[0] = 1){
``````

Yet another “NO!

Why doesn’t this work?

It does work, for some definitions of “work”.
But you didn’t tell us what your definition is.

I want to sent a simple command, like 1100. If i sent that command i want to turn on 2 LED’s and the other 2 have to be turned off.

I don’t see what this has got to do with analogWrite.

As i already explained. I want to keep it basic for now. As soon as this works ill be adding analogWrite, since thats not too hard.

``````if (x > 120) digitalWrite(LEDpin, HIGH);

if (x > 120)
digitalWrite(LEDpin, HIGH);

if (x > 120){ digitalWrite(LEDpin, HIGH); }

if (x > 120){
digitalWrite(LEDpin1, HIGH);
digitalWrite(LEDpin2, HIGH);
}                                 // all are correct
``````

I don't see your problem with that but OK.

I don't see your problem with that but OK.

You're right. I should remove analogWrite. You got any solution for me so it could work?

I did but i can't see how that would help me since i do everything as they say i have to.

I pointed out two very obvious bugs.

You check to see if there is at least one character available, then go ahead and read all four of them.

Equality is tested by "==", not "="

…silly me. You’re right that would never work.
so now i’ve changed it but it still doesn’t work.
My code is now:

``````char array1[4];
int i;

void setup()
{
Serial.begin(9600);
pinMode(8, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}

void loop(){

if (Serial.available() > 3) {
for (i = 0; i < 4; i++) {
}
Serial.print(array1[0]);
Serial.print(array1[1]);
Serial.print(array1[2]);
Serial.print(array1[3]);
Serial.println();
}

digitalWrite(8, array1[0]);
digitalWrite(11, array1[1]);
digitalWrite(12, array1[2]);
digitalWrite(13, array1[3]);
}
``````
``````    for (i = 0; i < 4; i++) {
}
``````

Read the data in a loop.

``````    Serial.print(array1[0]);
Serial.print(array1[1]);
Serial.print(array1[2]);
Serial.print(array1[3]);
``````

Print it the hard way.

Why?

``````  digitalWrite(8, array1[0]);
digitalWrite(11, array1[1]);
digitalWrite(12, array1[2]);
digitalWrite(13, array1[3]);
``````

Suppose you sent “1100” (what follows after the text you entered depends on the options selected on the serial monitor, if that is what you are using).

So, then, you call digitalWrite() with a pin number and the values 49, 49, 48 and 48. What do you expect digitalWrite() to do?

I now declared the array as a char so i will get the same values as i put in the serial monitor so that will be a 1 and a 0. So basicly if i fill in the serial monitor 0101, the first and the third LED will turn on(thats the theory...) Instead, if i sent the value 0101 all the LED's turn on and i can't turn them off again.

Instead, if i sent the value 0101 all the LED’s turn on and i can’t turn them off again.

But you’re almost certainly not sending 0101, you’re sending “0101”, which is 0x30 0x31 0x30 0x31, which is what PaulS was pointing out to you.

Ah i see. Got it working now. Thanks guys