We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello! I hope I made the post in the correct forum -- I've read the rules, but I do apologize if I made a mistake! I've searched the forums and Google for any help, but the one possible post that could have helped is deleted! D'oh!
Anyway, I've been working on a class project where I want to be able to draw freehand in Processing. This project contains two parts: connecting the wiichuck to the Arduino, and then using the Serial/drawing in Processing. The first works, but the second one is giving me problems. Admittedly, before this class, I've never heard nor used Processing, so I'm pretty much an idiot wandering blind as a bat here.
I got as far as having lines appear, but anytime I try to edit or add variables, it goes poof. I honestly just want to be able to have some freehand lines so that they can be manipulated with the wiichuck controls. That's really the only piece of code I want to learn to use and have for this.
Here's the code, in its current state:
import remixlab.bias.*;
import remixlab.bias.event.*;
import remixlab.dandelion.constraint.*;
import remixlab.dandelion.core.*;
import remixlab.dandelion.geom.*;
import remixlab.fpstiming.*;
import remixlab.proscene.*;
import remixlab.util.*;
import net.java.games.input.*;
import org.gamecontrolplus.*;
import org.gamecontrolplus.gui.*;
import at.mukprojects.console.*;
// Processing sketch to draw line that follows nunchuck data
import processing.serial.*;
float accy;
float accx;
float zbut;
float x1, x2;
float y1, y2;
float cbut;
//adding a custom font
PFont font;
// creates a processing font value named font
import fontastic.*;
Serial myPort; // Create object from Serial class
public static final short portIndex = 1;
void setup()
{
size(500, 500);
// Open whatever port is the one you're using - See Chapter 4
myPort = new Serial(this,Serial.list()[0], 9600);
font = loadFont("ArtistampMedium-40.vlw");
textFont(font);
text("Style is influence is style", 26, 30, 260, 200);
/*
assigns the font stored in the
font variable to textFont
*/
strokeWeight(3);
x1= cos(accy);
y1= cos(accx);
}
void draw()
{
if ( myPort.available() > 0) { // If data is available,
int y = myPort.read(); // read it and store it in val
int x = myPort.read(); // read it and store it in val
int z = myPort.read(); // read it and store it in val
int c = myPort.read(); // read it and store it in val
background(255); // Set background to white
x1= cos(accy);
y1= cos(accx);
line(x1, y1, x2, y2);
}
}
Any help would be appreciated. Thank you!
Answers
It will be great to see your arduino code. What are those imports on top of the code? If you are receiving the data trough your arduino via serial, then I doubt you need any other library unless you want to use any provided library to manipulate data streamed through the arduino unit. Could you clarify on those imports lines, what are they use for? Or did you just copy them as part of a previous example?
Alternative, you could describe what data will arrive via serial and how to precisely use this data to produce lines.
A final point and to complement your post, what arduino unit are you using and how does it interact with your wiichuck device?
Kf
No problem! Here's my Arduino code -- I added the y,z and c in the get_data line group, but more or less I've left it intact:
I'm using the Arduino UNO unit and I'm using a Wiichuck Adapter so it can interact with my wiichuck. EDIT: Forgot to add that, all the numbers in the serial monitor work whenever I manipulate the wiichuck.
The imports on the top of the code are imports that allow for any game console to be used and proscene, which allows for more interactivity? That's what I understood. I downloaded/installed them directly from Processing's libraries. The initial base code worked just fine without them, but I added them because I thought maybe they'd allow me more control in lineart variables than what I have right now, haha.
The data I want to receive through Arduino is basically accelerometer data (I hope I used that right), which would allow anyone to manipulate a line in Processing, in the way you would with a tablet in any art program. If you use the z or c button to fill in for example, and the x and y axis to draw the lines. I suppose more like a less-expensive glorifed Cintiq! Does that help clarify things?
Not sure how the forum feels about double posting, but I thought I should update: I changed the code in the Arduino to see if I could get my Processing code to work. While it hasn't done anything (in other words, I'm still stuck on my original problem), I'm posting the new code here:
I'm still using the same board and adapter, by the by!
YOu should check any of the previous serialEvent posts. For example, here is one: https://forum.processing.org/two/discussion/comment/92348/#Comment_92348
You need to adapt your code in arduino and then in processing. I looked at you code from the first two posts and there is no connection between those two codes. Then you last post of arduino looks more promising but then your processing code needs to be adapt it.
But then before you implement any changes, you need to learn how the serialEvent works. For this, you need to read all the entries in the Processing's webpage under libraries >> Serial. This is a good start to understand the tools you will be using for your task. Also study the example in the link I provided you. Although the example uses an arduino unit, the process of sending data would be the same. Keep in mind I am assuming the wiichuck controller is sending data as you described in your posts. You can always provide a link to your resources and any other documentation and any forum goer will be able to assist you better.
Here is an example of how your wiichuck loop should look like:
Kf
Okay, maybe I am too stupid for this project in general more than I thought :-S
So I read over the Serial library like you told me to, and while I'm beginning to understand what does what, anytime I try to adapt it for the wiichuck, nothing seems to work. If anything, Processing tells me that part of the Serial codes like buffer are invalid! :\ It's not the first time the program has told me that codes for it are invalid...
I will try to see what it is I'm doing wrong (because again, I'm too stupid), but yeah, let me leave what it is I'm using for resources and documentation!
In the attached files, is the Arduino code with the wiichuck loop and Arduino telling me to declare the scope even though I have? And the other file is what I would like the end product to be: a canvas where you can doodle or draw anything you want.
So in that sense, I want to do something like this: https://processing.org/examples/continuouslines.html
The second Arduino code I posted comes from here: https://github.com/olavolav/MIDIDance/blob/master/sendOneNunchuckData/sendOneNunchuckData.ino
(There's another version here, but that seems to be for Processing, based on the .pde extension. Hmmm.) https://todbot.com/arduino/sketches/NunchuckPrint/NunchuckPrint.pde
Meanwhile, the Processing code I used in my first post (albeit without the added imports and such) came from the Arduino Cookbook by Michael Margolis, pg.397-401. From his section on the Wiichuck Accelerometer.
Post your latest code please.
Kf
Start by using the code in your arduino from your prev post: https://todbot.com/arduino/sketches/NunchuckPrint/NunchuckPrint.pde
In processing use:
This will test what data is arriving from your Arduino or at least that the data is being transferred.
Kf
Oops, I actually forgot to put the code in my previous post! Sorry about that, here's the Arduino version (which only has the nunchuck_get data added in)
and the Processing code I've been working as I try to understand the Serial libraries further:
So I've tried the Processing code you posted and am receiving the following in BAUDRATE 19200, which compared to 9600 LOOKS a lot better, haha.
and BAUDRATE 9600 -- which was much more longer, but I put a snippet of it here just so you can see how it looked for rows and rows of code:
If I'm not mistaken, considering how the 19200 one went, I suppose it means I can edit it to the point where I can do my original purpose with it, right? (Though honestly I'm just happy the data is FINALLY being seen in Processing! That was the only part I never saw in all my attempts.)
You should match the baud rate between units. In your nunchuck code line 14 you have 19200 so your Serial code in Processing should be also 19200 (Line 19 in the Serial function)
Ok, so now it seems everything is working when using the proper baud rate. Good news! Keep working in the code. Whatever you send from your first code, check if you receive it in your second code. After you make sure all your data that you need is send and received properly, then you could work in the processing part as drawing lines or working in any other advanced features. If you get stuck, post your questions and details below.
Kf
Thank you so much really for all your help! :D I'm really glad this has worked, but I definitely still keep getting stuck :\ Here's what I've been tinkering with the Processing code. I keep trying to add values that can match the ones in Arduino, like you mentioned (which I hadn't noticed before!!) but Processing keeps telling me it doesn't work, even though it has in the examples codes I've gotten from other sites like the Cube for Wii (I'll post that after my own code):
The Cube:
I even tried going into the H file of the Nunchuck library in Arduino to see if maybe there's a hint I'm missing and I found it weird that part of it looked more like Processing but that's just me mixing everything up at this point probably.
It likely has nothing to do with this in the end, but I'm just getting frustrated at my stupidity. I hate it when I know (hope?) I'm close but every drawing code is made for a mouse and people's custom Nunchuck codes aren't working despite otherwise.
@kfrajer: I feel so bad after every piece of help you've given me but the deadline for this is only in a couple of hours and at this rate, I may have to call it "as is." At least I learned enough that I can explain my own goal and process. (And luckily I am not the only one in the class who had some trouble but still, I detest my "incomplete" situation!)
Again, thank you so much! I'm just going to have to chalk this up to technology + my own stupidity going against me this time.
Unfortunately my help is limited as I do not own an unit to test the code on. In cases when you are dealing with hardware (or even software), documentation is key. Yes, using somebody else code works but that is only if the code is up to date and compatible with your unit.
The cube code above seems alright. Notice in that example they have the option to use accelerometers or joystick. Mixing these two sensors could work for this example, but most of the time mixing sensors that way doesn't work. One reason is that you need to be aware of the units each sensor sends. For example, if you are tapping into an android phone, you can use the accelerometer of this device, or you can inquire for temperature, gps coordinates, etc. Each value returned has their own units (acceleration could be in units of force, gps coordinates in degrees or in UTM, temperature in Celsius or Kelvin, etc) For this reason it is important to get your hands in the documentation. In the documentation it will provide all these details to be able to use your sensor in your application.
For example, from this site: http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Nunchuck
I don't know if this applies to your unit, further investigation required. From the serial printout you provided above, it seems you were getting the right values. If you are interested to pursue this, please provide the latest arduino and processing code.
Kf