Arduino is probably the highest level programming language for 8 bit microcontrollers, but it is still not that high. So you have to do some of the dirty work to achieve what you want.
First I need to convert the ASCII char to hexadecimal.
Second I am still confused as to read 2 values on the serial.read.
So: "1200, 30" will NOT result in freq = 1200 and duration = 30
How do I do that?
I wish Arduino Software would have a better interface using Serial Monitor. Something like a ASCII to hex converter. I mean my programming skills are based on trial and error ( much of it) and interaction with the serial comm is the best way to test things.
Anyway, I read the article on the blog, but as expected i became very confused.
The point is I want to test tones at diferent frequencies and durations using serial interface. Anyone knows a good solution?
The below code is for servos, but it shows a simple way to send two values together from the serial monitor, split the values, and convert the vaues into numbers for use with the servos.
// zoomkat 10-20-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1; // create servo object to control a servo
Servo myservo2;
void setup() {
Serial.begin(9600);
myservo1.attach(6); //the pin for the servo control
myservo2.attach(7);
Serial.println("servo-test-22"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(1);
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //see what was received
// expect a string like 07002100 containing the two servo positions
servo1 = readString.substring(0, 4); //get the first four characters
servo2 = readString.substring(4, 8); //get the next four characters
Serial.println(servo1); //print to serial monitor to see results
Serial.println(servo2);
int n1; //declare as number
int n2;
int n1 = servo1.toInt();
int n2 = servo2.toInt();
myservo1.writeMicroseconds(n1); //set servo position
myservo2.writeMicroseconds(n2);
readString="";
}
}