Sending Data via i2c, value conversion question

I’m having a bit of trouble. I’m looking to send the value of a potentiometer to an i2c LCD screen but I can’t just send it as is or I get the ASCII equivilant on the screen. Here is what I have thus far:

#include <Wire.h>  //i2c library


 int joyPin1 = 0;                 // slider variable connecetd to analog pin 0
 int value1 = 0;                  // variable to read the value from the analog pin 0
 int value2 = 0;                  // variable to read the value from the analog pin 1
 void setup() 
 {
  Serial.begin(9600);
  Wire.begin(); //initializes i2c screen
 }

 void loop() {
  Wire.beginTransmission(0x4C);
  Wire.send(0xFE);
  delay(1);
  Wire.send(0x14);
  Wire.endTransmission();
  delay(500);
  value1 = analogRead(joyPin1); 
  value2 = value1/4;
  Serial.print(value2);
  Serial.println("");
  Wire.beginTransmission(0x4C);
  Wire.send(value2);
  Wire.endTransmission();
  delay(1000);
 }

I feel I need to convert the value to another form in order for the screen to display it correctly. Any help would be greatly appreciated. Thanks!

I feel I need to convert the value to another form in order for the screen to display it correctly.

What does this code display on the screen?

but I can't just send it as is or I get the ASCII equivilant on the screen.

Can you read the value if it were displayed in its binary form?

What are you trying to do? I would expect that converting the value to a character string would be the reasonable thing to do. But, perhaps you mean something different by "the ASCII equivalent" (or don't know what that means).

Ok, for example, if I set the pot value to 104. I get a “h” on the screen. Because 104 in binary is 1101000 or in ASCII characters its the lower case letter h.

I need the screen to display 104.

I think a string may be the answer, but i can’t seem to get it working correctly.

When working on a Picaxe project previously, I worked around this by a bin to ascii conversion i.e.:

bintoascii potval , variable1, variable2, variable3

where:
variable1 = hundreds,
variable2 = tens,
variable 3 = units.

Then I could send the 3 different variables to the screen to make up my 3 digit number. Obviously this is a completely different language and environment so I need to figure out the C++ way of accomplishing this.

Here's an example of how I do it

     data[6] = 'T';
      data[7] = '2';
      data[8] = '=';
      data[9] = (int(temperature2) / 100) + 48;
      data[10] = (int(temperature2) - ((int(temperature2) / 100) * 100)) / 10 + 48;
      data[11] = (int(temperature2) - ((int(temperature2) / 100) * 100))-(((int(temperature2) - ((int(temperature2) / 100) * 100)) / 10)* 10) + 48;

Replace data[] with lcd.print
and you can remove all the int(), that's because im printing a float
the only thing is it only works for 3 chars so max number is 999

Replace data[] with lcd.print
and you can remove all the int(), that's because im printing a float
the only thing is it only works for 3 chars so max number is 999

Not quite sure what you mean here. I'm not using any type of LCD library at all, if that clears anything up.

The sprintf() function will allow you to convert an int to a string (NULL terminated array of characters) with control over the formatting. The itoa() will do the same, but with no control over the formatting (though smaller footprint).

That works for me. When I ended up doing was:

#include <Servo.h>
#include <Wire.h>  //i2c library


 int joyPin1 = 0;                 // slider variable connecetd to analog pin 0
 int value1 = 0;                  // variable to read the value from the analog pin 0
 int servoval =0; // variable to read the value from the analog pin 1
 char buf[3];
 Servo myservo1;
 
 void setup() 
 {
  Serial.begin(9600);
  Wire.begin(); //initializes i2c screen
  myservo1.attach(9);
 }

 void loop() {
   Wire.beginTransmission(0x4C);
  Wire.send(0xFE);
  delay(1);
  Wire.send(0x14);
  Wire.endTransmission();
  value1 = analogRead(joyPin1); 
  servoval=map(value1, 0, 1023, 0, 179);
  myservo1.write(servoval);
  Wire.beginTransmission(0x4C);
  Wire.send(itoa(servoval, buf, 10));
  Wire.endTransmission();
  delay(15);
 }

It took me a bit to figure out how to use the itoa() function. What I assume this does is take the individual digits of my value and convert that to a string and then the Wire.send() sends each digit seperately the way it would send “hello” as “h”, “e”, “l”, “l”, “o”. Am I correct with that?

Thanks Paul!

You are on the right track, but for this:

 char buf[3];

itoa creates a null-terminated string (so 104 would be ‘1’, ‘0’, ‘4’, 0x00). Thus you need buf to be at least 4, and preferably a bit bigger in case you ever get into the thousands.

What I assume this does is take the individual digits of my value and convert that to a string and then the Wire.send() sends each digit seperately the way it would send “hello” as “h”, “e”, “l”, “l”, “o”.

Roughly. It converts the interval value to a string, and yes, that gets sent by Wire.send() as you said.

I actually did change that to 4 after I posted that code. But thanks for the pointing that out.

Just out of curiousity, if I limited the size of the char to 4 and it did get into the thousands, would it then roll over; replacing the initial values of the string with the new "appending" values?

No, it would corrupt memory, possibly having no visible effect, possibly causing your sketch to crash. The itoa function doesn't know how long your buffer is, so it cannot know when to wrap-around.

So I assume if it overloads the char limit it just appends to the next byte which may or may not be used by some other part of the program. So the size of the char is just the number of "place holders" you have associated with that function (plus the 0x00).

Er, yes, although your use of language confuses me a bit.

char buf[3];

That allocates 3 "char" datatypes, which happen to be one byte long each, so in this case, 3 bytes.

So the size of the char is just the number of "place holders" you have associated with that function (plus the 0x00).

They aren't really associated with the function, you pass them to the function as an argument. This particular function doesn't have an argument for the size of the buffer, so it assumes it can use as much memory as it wants to.