Search code examples
binaryarduinoarduino-unoeeprom

Arduino Uno - Incorrect/Scrambled Serial Data


I have been struggling to write a very simple Arduino program that increments address pins to an EPROM and then reads in the data through other pins. When I was unable to do something as simple as increment an array of boolean values (the MSB on both values is almost always stuck at 1, too), my assumption was that my code must be messed up, but then it started sending twice as many characters as it should (Serial.println(char);) which didn't happen when sending strings. Then even worse things started happening, such as the device totally not responding when no errors were found during compile. So what I need to know is, is my code broken or is my Arduino broken? I have tried this at all baud rates, and this is some of the output I get. Notice I inserted delays in my code to slow things down, which made no change. I had to snap a screen of this because the characters wouldn't paste in a snippet. Arduino serial output

And here is my half-baked code, which you will notice is full of loops for sending single characters at a time (because this avoids some of the crazy behavior).

/**
 * EEPROM Reader/Dumper for EPROMS or EEPROMS.
 * 
 * @version 1.0.0.0
 * @author  Bit Fracture
 * @date    2015.05.23
 */

//Defining the address lines (four additional pins are manual)
int     a_pins[10]  = {2,3,4,5,6,7,8,9,10,11};

//Defining the 8-bit data pins
int     d_pins[8]   = {12,13,14,15,16,17,18,19};

//Store our current address (start at binary 0)
boolean address[10] = {0,0,0,0,0,0,0,0,0,0};

//Store our serial output data (start at binary 0)
boolean data[8]     = {0,0,0,0,0,0,0,0};

void setup()
{
  //Start communication with the computer
  Serial.begin(115200);
  delay(100);

  //Set address pins to output
  for (int i=0; i < sizeof(a_pins); i += 1)
  {
      pinMode(a_pins[i], OUTPUT);

      //Tell Serial this pin's status
      Serial.println("PO: " + (a_pins[i]));
      delay(100);
  }

  //Set data pins as input
  for (int i=0; i < sizeof(d_pins); i += 1)
  {
      pinMode(d_pins[i], INPUT);

      //Tell Serial this pin's status
      Serial.println("PI: " + (d_pins[i]));
      delay(100);
  }

  //Warn start
  Serial.println("BEGINNING TRANSMISSION");
}

void loop()
{
  //Calculate new binary address
  boolean carry = 1; //Start with a carry to initiate increment process
  for (int i=0; i < sizeof(address); i += 1)
  {
    boolean _temp = ((address[i] || carry) && (!address[i] || !carry));
    carry         = (address[i] && carry);
    address[i]    = _temp;
  }

  //Set output pins to new values
  for (int i=0; i < sizeof(a_pins); i += 1)
  {
    digitalWrite(a_pins[i], address[i]);
  }

  //Allow for the changes to propagate through the EPROM circuitry
  delay(250);

  //Read the inputs
  for (int i=0; i < sizeof(d_pins); i += 1)
  {
    data[i] = digitalRead(d_pins[i]);
  }

  //Output the address
  for (int i = sizeof(address); i > 0; i--)
  {
    if (address[i] == 1)
      Serial.print("1");
    else
      Serial.print("0");
  }
  Serial.print(": ");

  //Output the value
  for (int j = sizeof(data); j > 0; j--)
  {
    if (data[j] == 1)
      Serial.print("1");
    else
      Serial.print("0");
  }
  Serial.println();

  //Keep things from going too fast for now
  delay(1000);
}

Put simply: I need to understand why it looks like the serial data at the beginning is racing out into program memory instead of sending the string it is supposed to, and why I can't seem to do something as simple as increment a binary number and output it on serial!

Thanks everyone


Solution

  • you are sending binary numbers to the serial port and not converting them to ascii, do this small change in your setup code by converting the binary numbers to ascii,

    char my_buffer_ax[10];
    char my_buffer[200];
    
    
      memset(my_buffer, 0, 200);
      strcat(my_buffer, "PO: "); 
      //Set address pins to output
      for (int i=0; i < sizeof(a_pins); i += 1)
      {
          pinMode(a_pins[i], OUTPUT);
    
          //Tell Serial this pin's status
          memset(my_buffer_ax, 0, 10);
          itoa(a_pins[i], my_buffer_ax, 10); 
          strncat(my_buffer, my_buffer_ax, strlen(my_buffer_ax)); 
          delay(100);
      }
      Serial.println(my_buffer);
    
      memset(my_buffer, 0, 200);
      strcat(my_buffer, "PI: "); 
      //Set data pins as input
      for (int i=0; i < sizeof(d_pins); i += 1)
      {
          pinMode(d_pins[i], INPUT);
    
          //Tell Serial this pin's status
          memset(my_buffer_ax, 0, 10);
          itoa(d_pins[i], my_buffer_ax, 10); 
          strncat(my_buffer, my_buffer_ax, strlen(my_buffer_ax)); 
          delay(100);
      }
      Serial.println(my_buffer);