title: How to transmit the value of a variable from one arduino to another
body: <p>I'm setting up a touchscreen interface to control a stepper motor on a micro-injection molding machine I'm building. I've been able to control most of the switches/buttons using push button controls, but I want to try using a touchscreen to select a value for both the motor rotation speed, as well as the number of rotations the motor should complete. No need for an initiate or direction function as those are controlled by the Master arduino via pushbuttons.</p>
  
  <p>What I'm trying to do:</p>
  
  <ol>
  <li>Master: Request value of variable "CV_Num_Rotations_Output" held in slave</li>
  <li>Slave: Send value</li>
  <li><p>Master: Write value to "CV_Num_Rotations"</p></li>
  <li><p>Master: Request value of variable "CV_Speed_Rotations_Output" held in slave</p></li>
  <li>Slave: send value</li>
  <li>Master: Write received value to "CV_Speed_rotations"</li>
  </ol>
  
  <p>After looking at the types of data that can be transmitted via I2C, I've learned that it's not easy to send floats, so I've converted the two variables to whole integers which will be interpreted later on by the Master.</p>
  
  <p>I've also played around with the idea of using struct to hold the values, but since I'm just transmitting numbers and already have the conversion functions written, I'd rather just stick to overwriting the variables with their new values once the Slave sends them over.</p>
  
  <p>Right now I'm using int for the two variables on both Master/Slave:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>int CV_Num_Rotations_Output = 1; // Pre-conversion value ready to be transmitted (can't be a float/decimal)
  int CV_Speed_Rotations_Output = 1;
  </code></pre>
  
  <p>but I'm open to using another variable type.</p>
  
  <p>Master Arduino Code:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;Wire.h&gt;
  
  int CV_Num_Rotations = 1;
  int CV_Speed_Rotations = 1;
  int CV_Num_Rotations_Output = 1; // Pre-conversion value ready to be transmitted (can't be a float/decimal)
  int CV_Speed_Rotations_Output = 1;
  
  void setup() {
    Serial.begin(9600);
    Wire.begin();   // Join i2c bus (address optional for master [becomes master by default])
  
  void loop() {
    Wire.requestFrom(8,2)  // Request 2 bytes from slave device #8
  
    while (Wire.available()) {
  
      Wire.write(CV_Num_Rotations_Output);
      Wire.write(CV_Speed_Rotations_Output);
    }
    Serial.print("CV_Num_Rotations: ");
    Serial.println(CV_Num_Rotations_Output);
    Serial.print("CV_Speed_Rotations: ");
    Serial.println(CV_Speed_Rotations_Output);
    delay(150);
  }
  </code></pre>
  
  <p>Slave Arduino Code:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;Wire.h&gt;
  
  int CV_Num_Rotations_Output = 5; // Values range from 1-20
  int CV_Speed_Rotations_Output = 10; // Values range from 10-30
  
  void setup() {
    Serial.begin(9600);
    Wire.begin(8);
    Wire.onRequest(requestEvent);
  }
  
  void requestEvent() {
    Wire.write(CV_Num_Rotations_Output);
    Wire.write(CV_Speed_Rotations_Output);
  }
  </code></pre>
  
  <p>Since the Master has initial values of '1' for the two CV_Rotation variables, the printout should change from 1 --> 5 and 1 --> 10 respectively.</p>
  
  <p>My results are: I get nothing. Nothing gets printed to the Serial Monitor...</p>
  
  <p>Please help. I can't believe how difficult it is to do such a simple operation such as pass the value of a variable...</p>
  
  <p>Thank you in advance</p>