Problem with code in I2C communications

New to the forum, but a friend who used it a lot told me this was a good place to go, so…

using a mega 2560 and setting up an I2C communications and attempting to write to an LCD (newhaven display: http://www.newhavendisplay.com/specs/NH … BBW-V3.pdf) from the arduino.

I’m writing to the LCD fine and I have the I2C set up fine. What I’m trying to do now is blink the LED (doing this, obviously easy) and every time it blinks, increment a counter and then output the counter on the 2nd row on the LCD. This should be easier than it has been for me and I cannot get the correct output with the count. It outputs “00512” instead of starting at “00000” and counting upwards (I used a an array of [5])…So, I know this is probably simple, but I cannot get it to work, so I figured I’d post here.

Here’s the code:

#include<Wire.h>
const int LED = 13;

char convert(int a)
{
  return a + '0';
}

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  TWBR = 720; // This slows down the I2C bus to 10 kHz
  
  pinMode(LED, OUTPUT);
  
  //// **** This is how to control backlight brightness **** ////
  // Goes from 1-8
  Wire.beginTransmission(0x28);
  Wire.write(0xFE);
  Wire.write(0x53);
  Wire.write(3);
  Wire.endTransmission();
  
  
  // Beginning of program
  Wire.beginTransmission(0x28);
  Wire.write(0xFE);
  Wire.write(0x45); // Sets cursor
  Wire.write(0x00); // Sets cursor on line one
  Wire.endTransmission();
  delay(250);
  
  Wire.beginTransmission(0x28);
  Wire.write("Atlas Jackplate ");
  Wire.endTransmission();
  delay(2000);
  
  Wire.beginTransmission(0x28);
  Wire.write(0xFE);
  Wire.write(0x45); // Sets cursor
  Wire.write(0x40); // Sets cursor on line two
  Wire.endTransmission();
  delay(250);
  
  Wire.beginTransmission(0x28);
  Wire.write("Tester V.1.0    ");
  Wire.endTransmission();
  delay(2000);
  
  // clear screen
  Wire.beginTransmission(0x28);
  Wire.write(0xFE);
  Wire.write(0x51);
  Wire.endTransmission();
  delay(5000);
  
  Wire.beginTransmission(0x28);
  Wire.write("The count is: ");
  Wire.endTransmission();
  delay(2000);  
}

void loop()
{
  // Blinking LED
  digitalWrite(LED, HIGH);
  delay(1000);
  digitalWrite(LED, LOW);
  delay(1000);
  
  // While loop to increment the counter while LED is on
  /*while(LED == HIGH)
  {
    int i;
    i++;
  }*/
  
  int myArray[5];
  int digit, myNumber;
  do
  {    
    myArray[5] = 0;
    myArray[5]++;
    for(int x = 0; x < 5; x++)
    {
      digit = myNumber;
      digit = digit % 10;
      myNumber = myNumber / 10;
      myArray[x] = convert(digit);
    }
  }while(LED == HIGH);
  
  /*char myArray[5]; // This array holds up to 5 digits
  int digit, myNumber;
  for(int x = 0; x < 5; x++)
  {
    digit = myNumber;
    digit = digit % 10;
    myNumber = myNumber / 10;
    myArray[x] = convert(digit);
  }*/
  
  Wire.beginTransmission(0x28);
  Wire.write(0xFE);
  Wire.write(0x45); // Sets cursor
  Wire.write(0x40); // Sets cursor on line two
  Wire.endTransmission();
  
  Wire.beginTransmission(0x28);
  Wire.write(myArray[4]);
  Wire.write(myArray[3]);
  Wire.write(myArray[2]);
  Wire.write(myArray[1]);
  Wire.write(myArray[0]);
  Wire.endTransmission();
  delay(1000);

}

These lines:

    myArray[5] = 0;
    myArray[5]++;

don’t do what you think they do. Check them out.

Also, look at your do/while. What is the state of LED when the do is entered?

  • Chip