Setting a "char" equal to a variable

Here is my code:

//Reciever

#include <Wire.h>

int num;

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(A2, OUTPUT);
  digitalWrite(A0, LOW);
  digitalWrite(A1, LOW);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(A2, LOW);
}

void loop()
/*{
  Wire.requestFrom(2, 1);    // request _ bytes from slave device #2

  while(Wire.available())    // slave may send less than requested
  { 
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);     // print the character
    num = c;
  }
  
  
  if(num == '5'){
   digitalWrite(9, HIGH); 
   digitalWrite(8, HIGH);
   digitalWrite(10, HIGH);
   digitalWrite(A2, HIGH);
   digitalWrite(11, HIGH);
   delay(1000);
  }
*/
{
  Wire.requestFrom(2, 2);    // request _ bytes from slave device #2

  while(Wire.available())    // slave may send less than requested
  { 
    char c = Wire.read(); // receive a byte as character
//    Serial.print(c);     // print the character
    num = c;
    Serial.print(num);
    
 
}

if(num == '10'){
// Serial.print(num);
 digitalWrite(3, HIGH);
 digitalWrite(4, HIGH);
 digitalWrite(9, HIGH);
 digitalWrite(7, HIGH);
 digitalWrite(A3, HIGH);
 digitalWrite(11, HIGH);
 digitalWrite(12, HIGH);
 digitalWrite(8, HIGH);
  }  
  

  
/* 
  if(num == '44'){
   digitalWrite(3, LOW); 
  }
*/

  delay(500);
}

When I serial print the variable "num", I get 4948. The number that I am actually trying to send is 10. When I serial print c, I get 10. What should I do to make the variable num be equal to char c?

In ASCII, char '0' = 48, '1' = 49, and you convert the char to an int, that's why you see 4948 in the serial monitor.

You must store the chars in a char array (C string), then convert that char array to a number, with function atoi().

Also, you must not compare a number with a character:

if ( 1 == '1' ) // false, because '1' = 49
if ( 1 == 1 ) //true

Untested example:

Wire.requestFrom(2, 2);

static uint8_t idx = 0;
static char input[3];

while(Wire.available())    // slave may send less than requested
{ 
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);     // print the character
    input[ idx++ ] = c; // add the char to the char array
}
input[ idx ] = '\0'; // terminate the char array to make it a valid C string.

if ( idx == 2 ) // if we received 2 characters
{
  num = atoi( input );
  Serial.print( num );

  if ( num == 10 ){
    ...
}

idx = 0; // reset index for next input

4948 - yep makes sense 49 is '1' in ascii and 48 is '0'

Mark

How do I code that? I am not sure what you mean. By the way, this system works fine with single digits. Here is my code for a single digit:

void loop()
{
  Wire.requestFrom(2, 1);    // request _ bytes from slave device #2

  while(Wire.available())    // slave may send less than requested
  { 
    char c = Wire.read(); // receive a byte as character
   // Serial.print(c);     // print the character
    num = c;
    Serial.print(num);
  }
  
  
  if(num == '6'){
   digitalWrite(9, HIGH); 
   digitalWrite(8, HIGH);
   digitalWrite(10, HIGH);
   digitalWrite(A2, HIGH);
   digitalWrite(11, HIGH);
   delay(1000);
  }
  delay(500);
}

It shows 54 in the serial monitor for the variable num, but when I say "if(num == '6')", it still works. It does not work if I change the input to 7 or something though. Anyway, just let me know how to code the thing you were talking about if that will work. I noticed you posted this:

if ( 1 == '1' ) // false, because '1' = 49
if ( 1 == 1 ) //true

But I still don't understand what you mean. Could you please just modify my code to work with what you are saying (I am really bad at coding, but really good at the hardware).

Oh.. I didn't see that you gave me some example code guix. It works! Thanks! I am prob going to have more questions about this code, like how to condense it, but I will ask that later once I get to that point. Thanks again!

Okay, next question. Here is the code once again (the most updated version)

//Reciever

#include <Wire.h>

int num;

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(A2, OUTPUT);
  digitalWrite(A0, LOW);
  digitalWrite(A1, LOW);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(A2, LOW);
}

void loop()
{
  Wire.requestFrom(2, 2);    // request _ bytes from slave device #2

  static uint8_t idx = 0;
static char input[3];

while(Wire.available())    // slave may send less than requested
{ 
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);     // print the character
    input[ idx++ ] = c; // add the char to the char array
}

input[ idx ] = '\0'; // terminate the char array to make it a valid C string.
idx = 0; // reset index for next input

num = atoi( input );
Serial.print( num );


if ( num == 99 ){
   digitalWrite(9, HIGH); 
   digitalWrite(8, HIGH);
   digitalWrite(10, HIGH);
   digitalWrite(A2, HIGH);
   digitalWrite(11, HIGH);
  }
  
  delay(500);
}

I have a dual 7 segment display, which is what this is all about. The way it is powered is by connecting each pin to ground, which is a lot of pins (I used an NPN transistor array, which is why HIGH is still on). When a number is recieved by the arduino (using this code), it will turn on the appropriate pins to allow for it to display that number. Would I have to go through every number, 0-99, for this to work (by manually telling it which pins to turn on for each one)? Or is there a more efficient way to go about doing this? I was thinking something like this:

If a number (i.e. 5) is in the ones place, turn on these pins. If 5 is in the tens place, turn these pins on. This way, I only have to account for 20 different things. (0-9 for the ones place, and 0-9 for the tens place). So if I put in, for example, 89, it would see the first digit is 8, turning on those pins, and the second digit is 9, so it would turn on those pins. How would I do this?

Have you looked at this:

http://playground.arduino.cc/Main/SevenSegmentLibrary

Ok so you don't really need that num variable. You can do it in an easier way, like so (again, untested, just giving ideas):

// your pins for each segments
const uint8_t segmentsPins[][7] =
{
  // A   B   C   D   E   F   G (?)
  { A0, A1,  2,  3,  4,  5,  6 },
  {  7,  8,  9, 10, 11, 12, A2 }
};

// each bit set to 1 represent one segment to be turned on
const uint8_t segmentsData[] =
{
  // ABCDEFG
  0b01111110, // 0
  0b00110000, // 1
  0b01101101, // 2
  0b01111001, // 3
  0b00110011, // 4
  0b01011011, // 5
  0b01011111, // 6
  0b01110000, // 7
  0b01111111, // 8
  0b01111011  // 9
};

void loop()
{
  Wire.requestFrom(2, 2);

  if ( Wire.available() == 2 )
  {
    displayChar( 0, Wire.read() );
    displayChar( 1, Wire.read() );
  }
}

// id is the 7seg to be drawn, either 0 or 1 
// chr is the character to display 
void displayChar( uint8_t id, char chr )
{
  if ( id == 0 || id == 1 )
  {
    if ( chr >= '0' && chr <= '9' )
    {
      uint8_t digit = chr - '0'; // substracting character '0' from a digit character, will make it a number
      uint8_t segData = segmentsData[ digit ]; // get the segments data for this digit

      // turn each segment on or off
      for ( uint8_t i = 0; i < 7; i++ )
      {
        digitalWrite( segmentsPins[ id ][ i ], segData & (1 << (6-i)) ? HIGH : LOW );
      }
    }
  }
}

Something like that... or yes, try that library, should work too :stuck_out_tongue: