I2C - sending value

Hi Guys, I create a circuit to control several servos with several Poti via I2C. I use the standard I2C library and modify it.

The Slave Ardu expected values like:

“,2100a,” or “,1800b,”

It must be start and end with a comma.

It doesn’t work with the standard routine like this:

 Wire.beginTransmission(8); // transmit to device #8
 Wire.write(", ");        
 Wire.write(x);             
 Wire.write(",");
 Wire.endTransmission();    // stop transmitting

How can I realize , that the Master Ardu send Value in this particular form ?

Iam Thankfull for any help

It doesn't work

Something happens. "It doesn't work" doesn't tell us squat.

Wire.write() takes three forms, and none of them automatically send an int as a string.

Wire.write(value) Wire.write(string) Wire.write(data, length)

Parameters:- value: a value to send as a single byte string: a string to send as a series of bytes data: an array of data to send as bytes length: the number of bytes to transmit

You'd probably do better to pre-format your data into a char array, then send it.

This is my scheme:

The MASTER Ardu has several Potis and the SLAVE Ardu has several Servos.

Master ----> Slave

First of all I want to control with one Poti a servo. My problem is the content from the sending Data.

I want to send and receive data like this:

“,2100a,” , “,1500b,” , “,900f,”

//Master Ardu

Wire.beginTransmission(8); // transmit to device #8
     
      Wire.write(",");
      Wire.write(valNew2);        // sends five bytes
      Wire.write(",");
      
      Wire.endTransmission();    // stop transmitting
Slave-side

char x = Wire.read();    // receive byte as an integer
Serial.println(x);         // print the integer

With " char x = Wire.read(); " i receive: (C&P from S-Monitor) ,., ,@, ,U, ,j, ,m, ,n, ,j, ,L, ,/, ,),

with "int x = Wire.read();" i receive: (C&P from S-Monitor)

,(44 ,+44 ,644 ,@44 ,M44 ,Y44 ,^44

My Aim is, that the slave-ardu can receive “,2100a,” and put the PWM signal 2100 to servo a. If you have a better solution for this, please tell me

LLucas: This is my scheme:

The MASTER Ardu has several Potis and the SLAVE Ardu has several Servos.

Master ----> Slave

First of all I want to control with one Poti a servo. My problem is the content from the sending Data.

I want to send and receive data like this:

“,2100a,” , “,1500b,” , “,900f,”

Why? It doesn't seem like the simplest way. Why not something like a2100,b1500,f900 and so on... ? Then you don't have to parse all those nested quotes and commas.

That is the part of the code that it use to control Servos, the comma mark the Beginning and the end of value:

(CODE ISNT COMPLETE, but it shows how it works)

Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() 
  {
    Wire.begin(); // join i2c bus (address optional for master)
    Serial.begin(9600);
   
    myservoa.attach(6);  //the pin for the servoa control
    myservob.attach(5);  //the pin for the servob control
    myservoc.attach(8);  //the pin for the servoc control
    myservod.attach(9);  //the pin for the servod control 
    Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
  }

char c = wire.read();  //gets one byte from serial buffer
    if (c == ',') 
      {
        if (readString.length() >1) 
          {
          Serial.println(readString); //prints string to serial port out
          int n = readString.toInt();  //convert readString into a number

            // auto select appropriate value, copied from someone elses code.
            if(n >= 1)
              {
                Serial.print("writing Microseconds: ");
                Serial.println(n);
                if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
                if(readString.indexOf('b') >0) myservob.writeMicroseconds(n); 
                if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
                if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
              }
          
          readString=""; //clears variable for new input
          }
      }

@aarg how can i do it on the simple way?

the comma mark the
Beginning and the end of value:

As opposed to a '<" marking the beginning and a ‘>’ marking the end? Why use the same character for two different functions? Doing so offers NO advantage.

Putting the letter on the end makes parsing more difficult that putting in on the beginning.

Look at Robin2’s post on serial data handling. It applies just as well to Wire data handling and does NOT piss away resources on Strings.