Sorry its been a day or two, but have you ever wondered how your chosen microprocessor keeps acurate time, well look here,
http://micro.magnet.fsu.edu/creatures/pages/rolex.html , i spent most of yesterday being amazed,by what was on that site worth a look but dont blame me if you spend far too long on there.
Last time i did a short sketch introducing serial comms, if you tried it you would have noticed that if the choosen com port was wrong or the comport not connected then it would error, but if you were connected and using the right comm port all was well, it was a quick way of showing how easy it is to establish comms, Did i say easy well that bit was but getting flowcode and processing to talk took a while, why because im stupid, yup had a homer simpson day trying to sort it out and in the end it was simple, both processing and flowcode communicate very well, but i had made a school boy error what i wanted to do was to send a capital A to switch on an output, i tried everyway in flowcode, but just could not get it to work, basically i set up a decision saying if "A" is received switch on port c0# instead of "A" i should of used 'A' , simple errors take the longest to sort out, moving swiftly on we will combine our last two sketches below is the sketch as we finished a couple of posts ago and this is the one we will modify.
Code: Select all
boolean buttonr = false;
boolean buttonl = false;
PFont Dave; // The display font we will call ours dave(call it what you want)
void setup(){
size(600,600);
background (21,45,120);
fill(255,0,0);
rect(13,460,33,30);
fill(255,0,0);
rect(539,460,33,30);
Dave = loadFont("SegoePrint-Bold-48.vlw");
textFont(Dave, 18);
}
void draw(){
if (buttonr==true) {
delay(600);
fill(0,255,0);
rect(539,460,33,30);
fill(0,0,255);
text("On",542,480);
} else if (buttonl==false){
fill(255,0,00);
rect(539,460,33,30);
fill(#3F25A1);
text("Off",541,480);
}
if (buttonl==true) {
delay(600);
fill(0,255,0);
rect(13,460,33,30);
fill(0,0,255);
text("On",16,480);
} else if (buttonl==false){
fill(255,0,00);
rect(13,460,33,30);
fill(#3F25A1);
text("Off",14,480);
}
}
void mousePressed() {
if (mousePressed && (mouseButton == LEFT&& mouseX > 13 && mouseX < 13+33 && mouseY > 460 && mouseY < 460+30)) {
buttonl = !buttonl;
println(buttonl);
} else if (mousePressed && (mouseButton == RIGHT && mouseX > 539 && mouseX < 539+33 && mouseY > 460 && mouseY < 460+30)) {
buttonr = !buttonr;
}
}
we shall start by adding what we did last time add the 4 lines of code below to the top of your sketch, the 1st line simply imports the serial libray, 2nd line we define our port name, the 3rd line we will add for future functionality basically we are setting up a string for recieving from the pic, 4th line is an ascii line feed so any time we send a 10 then the pic will know its the end of the message
Code: Select all
import processing.serial.*;
Serial monkeyPort; // The serial port:
String inString; // Input string from serial port:
int lf = 10; // ASCII linefeed
the following line go in the setup under the trextfont (Dave,18); bit you will remember from last time we used this to list our available ports and to choose our port and baud, the extra n,8,1.0 are just extra definitions and can be left out
Code: Select all
println(Serial.list());
monkeyPort = new Serial(this, Serial.list()[3], 9600,'N', 8, 1.0);
now we will add an action to our buttons and send a capital A and B to switch on led's connected to the pic, so in the mousepressed function you will notice two writes the first write sends Ascii 65 ( A ) to our pic, the second line sends a linefeed to signal the pic its finished sending data so beneth our println(buttonl); bit we add the following, at the same time add the same code to the buttonr bit but change the 65 A to 66 B
Code: Select all
monkeyPort.write(65);
monkeyPort.write(10);
now remember we set up a define for instring at the top now we are going to use it we create a function so at the bottom of your sketch add the following code, all we are doing is waiting for an incoming message then if one comes in we print it out in the bottom of the window,
Code: Select all
void serialEvent(Serial monkeyPort) {
inString = (monkeyPort.readString());
println(inString);
}
the last bit of code is all well and good but we need to add one last bit so that when you send data the sketch knows when it ends, so we add the following this needs to go into the last line of set up.
monkeyPort.bufferUntil(lf);
thats it for now, nearly forgot to add the flowcode its a simple comms using the uart of your pic, it runs of the intosc at 4Mhz so this needs to be set up by you( THIS IS NOT MY FLOWCHART I COPIED ONE OFF THE FORUM AND ADDED MY BITS(i hope the createor of the flowchart doesnt mind)), if your chip has an extended instruction set entry turn it off and also turn off watchdog timer and low voltage program. You will notice that our flowchart has four decisions and we only use Two A and B . C and D are there for you to workout how to switch the leds back off in processing. im adding the complete sketch here ,it is working fine, my hardware im using is a pic 18F27j53, easypic 7 dev board and a lot of luck.
There is so much more you can do with processing but its beyond my abilities t present to do fancy stuff, but you should by now see how simple it is to allow your pc and pic or avr to communicate, other things you could try is set up an LDR and send the values to processing then graph the data. Examples are on the web, you can also set up a wireless connection to talk to your pics etc.
code for this post bellow
Code: Select all
import processing.serial.*;
Serial monkeyPort; // The serial port:
String inString; // Input string from serial port:
int lf = 10; // ASCII linefeed
boolean buttonr = false;
boolean buttonl = false;
PFont Dave; // The display font we will call ours dave(call it what you want)
void setup(){
size(600,600);
background (21,45,120);
fill(255,0,0);
rect(13,460,33,30);
fill(255,0,0);
rect(539,460,33,30);
Dave = loadFont("SegoePrint-Bold-48.vlw");
textFont(Dave, 18);
println(Serial.list());
monkeyPort = new Serial(this, Serial.list()[3], 9600,'N', 8, 1.0);
monkeyPort.bufferUntil(lf);
}
void draw(){
if (buttonr==true) {
delay(600);
fill(0,255,0);
rect(539,460,33,30);
fill(0,0,255);
text("On",542,480);
}
else if (buttonl==false){
fill(255,0,00);
rect(539,460,33,30);
fill(#3F25A1);
text("Off",541,480);
}
if (buttonl==true) {
delay(600);
fill(0,255,0);
rect(13,460,33,30);
fill(0,0,255);
text("On",16,480);
// monkeyPort.write("240");
// monkeyPort.write(10);
}
else if (buttonl==false){
fill(255,0,00);
rect(13,460,33,30);
fill(#3F25A1);
text("Off",14,480);
//monkeyPort.write("24");
//monkeyPort.write(10);
}
// while (monkeyPort.available() > 0) {
// int lastIn = monkeyPort.last();
// println(lastIn);
// }
}
void mousePressed() {
if (mousePressed && (mouseButton == LEFT&& mouseX > 13 && mouseX < 13+33 && mouseY > 460 && mouseY < 460+30)) {
buttonl = !buttonl;
println(buttonl);
monkeyPort.write(65);
monkeyPort.write(10);
}
else if (mousePressed && (mouseButton == RIGHT && mouseX > 539 && mouseX < 539+33 && mouseY > 460 && mouseY < 460+30)) {
buttonr = !buttonr;
println(buttonr);
monkeyPort.write(66);
monkeyPort.write(10);
}
}
void serialEvent(Serial monkeyPort) {
inString = (monkeyPort.readString());
println(inString);
}
If anyone want me to carry on with these posts just holler
Regards
Dazz 0) {
// int lastIn = monkeyPort