title: Sending Byte from Raspberry Pi to Arduino With I2c & IoT for PWM
body: <p>I am very new to the using both I2C and C#/Windows IoT so apologies up front if any of this is a dumb question. I have a Raspberry Pi 3 master and Arduino slave. I am trying to send a value from a slider on my UI form over I2C to the Arduino which I will use to adjust my PWM duty cycle. There a couple of issues I am having and can't work out if its the Pi, Arduino or both.</p>
  
  <p>Here is my Arduino Slave code:</p>
  
  <pre><code>#include &lt;Wire.h&gt;
  #define MyAddress 0x03
  
  byte ReceivedData;
  int pass;
  
  void setup() {
      Wire.begin(MyAddress);
      Wire.onReceive(I2CReceived);
      Serial.begin(9600);
      //Wire.onRequest(I2CRequest);
  }
  
  void loop() {
  
      delay(100);
  }
  
  void I2CReceived(int NumberOfBytes)
  {
      /* WinIoT have sent data byte; read it */
    byte ReceivedData = Wire.read();
    Serial.println(ReceivedData);
    if (ReceivedData &lt;= 127){
        Serial.println("Equal or under");
        return;
    }else{
        Serial.println("over");
        return;
    }
  
  }
  </code></pre>
  
  <p>And my Pi Master:</p>
  
  <pre><code>using System;
  using Windows.Devices.Gpio;
  using Windows.UI.Xaml;
  using Windows.UI.Xaml.Controls;
  using Windows.UI.Xaml.Controls.Primitives;
  using Windows.UI.Xaml.Media;
  using Windows.UI.Core;
  
  
  
  
  using Windows.Devices.Enumeration;
  
  using Windows.Devices.I2c;
  
  using System.Diagnostics;
  
  using System.Threading;
  
  namespace I2COutput
  {
  
      public sealed partial class MainPage : Page
  
      {
  
  
          private I2cDevice TransPump;
  
          private Timer periodicTimer;
  
          private const byte pump = 0x03;
  
          double pos;
  
          public MainPage()
  
          {
  
              InitializeComponent();
  
              initcomunica();
  
          }
  
  
  
          private async void initcomunica()
  
          {
  
  
              var pumpset = new I2cConnectionSettings(pump);
  
  
              pumpset.BusSpeed = I2cBusSpeed.StandardMode;
  
              string aqs = I2cDevice.GetDeviceSelector("I2C1");
  
              var dis = await DeviceInformation.FindAllAsync(aqs);
  
  
              TransPump = await I2cDevice.FromIdAsync(dis[0].Id, pumpset);
  
  
          }
  
          private async void SendChange()
  
          {
              byte[] sendpos;
              try
              {
                 sendpos = BitConverter.GetBytes(pos);
                  TransPump.Write(sendpos);
              }
              catch (Exception e)
              {
                  Debug.WriteLine(e.Message);
              }
  
          }
  
  
          private void tempLbl_SelectionChanged(object sender, RoutedEventArgs e)
          {
  
          }
  
          private void slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
          {
  
              pos = slider.Value;
              temp2Lbl.Text = pos.ToString();
              Convert.ToInt16(pos);
              SendChange();
  
              return;
  
          }
      }
  }
  </code></pre>
  
  <p>The first issue I am having is that my <code>ReceivedData</code> on the Arduino is always 0 not matter what the value of <code>sendpos</code> is on the Pi (yes, it does change when I move the slider).</p>
  
  <p>The second issue I am having is the first time the slider is moved I get the output on the Arduino serial but then nothing after. If I either reset or reload the Arduino I then get the output of the initial slider change again and nothing after.</p>
  
  <p>Apologies if any of this is too vague or explained poorly, any help or nudge in the right direction would be greatly appreciated.</p>
  
  <p>Thanks in advance. </p>