Здесь показаны различия между двумя версиями данной страницы.
| Следующая версия | Предыдущая версия | ||
|
oscilloscope-code [2018/11/28 17:46] super_admin создано |
oscilloscope-code [2019/06/24 20:29] (текущий) |
||
|---|---|---|---|
| Строка 2: | Строка 2: | ||
| [[https://circuitdigest.com/microcontroller-projects/arduino-oscilloscope-code-circuit]] | [[https://circuitdigest.com/microcontroller-projects/arduino-oscilloscope-code-circuit]] | ||
| + | |||
| + | Для тех, кто программирует на Python в Windows, рекомендую простой редактор [[https://thonny.org/]] | ||
| With pip installed, we can now install the other libraries we need. | With pip installed, we can now install the other libraries we need. | ||
| Строка 13: | Строка 15: | ||
| <code shell> | <code shell> | ||
| pip install matplotlib | pip install matplotlib | ||
| - | </code> | + | </code> |
| Drawnow is sometimes installed alongside matplotlib but just to be sure, run; | Drawnow is sometimes installed alongside matplotlib but just to be sure, run; | ||
| <code shell> | <code shell> | ||
| pip install drawnow | pip install drawnow | ||
| + | </code> | ||
| + | |||
| + | Немного скорректированный код | ||
| + | |||
| + | <code python> | ||
| + | import time | ||
| + | import matplotlib.pyplot as plt | ||
| + | from drawnow import * | ||
| + | import serial | ||
| + | val = [ ] | ||
| + | cnt = 0 | ||
| + | #create the serial port object | ||
| + | port = serial.Serial('COM6', 9600, timeout=0.5) | ||
| + | plt.ion() | ||
| + | |||
| + | #create the figure function | ||
| + | def makeFig(): | ||
| + | plt.ylim(-1023,1023) | ||
| + | plt.title('Osciloscope') | ||
| + | plt.grid(True) | ||
| + | plt.ylabel('data') | ||
| + | plt.plot(val, 'ro-', label='Channel 0') | ||
| + | plt.legend(loc='lower right') | ||
| + | |||
| + | while (True): | ||
| + | port.write(b's') #handshake with Arduino | ||
| + | if (port.inWaiting()):# if the arduino replies | ||
| + | value = port.readline()# read the reply | ||
| + | print(value)#print so we can monitor it | ||
| + | decode = value.decode('utf-8').strip() | ||
| + | print(decode) | ||
| + | | ||
| + | number = int(decode) #convert received data to integer | ||
| + | print('Channel 0: {0}'.format(number)) | ||
| + | # Sleep for half a second. | ||
| + | time.sleep(0.01) | ||
| + | val.append(number) | ||
| + | drawnow(makeFig)#update plot to reflect new data input | ||
| + | plt.pause(.000001) | ||
| + | cnt = cnt+1 | ||
| + | if(cnt>50): | ||
| + | val.pop(0)#keep the plot fresh by deleting the data at position 0 | ||
| + | |||
| + | </code> | ||
| + | |||
| + | <code c++> | ||
| + | int sensorpin = A0; | ||
| + | void setup() { | ||
| + | // initialize serial communication at 115200 bits per second to match that of the python script: | ||
| + | Serial.begin(9600); | ||
| + | } | ||
| + | void loop() { | ||
| + | // read the input on analog pin 0:######################################################## | ||
| + | int sensorValue = analogRead(sensorpin); | ||
| + | byte data = Serial.read(); | ||
| + | if (data == 's') | ||
| + | { | ||
| + | Serial.println(sensorValue); | ||
| + | delay(100); // delay in between reads for stability | ||
| + | } | ||
| + | } | ||
| </code> | </code> | ||