New to arduino, and need some help with my logic

OK, going to try this again. I’m using I2C and trying to write something to an LCD.

I’ve got that set up and it’s writing to it, but I’m not getting what I want to get and I am not sure where to go next.

I’m having trouble converting it from hex to ascii and would love any help whatsoever. i’m just trying to increment the count everytime the led blinks…nothing crazy.

here’s the code:
to convert to ascii

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

this is the code for the outputting. instead of having it increment by one and write it on the lcd, it is just outputting “00512” and staying there. no incrementing or anything.

int myArray[5];
  int digit, myNumber, x;
  do
  {    
    myArray[5] = 00000;
    myArray[5]++;
    for(x = 0; x < 5; x++)
    {
      digit = myNumber;
      digit = digit % 10;
      myNumber = myNumber / 10;
      myArray[x] = convert(digit);
    }
  }while(LED == HIGH);

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();

why not just us the LiquidCrystal_I2C library?

int myArray[5];Declare an array of ints with 5 elements numbered 0 to 4
    myArray[5] = 00000;Write 00000, more commonly known as 0, to the sixth element of the 5 element array
    myArray[5]++;Increment the sixth element of the 5 element array.

As the whole program had not been posted I stopped trying to understand what you were trying to do.

BulldogLowell: why not just us the LiquidCrystal_I2C library?

the guy I'm doing it for wants me to use the wire library. I've asked the same question.

UKHeliBob:
int myArray[5];Declare an array of ints with 5 elements numbered 0 to 4
    myArray[5] = 00000;Write 00000, more commonly known as 0, to the sixth element of the 5 element array
    myArray[5]++;Increment the sixth element of the 5 element array.

As the whole program had not been posted I stopped trying to understand what you were trying to do.

Okay, here’s my whole code…keep in mind, I am a novice here. I’m just trying to blink the LED and increment it when it blinks using the wire library.

#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
  
  
  
  //// **** 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(10);
  digitalWrite(LED, LOW);
  delay(10);
  
  int myArray[5];
  int digit, myNumber, x;
  do
  {    
    myArray[6] = 00000;
    myArray[6]++;
    for(x = 0; x < 5; x++)
    {
      digit = myNumber;
      digit = digit % 10;
      myNumber = myNumber / 10;
      myArray[x] = convert(digit);
    }
  }while(LED == HIGH);
  
  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);

}
void loop()
{
  // Blinking LED
  digitalWrite(LED, HIGH);
  delay(10);
  digitalWrite(LED, LOW);
  delay(10);
  
  int myArray[5];                                        [color=red]// initialize an array with 5 elements[/color]
  int digit, myNumber, x;
  do
  {    
    myArray[6] = 00000;                         [color=red]// Keep writing 0 to the 7th element of a 5 element array[/color]
    myArray[6]++;
    for(x = 0; x < 5; x++)
    {
      digit = myNumber;
      digit = digit % 10;
      myNumber = myNumber / 10;
      myArray[x] = convert(digit);
    }
  }while(LED == HIGH);                       [color=red]//  You're going to stay in this do loop until LED goes back to HIGH[/color]
  
  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);

}

The first big problem is stepping on memory you don’t own. That’s going to cause pretty unpredictable effects. You can’t write to the 7th element of a 5 element array. The legal indices for that array are 0 through 4. 5 is too much. 6 is right out.

The last problem is the do loop. It’s going to keep doing that loop over and over and over again until eternity or until someone puts a line of code into it that somewhere sets LED = HIGH. Right now LED is set to the pin number of the LED, which is 13. I can’t imagine why you would want to change the pin number to HIGH, but that’s up to you. I think that will actually end up being pin 1.

The light will flash once before you enter that do loop. But then you’re trapped there forever.

OK, changed that…As for the LED, everytime the LED comes on, I want to increment my count, starting at zero.

I did this:

pinMode(LED, OUTPUT);  // Sets digital pin as output

and then this:

// Blinking LED
  digitalWrite(LED, HIGH);
  delay(1000);
  digitalWrite(LED, LOW);
  delay(1000);

as you saw in the earlier code, I set the LED connected to digital pin 13 as the output and now it blinks on the delay I set it as.

As for the do/while loop, I took it away and replaced it with just a while and now I’m getting “|| || || ||” as my output. No idea why I’m getting those bars. My hex to ascii conversion code works when I plug in a number 0-9 so I think it’s something with my for loop and array working. Here’s what I have now:

int myArray[5];
  int digit, myNumber, x;
  while(LED == HIGH)
  {    
    myArray[4] = 00000;
    myArray[4]++;
    for(x = 0; x < 5; x++)
    {
      digit = myNumber;
      digit = digit % 10;
      myNumber = myNumber / 10;
      myArray[x] = convert(x);
    }
  }

here is my hex to ascii conversion code in case anyone wants to look at it:

char convert(int a) // This converts Hex to ASCII. 
{
  return a + '0';
}

OK, changed that..

So post the whole program as it is now. Do not do it by updating your original post as that will make nonsense of the thread.

char convert(int a) // This converts Hex to ASCII.

At best, it converts a single digit of BCD to ASCII - what about A through F ?

UKHeliBob:

OK, changed that…

So post the whole program as it is now. Do not do it by updating your original post as that will make nonsense of the thread.

sorry for the delay, went out of town for a bit:

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

char convert(int a) // This converts Hex to ASCII. 
{
  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);
  
  int myArray[5];
  int digit, myNumber, x;
  while(LED == HIGH)
  {    
    myArray[4] = 00000;
    myArray[4]++;
    for(x = 0; x < 5; x++)
    {
      digit = myNumber;
      digit = digit % 10;
      myNumber = myNumber / 10;
      myArray[x] = convert(x);
    }
  }
  
  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);

}

AWOL: char convert(int a) // This converts Hex to ASCII.

At best, it converts a single digit of BCD to ASCII - what about A through F ?

yeah, I hear you. I was thinking I could run through the array cell by cell and do the single digits that way.

  while(LED == HIGH)LED is a const int variable with a value of 13. HIGH has a value of 1
When will 13 equal 1 ?

I assume that you meant to read the value of the LED pin to ascertain whether it is HIGH or LOW and then act accordingly, but you never read it, only write to it. However, had you read it then it would always have been LOW anyway because you set it that way a few lines above.

    myArray[4] = 00000;
    myArray[4]++;

I still don’t understand what these lines are supposed to do.

UKHeliBob:   while(LED == HIGH)LED is a const int variable with a value of 13. HIGH has a value of 1 When will 13 equal 1 ?

I assume that you meant to read the value of the LED pin to ascertain whether it is HIGH or LOW and then act accordingly, but you never read it, only write to it. However, had you read it then it would always have been LOW anyway because you set it that way a few lines above.

    myArray[4] = 00000;
    myArray[4]++;

I still don't understand what these lines are supposed to do.

as to part one - thanks for pointing that out. that makes sense. Appreciate it a ton.

for the 2nd part, I was just trying to set the array to zero and then increment it from there. I didn't know if setting it to "00000" would be any different than "0". and as it increments, it counts up and displays on the lcd.

You're only setting the fifth element myArray[4] to 00000 which could much more concisely be written as 0. I think that you think you're setting all the elements to 0, but you're not. Just the last one. Elements 0 through 3 aren't being touched.

Delta_G: You're only setting the fifth element myArray[4] to 00000 which could much more concisely be written as 0. I think that you think you're setting all the elements to 0, but you're not. Just the last one. Elements 0 through 3 aren't being touched.

Good catch, thanks. Now i'm getting "43210" on the lcd and it's not incrementing or anything.

Holland_Hill:

Delta_G:
You’re only setting the fifth element myArray[4] to 00000 which could much more concisely be written as 0. I think that you think you’re setting all the elements to 0, but you’re not. Just the last one. Elements 0 through 3 aren’t being touched.

Good catch, thanks. Now i’m getting “43210” on the lcd and it’s not incrementing or anything.

It’s a good idea to continue to update your code, so your latest revisions can be seen.

the bracketed number is the number of elements in the array

to initialize an array of five elements and to fill the array:

int myArray[5] = {10,6,35,7,42};

if you printed it:

for (int i = 0; i <5; i++);
{
Serial.print("Value "); Serial.print(i); Serial.print(" = "); Serial.println(myArray[i]);
}

will output :

Value 0 = 10
Value 1 = 6
Value 2 = 35
Value 3 = 7
Value 4 = 42

note the firs element of the array is in position 0 not 1

Good catch, thanks.

Well, you were told about it some time ago.

    myArray[5] = 00000;Write 00000, more commonly known as 0, to the sixth element of the 5 element array

BulldogLowell:

Holland_Hill:

Delta_G:
You’re only setting the fifth element myArray[4] to 00000 which could much more concisely be written as 0. I think that you think you’re setting all the elements to 0, but you’re not. Just the last one. Elements 0 through 3 aren’t being touched.

Good catch, thanks. Now i’m getting “43210” on the lcd and it’s not incrementing or anything.

It’s a good idea to continue to update your code, so your latest revisions can be seen.

the bracketed number is the number of elements in the array

to initialize an array of five elements and to fill the array:

int myArray[5] = {10,6,35,7,42};

if you printed it:

for (int i = 0; i <5; i++);

{
Serial.print("Value “); Serial.print(i); Serial.print(” = "); Serial.println(myArray[i]);
}




will output :

Value 0 = 10
Value 1 = 6
Value 2 = 35
Value 3 = 7
Value 4 = 42

note the firs element of the array is in position 0 not 1

changed up the code (posted below) and getting the output of “00000” but can’t get it to increment and output the new numbers.

here’s new code

void loop()
{
  // Blinking LED
  digitalWrite(LED, HIGH);
  delay(10);
  digitalWrite(LED, LOW);
  delay(10);
  
  int myArray[5];
  int digit, myNumber;
  do
{
    // sets all array cells to zero
    myArray[4] = 0;
    myArray[3] = 0;
    myArray[2] = 0;
    myArray[1] = 0;
    myArray[0] = 0;
    
    // for loop
    for(int x = 0; x < 5; x++)
    {
      
      myArray[x] = convert(myArray[x]);
    }
    
    // prints the zeros  
    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);
    
    // increment the zeros
    myArray[4]++, myArray[3]++, myArray[2]++, myArray[1]++, myArray[0]++;
    

    
    // for loop
    for(int x = 0; x < 5; x++)
    {
      myArray[x] = convert(myArray[x]);
    }
    
    // print new numbers
    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);
}while(LED == 1);

}