Storing data in CSV file using python

Hello!
I would like to read data from resistive sensor and store it in CSV file but i do not see the chage in CSV file when the value is changes.

please suggest me a solution.

int analogPin= 0;
int raw= 0;
int Vin= 3;
float Vout= 0;
float R1= 470000;
float R2= 0;
float buffer= 0;

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

void loop()
{
raw= analogRead(analogPin);
if(raw)
{
buffer= raw * Vin;
Vout= (buffer)/1024.0;
R2 = (Vout*R1)/(Vin-Vout);

Serial.print("Vout: ");
Serial.print(Vout);
Serial.print("V");
Serial.print(" ");
Serial.print("R2: ");
Serial.print(R2);
Serial.println("ohm");
delay(100);
}
}

import serial
import time
import csv
import matplotlib
arduino_port = "COM7"
baud = 115200
ser = serial.Serial(arduino_port, baud)
fileName="analog-data.csv"
file = open(fileName, "w")
print("Created file")
time.sleep(2)
ser.flushInput()

samples = int(input("How many sample you want ? :_ "))
line = 0
while line <= samples:
getData = str(ser.readline().decode())
data = getData[18:][:-5]
print(getData)
file = open(fileName, "a")
file.write(data+'\n')
line = line + 1

print("Data collection complete!")
file.close()

Is that a Python program ?

Yes

Then why are you asking for help on an Arduino forum when the Arduino is programmed in C++ ?

raw = analogRead(analogPin);
if (raw) {

This isn’t really helping much…
raw is almost guaranteed to be true ‘all the time’

You’ll get more friends if you use CODE TAGS

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.