need some help communicating with my Serial

Well here is the code I am using:

int incomingByte[10] = {0};
const int ledPin[10] = {4,5,6,7,8,9,10,11,12,13};
int outputpin[10] = {0};
const int ledCount = 10;

void setup() {

Serial.begin(9600); // opens serial port, sets data rate to 9600 bps

for (int thisLED = 0; thisLED < ledCount; thisLED++)
{
pinMode(ledPin[thisLED], OUTPUT);
}

}

void loop() {

for (int y = 0; y < 10; y++)
{
Serial.write(outputpin[y]);
}
delay(50);

if (Serial.available()) {

for (int x = 0; x < 10; x++)
{
incomingByte[x] = Serial.read();
}

for (int x = 0; x < 10; x++)
{
Serial.println(incomingByte[x]);
}

for (int i = 4; i < 13; i++)
{
for (int x = 0; x < 10; x++)
{
digitalWrite(ledPin*, incomingByte[x]);*

  • }*
  • }*
    }
    }
    I am communicating with the arduino via a USB port and LabVIEW all I am doing with my labVIEW code is sending a 1 or 0 to the ports that have LEDs on them to turn on and off an LED right now with this code 6 of my ten leds turn on and blink a couple of times if anyone can debug my code a bit I would appreciate it a ton:)
    Thanks,
    Harold Timmis
Serial.write(outputpin[y]);

I don't see where the outputpin array has been fully initialised so presumably this command transmits 0 followed by 9 random bytes.

The other thing is when you do:-
if (Serial.available())
then you go on to read 20 bytes. Serial.available() will tell you only if a byte is available in the buffer not 20 of them. If you don't want to skip all those reads you must wait until you have all the bytes in something like:-
while( Serial.available() <19) { } // do nothing until there are 20 bytes read in.

From Serial.read() documentation:

Returns

the first byte of incoming serial data available (or -1 if no data is available) - int

so you're probably putting -1 into many of those array cells.

where the outputpin array has been fully initialised

In fact, the array is initialized to all zeros (see Footnote) and its contents are never changed, so ten zero bytes are sent out every time through the loop.

Regards,

Dave

Footnote:
In C (and C++) if the number of initializers in a list is smaller than the number of elements in the array the rest of the elements are always initialized to zero, so his statement initializes all to zero. What is not clear to me is what the heck he intended to do with the array since the contents are never changed. (But I said that already.)

Well I did a change with no luck:

I am sending to the serial a array on Boolean through my other program so that should be initialized into my program through Serial.Write() but I am guessing it is not as I am returning a value of -1 in my read function.

When I write this code out into the long version as in with out the for loops and I call each one of these values I get the correct result I am able to turn off and on each of the LEDs on the Bargraph .

here is the code:

int incomingByte[10] = {0};
const int ledPin[10] = {4,5,6,7,8,9,10,11,12,13};
int outputpin[10] = {0};
const int ledCount = 10;

void setup() {

Serial.begin(9600); // opens serial port, sets data rate to 9600 bps

for (int thisLED = 0; thisLED < ledCount; thisLED++)
{
pinMode(ledPin[thisLED], OUTPUT);
}

}

void loop() {

for (int y = 0; y < 10; y++)
{
Serial.write(outputpin[y]);
delay(20);
}
delay(50);

while( Serial.available() < 19) {

for (int x = 0; x < 10; x++)
{
incomingByte[x] = Serial.read();
delay(20);
}

for (int x = 0; x < 10; x++)
{
Serial.println(incomingByte[x]);
delay(20);
}

}

for (int i = 4; i < 13; i++)
{
for (int x = 0; x < 10; x++)
{
digitalWrite(ledPin*, incomingByte[x]);*

  • delay(20);*
  • }*
  • }*
    }

Right first off the next code you post better be posted using the # icon.

Secondly what is the point of me telling you what to do if you don't do it.
I said:-

while( Serial.available() <19) { } // do nothing until there are 20 bytes read in.

you have done:-

while( Serial.available() < 19) {

You have missed out the final } and as a consequence you program is stuck in that while statement forever because it is waiting to get 20 bytes in the serial buffer but you are reading them out faster than the come in.