diff --git a/UsbReceive.ino b/UsbReceive.ino new file mode 100644 index 0000000000000000000000000000000000000000..94da7ff94e59e553bf5d284aa76b8d0ce602031f --- /dev/null +++ b/UsbReceive.ino @@ -0,0 +1,59 @@ +/************************************************************************* +* 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); +} diff --git a/usb_send.py b/usb_send.py new file mode 100644 index 0000000000000000000000000000000000000000..1eb25ef516d16d3ad930625f687b8460f38b474c --- /dev/null +++ b/usb_send.py @@ -0,0 +1,50 @@ +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.") +