Serialdata import python "every other data point"
<p>I'm trying to read serial data of an arduino which has been somewhat successful. The values that will be read from the arduino is voltage and current. I'm now trying to differentiate the different variables but i have no clue how. The arduino is sending the values in the following order with 1 sec delay. Voltage, AMPs, Voltage, AMPs.
How could i differentiate these values in to different variables? 
Here is my current code which doesn't differentiate the variables at all.</p>

<pre><code>import time
import serial
values = []
serialVoltage = serial.Serial('/dev/ttyACM0', baudrate=9600, timeout=1)
voltage = serialVoltage.readline()
time.sleep(1)
while True:print(voltage)



void setup() {
      Serial.begin(9600);
      }

    void loop() {
      float voltageRead = analogRead(A0);
      float ampsRead = analogRead(A1);
      float calculatedVoltage = voltageRead / 103;
      float calculatedCurrent = ampsRead / 1;
      Serial.println(calculatedVoltage);
      delay(1000);
      Serial.println(calculatedCurrent);

      delay(1000);
    }
</code></pre>

<p>First of all, you shouldn't need the <code>sleep</code>s on the raspberry Pi - <code>readline</code> will block until the output comes along.</p>

<p>You should structure your code to read the voltage and current separately:</p>

<pre><code>while True:
    voltage = serialVoltage.readline()
    current = serialVoltage.readline()
    print("V:", voltage, "A:", current)
</code></pre>

<p>Note also that if you want to work with the string values returned by <code>readline</code> you should first explicitly convert them to numeric form.</p>

<p>Remember that <code>readline</code> returns a string that includes the line termination - these are the <code>'\r\n'</code> characters you mention in your comment. I can't see how the "bad values" you mention are being created, but it would probably be helpful if the Arduino output allowed you to discriminate between voltage and current values.</p>

<p>My suggestion would be that you actually print out both values on a single line from the Arduino in a format that can readily be decoded by your Python program. Since it appears that the Arduino will already be running when you run the Python program on the Pi you could then simply ignore the first (possibly partial) line that you read, which will guarantee that all subsequent lines are whole outputs.</p>

<p>The "odd values" you see are because I assumed since you used the form <code>print(voltage)</code> that you were using Python 3! Since you appear to be using Python 2 your <code>print</code> statement should probably read something like</p>

<pre><code>print "V:", voltage, "A:", current
</code></pre>

<p>The interpreter will then print the output as a string, rather than trying to show you the value of a tuple containing the four things to be printed.</p>

python arduino serial-port raspberry-pi3