Nov 27, 2020

read_potentiometer

 read_potentiometer.ino

import serial

import time

 # set up the serial line

ser = serial.Serial('COM3', 9600)

time.sleep(2)

# Read and record the data

data =[]                       # empty list to store the data

for i in range(50):

    b = ser.readline()         # read a byte string

    string_n = b.decode()  # decode byte string into Unicode  

    string = string_n.rstrip() # remove \n and \r

    flt = float(string)        # convert string to float

    print(flt)

    data.append(flt)           # add to the end of data list

    time.sleep(0.1)            # wait (sleep) 0.1 seconds

ser.close()


read_potentiometer.ino

// potentiometer_read.ino

// reads a potentiometer and sends value over serial

int sensorPin = A0;  // The potentiometer on pin 0                  

int ledPin = 13;     // The LED is connected on pin 13

int sensorValue;     // variable to stores data


void setup() // runs once when the sketch starts

{

  // make the LED pin (pin 13) an output pin

  pinMode(ledPin, OUTPUT);


  // initialize serial communication

  Serial.begin(9600);

}


void loop() // runs repeatedly after setup() finishes

{

  sensorValue = analogRead(sensorPin);  // read pin A0   

  Serial.println(sensorValue);         // send data to serial


  if (sensorValue < 500) {            // less than 500?

    digitalWrite(ledPin, LOW); }     // Turn the LED off


  else {                               // greater than 500?

    digitalWrite(ledPin, HIGH); }     // Keep the LED on


  delay(100);             // Pause 100 milliseconds

}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.