Hi guys, for the project I'm working on I need to send data from the serial port in Arduino to Processing and turn that data into graphs. I have three variables, an1 (keeps track of the values of the accelerometer), an2 (indicates if the push button for the piezo speaker is active), an2 (indicates if the push button for the leds is active). I succeeded in visualizing of the values of the accelerometer in a line graph, but can't seem to figure out how I can make multiple graphs. Any advise?
int an1, an2, an3 = 0;
int speakerPin = 2;
int inPin1 = 4; // choose the input pin (for a pushbutton)
int val1 = 0; // variable for reading the pin status
int ledPin1 = 7;
int ledPin2 = 8;
int ledPin3 = 9;
int ledPin4 = 10;
int ledPin5 = 11;
int ledPin6 = 12;
int inPin2 = 13; // choose the input pin (for a pushbutton)
int val2 = 0; // variable for reading the pin status
boolean LedjesActiefDoorKnop = false;
boolean isPushed1 = false; // variable for the status of the button
boolean isPressed1 = false; // variable for the status of the lights
int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;
#include <Wire.h>
void setup()
{
pinMode(speakerPin, OUTPUT);
pinMode(inPin1, INPUT); // declare pushbutton as input
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(inPin2, INPUT); // declare pushbutton as input
Serial.begin(19200);
nunchuck_setpowerpins(); // use analog pins 2&3 as fake gnd & pwr
nunchuck_init(); // write the initilization handshake
Serial.print ("Finished setup\n");
}
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
stopMuziek();
}
}
}
void loop(){
val1 = digitalRead(inPin1); // read input value
if (val1 == HIGH) { // check if the input is HIGH (button pushed)
digitalWrite(speakerPin, HIGH); // turn LED ON
speelMuziek();
} else {
digitalWrite(speakerPin, LOW); // turn LED OFF
stopMuziek();
}
val2 = digitalRead(inPin2); // read input value
if (val2 == HIGH) { // check if the input is HIGH (button pushed)
// Serial.println("Knop niet ingedrukt");
if(digitalRead(ledPin1) == HIGH){
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);
digitalWrite(ledPin6, LOW);
LedjesActiefDoorKnop = false;
an2 = 20;
Serial.println(an2);
} else {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin5, HIGH);
digitalWrite(ledPin6, HIGH);
LedjesActiefDoorKnop = true;
an2 = 200;
Serial.println(an2);
}
}
nunchuck_get_data();
nunchuck_print_data();
delay(100);
}
void stopMuziek(){
noTone(5);
an3 = 10;
Serial.println(an3);
}
void speelMuziek(){
an3 = 100;
Serial.println(an3);
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
stopMuziek();
}
//
// Nunchuck functions
//
static uint8_t nunchuck_buf[6]; // array to store nunchuck data,
// Uses port C (analog in) pins as power & ground for Nunchuck
static void nunchuck_setpowerpins()
{
#define pwrpin PORTC3
#define gndpin PORTC2
DDRC |= _BV(pwrpin) | _BV(gndpin);
PORTC &=~ _BV(gndpin);
PORTC |= _BV(pwrpin);
delay(100); // wait for things to stabilize
}
// initialize the I2C system, join the I2C bus,
// and tell the nunchuck we're talking to it
void nunchuck_init(){
Wire.begin(); // join i2c bus as master
Wire.beginTransmission(0x52); // transmit to device 0x52
Wire.write(0x40); // writes memory address
Wire.write(0x00); // writes sent a zero.
Wire.endTransmission(); // stop transmitting
}
// write a request for data to the nunchuck
// was "write_zero()"
void nunchuck_write_request(){
Wire.beginTransmission(0x52); // transmit to device 0x52
Wire.write(0x00); // writes one byte
Wire.endTransmission(); // stop transmitting
}
// Receive data back from the nunchuck,
int nunchuck_get_data(){
int cnt=0;
Wire.requestFrom (0x52, 6); // request data from nunchuck
while (Wire.available ()) {
// receive byte as an integer
nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.read());
cnt++;
}
nunchuck_write_request(); // write request for next data payload
// If we recieved the 6 bytes, then go print them
if (cnt >= 5) {
return 1; // success
}
return 0; //failure
}
// Print the input data we have recieved
// accel data is 10 bits long
// so we read 8 bits, then we have to add
// on the last 2 bits. That is why I
// multiply them by 2 * 2
void nunchuck_print_data(){
static int i=0;
int joy_x_axis = nunchuck_buf[0];
int joy_y_axis = nunchuck_buf[1];
int accel_x_axis = nunchuck_buf[2]; // * 2 * 2;
int accel_y_axis = nunchuck_buf[3]; // * 2 * 2;
int accel_z_axis = nunchuck_buf[4]; // * 2 * 2;
int endValue =(sqrt(pow(accel_x_axis,2)+pow(accel_y_axis,2)+pow(accel_z_axis,2)));
int z_button = 0;
int c_button = 0;
// byte nunchuck_buf[5] contains bits for z and c buttons
// it also contains the least significant bits for the accelerometer data
// so we have to check each bit of byte outbuf[5]
if ((nunchuck_buf[5] >> 0) & 1)
z_button = 1;
if ((nunchuck_buf[5] >> 1) & 1)
c_button = 1;
if ((nunchuck_buf[5] >> 2) & 1)
accel_x_axis += 2;
if ((nunchuck_buf[5] >> 3) & 1)
accel_x_axis += 1;
if ((nunchuck_buf[5] >> 4) & 1)
accel_y_axis += 2;
if ((nunchuck_buf[5] >> 5) & 1)
accel_y_axis += 1;
if ((nunchuck_buf[5] >> 6) & 1)
accel_z_axis += 2;
if ((nunchuck_buf[5] >> 7) & 1)
accel_z_axis += 1;
an1 = (sqrt(pow(accel_x_axis,2)+pow(accel_y_axis,2)+pow(accel_z_axis,2)));
Serial.println(an1);
i++;
if(endValue != 0 && !LedjesActiefDoorKnop) {
if (endValue != sqrt(pow(accel_x_axis,2)+pow(accel_y_axis,2)+pow(accel_z_axis,2))){
if ((endValue > 268 && endValue < 295 ) || (endValue > 200 && endValue < 225 )){
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);
digitalWrite(ledPin6, LOW);
}else{
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin5, HIGH);
digitalWrite(ledPin6, HIGH);
}}
}
}
// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
char nunchuk_decode_byte (char x)
{
x = (x ^ 0x17) + 0x17;
return x;
}
int an1, an2, an3;
import processing.serial.*;
Serial myPort;
int xPos, yPos, zPos = 1;
//int zPos = an3;
void setup () {
size(800, 600);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 19200);
myPort.bufferUntil('\n');
background(255);
}
void draw () {
line(0,200,800,200);
line(0,225,800,225);
stroke(126);
}
void serialEvent (Serial myPort) {
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 520, 0, height);
stroke(#14b3c9);
line(xPos, height, xPos, height - inByte);
if (xPos >= 300) {
xPos = 0;
background(255);
}
else {
xPos++;
}
stroke(#f68620);
line(yPos, height, yPos, height - inByte);
if (yPos >= 300) {
yPos = 0;
background(255);
}
else {
yPos++;
}
stroke(#dde440);
line(zPos, height, zPos, height - inByte);
if (zPos >= 300) {
zPos = 0;
background(255);
}
else {
zPos++;
}
}
}