Created
March 20, 2020 03:28
-
-
Save aoirint/9fb69531fb6f5b379ef6fbc00ee8004a to your computer and use it in GitHub Desktop.
feb08 シリアル通信検証
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float f = 100; | |
float amp = 255; | |
void setup() { | |
// pinMode(A0, OUTPUT); | |
Serial.begin(115200); | |
} | |
void loop() { | |
float t = (float) millis() / 1000; | |
float value = (1.0 + sin(t * f * (2.0*PI))) / 2.0; | |
analogWrite(A0, (int) (value*amp)); | |
float output = analogRead(A1); | |
Serial.println(output); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <TimerOne.h> | |
unsigned char buffer[1500]; | |
int buffer_size = sizeof buffer; | |
int buffer_write_ptr = 0; | |
int buffer_read_ptr = 0; | |
bool first_data = true; | |
void setup() { | |
for (int i=0; i<buffer_size; i++) buffer[i] = 0; | |
Serial.begin(115200); | |
Timer1.initialize(1.0/500 * 1000*1000); | |
Timer1.attachInterrupt(readNextAmp); | |
} | |
void readNextAmp() { | |
int value = buffer[buffer_read_ptr]; | |
analogWrite(A0, value); | |
buffer_read_ptr += 1; | |
if (buffer_read_ptr > buffer_size) { | |
buffer_read_ptr = 0; | |
} | |
} | |
void loop() { | |
int value = Serial.read(); | |
first_data = value >= 0; | |
if (! first_data) { | |
value = 0; | |
} | |
if (value < 0) value = 0; | |
buffer[buffer_write_ptr] = value; | |
buffer_write_ptr += 1; | |
if (buffer_write_ptr > buffer_size) { | |
buffer_write_ptr = 0; | |
} | |
Serial.print(micros()); | |
Serial.print(","); | |
Serial.print(buffer_read_ptr); | |
Serial.print(","); | |
Serial.print(buffer_write_ptr); | |
Serial.println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment