arduino uno Serial.read with array

Hello,
I'm not a strong programer so any help would be appreciated.

i have a Serial.read of 0 coming in, so when ever a new 0 comes in i want to move down the array.
example: when 0 comes in hit first array index and assign it to incomingByte. when next 0 comes in hit the array 2nd index and assign that to incomingByte and so on.

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
int numberToDisplay;
int incomingByte = 0;
int python[] = {1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6};

void setup() {
//set pins to output so you can control the shift register
Serial.begin(9600);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);

}

void loop() {

if (Serial.available() > 0) {
// read the incoming byte:

numberToDisplay = Serial.read()-48;

// say what you got:
Serial.print("I received: ");
Serial.println(numberToDisplay);
}

// take the latchPin low so
// the LEDs don't change while you're sending in bits:
digitalWrite(latchPin, LOW);
// shift out the bits:
shiftOut(dataPin, clockPin, MSBFIRST, incomingByte);

//take the latch pin high so the LEDs will light up:
digitalWrite(latchPin, HIGH);
// pause before next value:
delay(50);

}

int python[] = {1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6};

Are you sure that doesn't need to be long?

when ever a new 0 comes in i want to move down the array.

it? What is it that should move? What array?

when 0 comes in i want to assign the first index of the array to incomingByte, when the next 0 comes in i want to assign the second index of the array to incomingByte, and so on.

what will long do?

what will long do?

Waste twice as much memory again

when 0 comes in i want to assign the first index of the array to incomingByte, when the next 0 comes in i want to assign the second index of the array to incomingByte, and so on.

The first index of the array doesn't make sense. The first ELEMENT of the array does.

On the other hand, you still haven't said what array.

Why do you want to overwrite the value in incomingByte?

this array

int python[] = {1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6};

sorry getting python list index mixed up with array elements.

So, you need a global variable, index, initialized to 0. Each time a value arrives, use index to select the array element, and then increment index.

It is not clear if you want to store the incomingByte in the python array, or overwrite incomingByte with the value from the python array.

   ptyhon[index++] = incomingByte;

or

   incomingByte = python[index++];

if you look at this part off the code near the bottom.

shiftOut(dataPin, clockPin, MSBFIRST, incomingByte);

this incomingByte is the one that i want to be assigned an element of python[]

when a zero comes in make incomingByte element 0 when the next zero comes in make incomingByte element 1 when the third zero comes in make incomingByte element 2 and so on.

wdaniel23:
if you look at this part off the code near the bottom.

shiftOut(dataPin, clockPin, MSBFIRST, incomingByte);

this incomingByte is the one that i want to be assigned an element of python[]

when a zero comes in make incomingByte element 0 when the next zero comes in make incomingByte element 1 when the third zero comes in make incomingByte element 2 and so on.

so:

   if(incomingByte == 0)
      incomingByte = python[index++];

where in the code would i put this, does it require a loop.

where in the code would i put this

Where do you get a byte from the serial port?

does there need to be a loop. i guessing it needs to be here. see below.

void loop() {

if (Serial.available() > 0) {
// read the incoming byte:

numberToDisplay = Serial.read()-48;

int index = 0;

if(numberToDisplay == 0)
incomingByte = python[index++];

// say what you got:
Serial.print("I received: ");
Serial.println(numberToDisplay);
}

does there need to be a loop

Of course. It's right here:

void loop() {

i guessing it needs to be here.

That index variable is NOT global. If it is going to be local like that, it needs to be static.

it seems to work now Thank you Thank you.

:slight_smile: :slight_smile: :slight_smile:

wdaniel23:
it seems to work now Thank you Thank you.

:slight_smile: :slight_smile: :slight_smile:

You probably want to make sure that you don't try to access beyond the end of the python array.

how would i do that,

maybe this????

if (python[] > 24){

break;
}

wdaniel23:
maybe this????

if (python[] > 24){

break;
}

No. Something like:

   if(index < 24)
   {
      if(numberToDisplay == 0)
         incomingByte = python[index++];
   }

awesome, thanks for that it works. :slight_smile: :slight_smile: :slight_smile: