Convert char (array of chars) to int

Hi,
I have 2 arduinos which change data using I2C. Actually the slave measure a rpm and send it to the master.

I need some help to convert chars i get on master into an integer value for being able to do something with it.

This is my code

slave:

// Slave rpm 

#include <Wire.h>

unsigned char pin_in = A0;
unsigned long val0, val1, val;
int rpm ;
char b[4] ="";

void setup() {
  pinMode(pin_in, INPUT);
  Wire.begin(10);                // join i2c bus with address #10
  Wire.onRequest(requestEvent); // register event
}

void loop() {
  val0 =  pulseIn(pin_in, LOW,1000000);
  val1 =  pulseIn(pin_in, HIGH,1000000);
  val = val0+val1;
  if (val==0){
    rpm = 0;
  }
  else {
    rpm = 60000000 / val / 4;
  }
  sprintf(b, "%04d", rpm);
  delay(50);
}

void requestEvent() {
  Wire.write(b); // respond with message of 4 bytes as expected by master
}

master

#include <Wire.h>

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

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

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

  delay(500);
}

So what i want is to get that “c” as an integer value

Thanks

Char is a signed data type of 8 bits representing values from -128 to 127.

Int is a signed data type of 16 bits representing values from -32,768 to 32,767.

Simply casting your char as an int should work.

you need to collect the chars you recieve here:

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

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

  delay(500);
}

into a buffer that can be then interpreted… sort of like this:

Wire.requestFrom(10, 4);
delay(20);
if (Wire.available())
{
  char myBuffer[16] = "";
  byte index = 0;
  while (Wire.available() and index < sizeof(myBuffer) - 1)
  {
    myBuffer[index++] = Wire.read();
    myBuffer[index] = '\0';
  }
  Serial.println(myBuffer);
  // then convert to a number....
}

You have a four character entity to convert to an integer. First read the characters into an array. Process the array by subtracting the ASCII value of zero from each character to get its integer value.

Each value should be multiplied by 1, 10, 100 and 1000 and summed to build the integer value.

void setup() {

  Serial.begin(9600);  // start serial for output

  char *str = "3120";   // string representing rpm
  int rpm = 0;          // rpm value as int
  int mult = 1;         // multiplier

  // process each digit
  for (int i = 3; i >= 0; i--)
  {

    // add this value to rpm
    rpm += (str[i] - '0') * mult;

    mult *= 10; // mult is used to get ones, tens, hundreds and thousands

  }

  Serial.print("rmp = "); Serial.println(rpm);

}

void loop() {

}

Looking further into your code:

int rpm ;

starts out as an int…

sprintf(b, "%04d", rpm);

gets stored as char b, a cstring…

void requestEvent() {
  Wire.write(b);

sent across the bus…

while (Wire.available()) {
    char c = Wire.read();

read as char c and finally…

Serial.println(c);

printed to the serial port.

My questions are, why not…

Wire.write(rpm);

which will send 2 bytes, as far as I know…

and have the master read the bytes into an int c?

byte bufferSize = 3;
byte buffer[bufferSize];
byte index = 0;
while (Wire.available() && index < bufferSize) {
buffer[index++] = Wire.read();
}
int c = buffer[0] | buffer[1] << 8;

which will send 2 bytes, as far as I know...

It will not. It will write one byte, because the function expects the argument to be a byte.

"Actually the slave measure a rpm and send it to the master."

That makes it sound like a regular asynchronous transfer. The slave can only send data when the master has started a transfer and is providing the clock signal. I can't tell if the code that was posted is following that. Nick Gammon has example of master/receiver code here http://www.gammon.com.au/i2c