minauras
Joined: Dec 19, 2016 Posts: 1 Location: Switzerland
|
Posted: Mon Dec 19, 2016 3:12 pm Post subject:
Problem using ChucK and Serial |
|
|
Hey everybody,
I'm fairly new to ChuCk, and also to Serial (I've never used it before). Now I'm working on a project for fun where I want to operate oscillators in ChucK using an arduino. So I've got this small Arduino program:
Code: | void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0)/4);
delay(100);
} |
which sends the data collected from the A0 port to Serial.
And I don't know much about Serial so in ChucK I've used the program from someone else from the internet who operated ChucK with an arduino too, and I've copied/paste their method of getting data from Serial. I get a ChucK program like this:
Code: | SerialIO.list() @=> string list[];
for(int i; i < list.cap(); i++)
{
chout <= i <= ": " <= list[i] <IO> int device;
if(me.args()) {
me.arg(0) => Std.atoi => device;
}
if(device >= list.cap())
{
cherr <= "serial device #" <= device <= " not available\n";
me.exit();
}
SerialIO cereal;
if(!cereal.open(device, SerialIO.B9600, SerialIO.ASCII))
{
chout <= "unable to open serial device '" <= list[device] <= "'\n";
me.exit();
}
fun float map(float a, float ilow, float ihigh, float olow, float ohigh)
{
((a/(ihigh-ilow))*(ohigh-olow)) + olow => float temp;
return temp;
}
SinOsc s => dac;
while(true)
{
cereal.onLine() => now;
cereal.getLine() => string line;
if(line$Object != null) {
StringTokenizer tok;
tok.set(line);
Std.atoi(tok.next()) => int pos;
map(pos, 0, 255, 300, 400) => float frequency;
s.freq(frequency); // Change sin wave frequency
}
} |
(sorry for the lack of comments) Now I don't exactly understand what it does but I've got a fair idea from reading it thoroughly.
Here's my problem: yesterday the program worked perfectly. And today after starting my computer and opening the programs it didn't work anymore (I only get the default frequency of my SinOsc).
I've tracked where the error would be and it seems that the program reaches this line, but never passes through it:
Code: | cereal.onLine() => now; |
From what I understand, it means my SerialIO cereal is waiting to get data but doesn't get anything.
I've checked my serial output from the arduino using the serial monitor and everything seems to work fine, so there must be a problem in my ChucK code. Any idea? (I've already tried to restart my computer )
Thank you everyone! |
|