Skip to content
Snippets Groups Projects
Commit 271885aa authored by ilo21's avatar ilo21
Browse files

Arduino and python codes where python sends sine wave data over serial and...

Arduino and python codes where python sends sine wave data over serial and arduino reads those data and sets dac to those values
parent 3548246e
No related branches found
No related tags found
1 merge request!1Arduino and python codes where python sends sine wave data over serial and...
/*************************************************************************
* Code that reads values sent from python through serial
************************************************************************/
#include <Wire.h> // For I2C communication with sensor
#include <Wireling.h> // For interfacing with Wirelings
// Make Serial Monitor compatible for all TinyCircuits processors
#if defined(ARDUINO_ARCH_AVR)
#define SerialMonitorInterface Serial
#elif defined(ARDUINO_ARCH_SAMD)
#define SerialMonitorInterface SerialUSB
#endif
void setup() {
SerialMonitorInterface.begin(115200);
Wire.begin();
// Initialize Wireling
Wireling.begin();
Wireling.selectPort(0);
analogWriteResolution(10); //Change the DAC resolution to 10-bits
analogWrite(A0, 0); // Initialize Dac to Zero
}
void loop() {
int output;
//static float x = 270; // Current degrees for sine wave (initially 270 so that
// the sine wave is initially zero).
// output = (int) (512.0 * sin(0.017453 * x) + 512); // Sine Wave
if (SerialMonitorInterface.available()> -1) {
output = SerialMonitorInterface.parseInt();
analogWrite(A0, output);
}// end if serial available
//analogWrite(A0, output); // Write the analog output to A0
//SerialMonitorInterface.print(output);
//SerialMonitorInterface.println();
/*
* Increment Degrees for the Next Wave Point
*
* To utilize the entire resolution of the 10-bit DAC, at least 1024
* values must be used between 0 and 360 degrees. Therefore, a step
* value greater than 360/1024 will not utilize the entier resolution
* of the DAC.
*/
//x += 0.35;
//if(x>=360.0) x -= 360.0;
//delay(1);
}
import serial
import numpy as np
import time
import struct
'''
Sends sinewave values to arduino dac
'''
# com port for arduiono usb
COM = "COM4"
# baudrate same as in arduino
BAUDRATE = 115200
# Current degrees for sine wave (initially 270 so that the sine wave is initially zero).
x = 270
my_serial = None
try:
my_serial = serial.Serial(COM, baudrate= BAUDRATE, timeout = 2)
print("Device found")
# start infinite loop
# breaks by key interrupt (ctrl + c)
while True:
try:
# calculate sine wave current value
output = 512.0 * np.sin(0.017453 * x) + 512 # Sine Wave
# print(output)
# translate to something that will be read by arduino
arg = bytes(str(int(output)), 'utf8','ignore') + b'\r'
# send to arduino serial
my_serial.write(arg)
time.sleep(0.01)
'''
* Increment Degrees for the Next Wave Point
*
* To utilize the entire resolution of the 10-bit DAC, at least 1024
* values must be used between 0 and 360 degrees. Therefore, a step
* value greater than 360/1024 will not utilize the entier resolution
* of the DAC.
'''
x += 0.35
if(x>=360.0):
x -= 360.0
except KeyboardInterrupt:
print("Done")
break
except:
print("Problem finding device.\nCheck your com port or device.")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment