Arduino help

I built an arduino to run a 7 segment LED. I use the serial port to feed it data or in this case, numbers to be stored in an array and displayed on the LED. I’m a tad lost here. I’ve included the code below. Any pointers would be nice. :slight_smile:

#include <string.h>

int LED_G = 2;
int LED_E = 3;
int LED_D = 4;
int LED_A = 5;
int LED_B = 6;
int LED_C = 7;
int LED_F = 8;
int i = 0;
int incomingByte = 0;
int bufsize = 0;
char buf[32];

void setup(){
  pinMode(LED_A, OUTPUT);
  pinMode(LED_B, OUTPUT);
  pinMode(LED_C, OUTPUT);
  pinMode(LED_D, OUTPUT);
  pinMode(LED_E, OUTPUT);
  pinMode(LED_F, OUTPUT);
  pinMode(LED_G, OUTPUT);
  Serial.begin(9600);
  delay(5000);
}

void loop(void) {
  incomingByte = Serial.read();
  
  if (incomingByte > 0) {
    buf[i] = incomingByte; //Read incoming data via serial port and put it in to an array
    ++bufsize; 
    ++i;
    if (incomingByte == 48) { LED_0; } //If incomingbyte equals 0, Make the 7 segment LED display 0
    if (incomingByte == 49) { LED_1; }
    if (incomingByte == 50) { LED_2; }
    if (incomingByte == 51) { LED_3; }
    if (incomingByte == 52) { LED_4; }
    if (incomingByte == 53) { LED_5; }
    if (incomingByte == 54) { LED_6; }
    if (incomingByte == 55) { LED_7; }
    if (incomingByte == 56) { LED_8; }
    if (incomingByte == 57) { LED_9; }
    delay(100);
    Clear_LED();
    delay(100);
  } else {
    delay(1000);
    RunLED();
  }
  delay(1000);
}

void RunLED() { //Run the stored sequence in buf[]
    int i;
    for (i=0; i<bufsize - 1; ++i){
      Clear_LED();
      if (buf[i] == 48) { LED_0; }
      if (buf[i] == 49) { LED_1; }
      if (buf[i] == 50) { LED_2; }
      if (buf[i] == 51) { LED_3; }
      if (buf[i] == 52) { LED_4; }
      if (buf[i] == 53) { LED_5; }
      if (buf[i] == 54) { LED_6; }
      if (buf[i] == 55) { LED_7; }
      if (buf[i] == 56) { LED_8; }
      if (buf[i] == 57) { LED_9; }
      delay(1000);
    }
}

void Clear_LED(){
  digitalWrite(LED_A, LOW);
  digitalWrite(LED_B, LOW);
  digitalWrite(LED_C, LOW);
  digitalWrite(LED_D, LOW);
  digitalWrite(LED_E, LOW);
  digitalWrite(LED_F, LOW);
  digitalWrite(LED_G, LOW);
}

void LED_0(){
  Clear_LED();
  digitalWrite(LED_A, HIGH);
  digitalWrite(LED_B, HIGH);
  digitalWrite(LED_C, HIGH);
  digitalWrite(LED_D, HIGH);
  digitalWrite(LED_E, HIGH);
  digitalWrite(LED_F, HIGH);
}
void LED_1(){
  Clear_LED();
  digitalWrite(LED_B, HIGH);
  digitalWrite(LED_C, HIGH);
}
void LED_2(){
  Clear_LED();
  digitalWrite(LED_A, HIGH);
  digitalWrite(LED_B, HIGH);
  digitalWrite(LED_G, HIGH);
  digitalWrite(LED_E, HIGH);
  digitalWrite(LED_E, HIGH);
  digitalWrite(LED_D, HIGH);
}
void LED_3(){
  Clear_LED();
  digitalWrite(LED_A, HIGH);
  digitalWrite(LED_B, HIGH);
  digitalWrite(LED_G, HIGH);
  digitalWrite(LED_C, HIGH);
  digitalWrite(LED_D, HIGH);
}
void LED_4(){
  Clear_LED();
  digitalWrite(LED_F, HIGH);
  digitalWrite(LED_G, HIGH);
  digitalWrite(LED_B, HIGH);
  digitalWrite(LED_C, HIGH);
}
void LED_5(){
  Clear_LED();
  digitalWrite(LED_A, HIGH);
  digitalWrite(LED_F, HIGH);
  digitalWrite(LED_G, HIGH);
  digitalWrite(LED_C, HIGH);
  digitalWrite(LED_D, HIGH);
}
void LED_6(){
  Clear_LED();
  digitalWrite(LED_A, HIGH);
  digitalWrite(LED_F, HIGH);
  digitalWrite(LED_E, HIGH);
  digitalWrite(LED_D, HIGH);
  digitalWrite(LED_C, HIGH);
  digitalWrite(LED_G, HIGH);
}
void LED_7(){
  Clear_LED();
  digitalWrite(LED_A, HIGH);
  digitalWrite(LED_B, HIGH);
  digitalWrite(LED_C, HIGH);
}
void LED_8(){
  Clear_LED();
  digitalWrite(LED_A, HIGH);
  digitalWrite(LED_B, HIGH);
  digitalWrite(LED_C, HIGH);
  digitalWrite(LED_D, HIGH);
  digitalWrite(LED_E, HIGH);
  digitalWrite(LED_F, HIGH);
  digitalWrite(LED_G, HIGH);
}
void LED_9(){
  Clear_LED();
  digitalWrite(LED_A, HIGH);
  digitalWrite(LED_B, HIGH);
  digitalWrite(LED_C, HIGH);
  digitalWrite(LED_G, HIGH);
  digitalWrite(LED_F, HIGH);
}

Your code actually looks pretty close to what you describe you want.

For one thing, your code reads data from the serial port even if there is no data there. You should get used to coding

  if (Serial.available()) {
    incomingByte = Serial.read();
  }

It looks like you found that incomingByte was 0 when there is no serial data available, but the correct way to determine that there is serial data waiting to be read is to use the test above. I recommend you replace the two lines

  incomingByte = Serial.read();
  
  if (incomingByte > 0) {

with

  if (Serial.available()) {
    incomingByte = Serial.read();

and continue with the logic below there when there is incoming serial data.

Also, within loop() and the RunLED() function, when you call your LED_0 through LED_9 functions, you will need to add the () after the function name. So

    if (incomingByte == 48) { LED_0; }

needs to be    if (incomingByte == 48) { LED_0[glow]()[/glow]; } and so on.

Finally, I presume you press the Arduino's RESET button before you enter another sequence of numbers. You never reset the bufsize variable to the beginning of buf[], which is Ok if you don't enter another set of numbers after the first set displays on the LED.

Other than that, is there a specific problem you are having with your code?

Sweet! I totaly forgot the (). It works perfect now. The problem was it wasn't displaying the numbers.

Thank you for the help!

Cool. Thanks for closing the loop on the problem. We'll consider it closed then.

New problem. After I upload the number sequence and close the com port, it stops rotating the number sequence. Any ideas?

New problem. After I upload the number sequence and close the com port, it stops rotating the number sequence. Any ideas?

Since the value to display comes from the com port, why does the number staying steady when the com port is closed surprise you?

i store the numbers in “buf” in an array so when the port closes it will display the numbers stored their.

Have you noticed that opening and closing the serial port causes the Arduino to reset?

No I didn’t. How would I store the values so that a reset wouldn’t affect them?

In EEPROM would be the easiest. http://www.arduino.cc/en/Reference/EEPROM