We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I tried your method, but still get the same error, disabling port.
What am I doing wrong?
Thanks
import processing.serial.*; Serial port; int BaseEncGlobal; int ElbowEncGlobal; int ShoEncGlobal; int VertRotEnc; int HorRotEnc; int GripEncGlobal;
double X_axis; double Y_axis; double Z_axis; double GripAngle;
String data; boolean newData = false;
PFont font;
void setup() { size(1280,800); //port = new Serial(this, "/dev/cu.usbserial-A50285BI", 115200); port = new Serial(this, "/dev/cu.usbmodem2574541", 115200); port.bufferUntil('\n'); font = loadFont("AgencyFB-Bold-200.vlw"); textFont(font, 40); }
void draw()
{ if (newData == true) {
int spaceDown = 55; background(0,0,0); fill(46, 209, 2); text(BaseEncGlobal, 70, spaceDown); fill(0, 102, 153); text(ShoEncGlobal, 70, spaceDown2); fill(0, 102, 153); text(ElbowEncGlobal, 70, spaceDown3); fill(0, 102, 153); text(VertRotEnc, 70, spaceDown4); fill(0, 102, 153); text(HorRotEnc, 70, spaceDown5); fill(0, 102, 153); text( GripEncGlobal, 70, spaceDown*6);
text(Double.toString(X_axis/10), 270, spaceDown ); newData =false; }
}
void serialEvent (Serial port) {
data = port.readStringUntil('\n'); if (data != null) { data = trim(data);
int[] nums = int(split(data, ',')); BaseEncGlobal = nums [1]; ShoEncGlobal = nums [2]; ElbowEncGlobal = nums [3]; VertRotEnc = nums [4]; HorRotEnc = nums [5]; GripEncGlobal = nums [6]; X_axis = nums [7]; Y_axis = nums [8]; Z_axis = nums [9]; GripAngle = nums [10]; //println(Double.toString(X_axis/10));
println(data);
newData = true; }
}
The full message I am getting is: Error, disabling serialEvent() for /dev/cu.usbmodem2574541 null
The sketch will get the data one time than it hangs on this message.
Here is my code; I am getting the data from an arduino teensy 3.5 Please help.
Thanks
import processing.serial.*; Serial port; int BaseEncGlobal; int ElbowEncGlobal; int ShoEncGlobal; int VertRotEnc; int HorRotEnc; int GripEncGlobal;
double X_axis; double Y_axis; double Z_axis; double GripAngle;
String data; boolean newData = false;
PFont font;
void setup() { size(1280,800); //port = new Serial(this, "/dev/cu.usbserial-A50285BI", 115200); port = new Serial(this, "/dev/cu.usbmodem2574541", 115200); port.bufferUntil('\n'); font = loadFont("AgencyFB-Bold-200.vlw"); textFont(font, 40); }
void draw()
{ if (newData == true) {
int spaceDown = 55; background(0,0,0); fill(46, 209, 2); text(BaseEncGlobal, 70, spaceDown); fill(0, 102, 153); text(ShoEncGlobal, 70, spaceDown2); fill(0, 102, 153); text(ElbowEncGlobal, 70, spaceDown3); fill(0, 102, 153); text(VertRotEnc, 70, spaceDown4); fill(0, 102, 153); text(HorRotEnc, 70, spaceDown5); fill(0, 102, 153); text( GripEncGlobal, 70, spaceDown*6);
text(Double.toString(X_axis/10), 270, spaceDown ); newData =false; }
}
void serialEvent (Serial port) {
data = port.readStringUntil('\n'); if (data != null) { data = trim(data);
int[] nums = int(split(data, ',')); BaseEncGlobal = nums [1]; ShoEncGlobal = nums [2]; ElbowEncGlobal = nums [3]; VertRotEnc = nums [4]; HorRotEnc = nums [5]; GripEncGlobal = nums [6]; X_axis = nums [7]; Y_axis = nums [8]; Z_axis = nums [9]; GripAngle = nums [10]; //println(Double.toString(X_axis/10));
println(data);
newData = true; }
}
/*
void serialEvent (Serial port) { data = port.readStringUntil('.'); data = data.substring(0, data.length() - 1);
// look for the comma between Celcius and Farenheit index = data.indexOf(","); // fetch the C Temp temp_c = data.substring(0, index); // fetch the F Temp //temp_f = data.substring(index+1, index); temp_f = data.substring(1, data.length());
ElbowEncGlobal = getValue(data, ',', 2);
} */
Nothing changes. Still zeros and one picture. But previous println(distance) showed the numbers at least.. Could there be wrong datatypes for something?
import processing.serial.*;
PImage photo1, photo2, photo3;
Serial myPort;
String data="" ;
int distance;
void setup() {
size(900, 900); // size of processing window
photo1 = loadImage("photo1.jpg");
photo2 = loadImage("photo2.jpg");
photo3 = loadImage("photo3.jpg");
myPort = new Serial(this, "/dev/cu.usbmodemfd111", 9600);
myPort.bufferUntil('\n');
}
void draw() {
background(0);
println(distance);
if (distance <= 20) {
image(photo1, 0, 0); //display first photography
} else if (distance > 20 && distance < 40) {
image(photo2, 0, 0); //display 2nd photography
} else if (distance >= 40) {
image(photo3, 0, 0); //display 3rdphotography
}
}
void serialEvent(Serial myPort) {
data=myPort.readStringUntil('\n');
distance=int(data);
}
In consol the range of the sensor is from 5 up to 60 (where is the ceiling). After updating the code the picture still is the same one. And even if the data shows more than 20, it plays photo1.
import processing.serial.*;
PImage photo1, photo2, photo3;
Serial myPort;
String data="" ;
PFont myFont;
int distance;
void setup() {
size(900, 900); // size of processing window
photo1 = loadImage("photo1.jpg");
photo2 = loadImage("photo2.jpg");
photo3 = loadImage("photo3.jpg");
//background(0);// setting background color to black
myPort = new Serial(this, "/dev/cu.usbmodemfd121", 9600);
myPort.bufferUntil('\n');
}
void draw() {
println(data);
distance=int(data);
if (distance <= 20) {
image(photo1, 0, 0); //display first photography
} else if (distance > 20 && distance < 40) {
image(photo2, 0, 0); //display 2nd photography
} else if (distance >= 50) {
image(photo3, 0, 0); //display 3rdphotography
}
}
void serialEvent(Serial myPort) {
data=myPort.readStringUntil('\n');
}
So now I have in processing this code:
import processing.serial.*;
PImage photo1, photo2, photo3;
Serial myPort;
String data="" ;
PFont myFont;
int distance;
void setup(){
size(1366,900); // size of processing window
photo1 = loadImage("photo1.jpg");
photo2 = loadImage("photo2.jpg");
photo3 = loadImage("photo3.jpg");
//background(0);// setting background color to black
myPort = new Serial(this, "/dev/cu.usbmodemfd121", 9600);
myPort.bufferUntil('\n');
}
void draw(){
if(distance>50){
image(photo1, 0, 0); //display first photograpgy
}
else if (40<distance && distance<60) {
image(photo2, 0, 0);
}
else if (distance<40) {
image(photo3, 0, 0);
}
}
void serialEvent(Serial myPort){
data=myPort.readStringUntil('\n');
}
It plays only photo3. How can i refer distance to income values? I guess that the income is written in last lines data=myPort.readStringUntil('\n'); ?!
Hi,
I am just starting to explore posibilities to make something work in processing via arduino (sensors). On school exhibition I want to make a picture display that shows one image at the time on the wall (with a projector), depending how close or far person is located from distance sensor.
This is a sketch of what I am thinking about:
To see if it really works, I explored some tutorials that explains how to connect Arduino input with Processing. Currently, I have these codes in both programs:
for Arduino:
const int anPin1 = 0;
long distance1;
void setup() {
Serial.begin(9600); // sets the serial port to 9600
}
void read_sensors(){
/*
Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
*/
distance1 = analogRead(anPin1)/2;
}
void print_all(){
/*
Serial.print("S1");
Serial.print("inches");
*/
Serial.print(" ");
Serial.print(distance1);
Serial.println();
}
void loop() {
read_sensors();
print_all();
delay(50);
}
And for Processing:
import processing.serial.*;
Serial myPort;
String data="" ;
PFont myFont;
int distance;
void setup(){
size(1366,900); // size of processing window
//background(0);// setting background color to black
myPort = new Serial(this, "/dev/cu.usbmodemfd111", 9600);
myPort.bufferUntil('\n');
}
void draw(){
background(0);
textAlign(CENTER);
fill(255);
text(data,820,400);
textSize(100);
fill(#4B5DCE);
text(" Distance : cm",450,400);
noFill();
stroke(#4B5DCE);
}
void serialEvent(Serial myPort){
data=myPort.readStringUntil('\n');
}
And this distance reading works perfect!
Unfortunately didn’t found any specific example for what i am looking for. So the question is, how can I get, for example, 3 pictures working this way:
if(distance>60){
image(photo1, 0, 0); //display first photograpgy
}
else if (40<distance<60) {
image(photo2, 0, 0);
}
else if (distance<40) {
image(photo3, 0, 0);
}
Do I have to write something like that in draw ()? And how can I get income distance value as number from which depends displayed image? And should I upload any specific library for this? Do i have to write something also again in Arduino code?
Would be great if someone could suggest any examples or codes for this.
Hoping for advise, komats!!
I have my code working well enough, but not I'm having troubles... implementing it. I can manually start my .exe by double-clicking it, but I can't open it through the command prompt or Windows Task Scheduler. Other applications (Notepad, Word, Processing) open through these channels, just not my Processing code exported as an executable file. Any idea if this problem stems from my code or anything Processing related? Here's the code if it helps:
import processing.serial.*;
import ddf.minim.*;
Minim minim;
AudioPlayer stand;
AudioPlayer sit;
AudioPlayer move;
int lf = 10;
String myString = null;
Serial myPort;
int sensorValue = 0;
void setup() {
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.clear();
myString = myPort.readStringUntil(lf);
myString = null;
minim = new Minim(this);
stand = minim.loadFile("stand.mp3");
sit = minim.loadFile("sit.mp3");
move = minim.loadFile("move.mp3");
}
void draw() {
while (myPort.available() > 0) {
myString = myPort.readStringUntil(lf);
if (myString != null) {
myString = myString.trim();
if(myString.length() > 0) {
println(myString);
}
if(myString.equals("sit")){
move.pause();
stand.pause();
sit.rewind();
sit.play();
if(sit.isPlaying() == true) {
println("Sound");
}
}
if(myString.equals("stand")){
sit.pause();
move.pause();
stand.rewind();
stand.play();
if(stand.isPlaying() == true) {
println("Sound");
}
}
if(myString.equals("move")){
stand.pause();
sit.pause();
move.rewind();
move.play();
if(move.isPlaying() == true) {
println("Sound");
}
}
}
}
}
I have been working on this game and I tried to make a multiplayer function with a server and a client but I can't seem to get it working with many problems such as a chance that the server will send out a null pointer exception when a client disconnects. I have been able to get the players to show on the other user's screen but it would lag such as when you move, you would move 10 seconds later on the other screen. I can post a GitHub of all the code below but ill write in the important bits below:
GitHub:
https://github.com/rockyhawk64/Trimate.git
CLIENT:
Client Sending Info:
positions();
send = name[0].replaceAll("=", "-") + "=" + x + "=" + y + "=" + bulX + "=" + bulY + "=" + dirr + "=" + gun[0] + '\n';
client.write(send);
Placing Other People On The Screen:
void positions(){
if(masf != null){
for(int i = 0; i <= split(masf,'$').length-1; i++) {
String coll[] = split(masf, '$');
String col[] = split(coll[i], '=');if(col[0].equals(client.ip())){
//if the IP is itself
}else{
if(col[6].equals("down")){
image(Edown, x+width/2+camX-float(col[2]), y+height/2+camY-float(col[3]));
}
if(col[6].equals("up")){
image(Eup, x+width/2+camX-float(col[2]), y+height/2+camY-float(col[3]));
}
if(col[6].equals("left")){
image(Eleft, x+width/2+camX-float(col[2]), y+height/2+camY-float(col[3]));
}
if(col[6].equals("right")){
image(Eright, x+width/2+camX-float(col[2]), y+height/2+camY-float(col[3]));
}
}
}
}
}
Recieve Server Message:
void clientEvent(Client client) {
masf = client.readStringUntil('\n');
}
SERVER:
import processing.net.*;
Server server;
String iM = "";
int port = 5204;
String players = "";
String positions[] = {};
String col[];
void setup(){
size(800, 400,P2D);
server = new Server(this, port);
}
void draw(){
background(0);
Client client = server.available();
if (client != null) {
iM = client.readStringUntil('\n');
if(positions.length == 0){
String join[] = {client.ip() + "=" + iM};
positions = join;
}
String found = null;
String ip = client.ip();
if(positions.length != 0){
for(int i = 0; i <= positions.length-1; i++) {
if(positions != null){
if(ip.equals(split(positions[i], "=")[0])){
found = str(i);
break;
}
}
}
}
if(found != null){
positions[int(found)] = client.ip() + "=" + iM;
}else{
append(positions, client.ip() + "=" + iM);
}
server.write(join(positions, '$'));
}
if(positions != null){
col = new String[0];
for(int i = 0; i <= positions.length-1; i++) {
if(split(positions[i], "=")[0].equals(split(players, "$"))){
//not used
}else{
for(int ii = 0; col.length == positions.length; ii++) {
if(ii != i){
append(col, positions[ii]);
}
}
}
}
positions = col;
}
}
void serverEvent(Server server, Client client) {
players = players + "$" + client.ip();
println(client.ip() + " Joined The Game");
}
void disconnectEvent(Client someClient) {
col = new String[0];
for(int i = 0; i <= split(players, '$').length-1; i++) {
if(split(players, '$')[i].equals(someClient.ip())){
for(int ii = 0; col.length == split(players, '$').length; ii++) {
if(ii != i){
append(col, split(players, '$')[ii]);
}
}
}
}
players = join(col, '$');
}
Hello, I wanted to ask you if you know how I can color input inputs (4 inputs, 4 lines with different colors), I tried with stroke (); but I do not come out of it. many thanks in advance
processing code:
// Importiamo la libreria seriale (già inclusa nell'IDE Arduino)
import processing.serial.*;
// Creiamo l'oggetto porta seriale
Serial port;
// Dato ricevuto dalla porta seriale
int val1;
int val2;
int val3;
int val4;
// Array per contenere i dati
int[] values1;
int[] values2;
int[] values3;
int[] values4;
//*
int lf = 10; // Linefeed in ASCII
String myString = null;
Serial myPort; // The serial port
//*
void setup()
{
size(800, 800);
//*
// List all the available serial ports
printArray(Serial.list());
// Open the port you are using at the rate you want:
myPort = new Serial(this, Serial.list()[0], 9600);
//myPort.clear();
// Throw out the first reading, in case we started reading
// in the middle of a string from the sender.
myString = myPort.readStringUntil(lf);
myString = null;
//*
// port = new Serial(this, Serial.list()[0], 9600);
// Inizializziamo l'array di interi che conterrà i dati
values1 = new int[width]; values1 = new int[height];
values2 = new int[width]; values2 = new int[height];
values3 = new int[width]; values3 = new int[height];
values4 = new int[width]; values4 = new int[height];
// Questo comando prepara l'anti-aliasing per la grafica
smooth();
}
void draw(){
//*
while (myPort.available() >= 3)
{
myString = myPort.readStringUntil(lf);
if (myString != null) {
println(myString);
}
if (myPort.read() == 0xff)
{
val1 = (myPort.read() << 8) | (myPort.read());
val2 = (myPort.read() << 8) | (myPort.read());
val3 = (myPort.read() << 8) | (myPort.read());
val4 = (myPort.read() << 8) | (myPort.read());
}
}
// Per ogni elemento dell'array...
for (int i=0; i<width-1; i++)
{
// ... sposto i valori salvati di una posizione
values1[i] = values1[i+1];
values2[i] = values2[i+1];
values3[i] = values3[i+1];
values4[i] = values4[i+1];
}
// ... e aggiungo il valore appena letto
values1[width-1] = val1; values1[height-1] = val1;
values2[width-1] = val2; values2[height-1] = val2;
values3[width-1] = val3; values3[height-1] = val3;
values4[width-1] = val4; values4[height-1] = val4;
// Imposto il colore di sfondo a nero
background(255);
// Imposto il colore del disegno a bianco
stroke(0);
// Per ogni elemento dell'array...
for (int x=1; x<width; x++)
{
// ... disegno una linea:
// dalle coordinate del punto precedente...
line(width-x, height-1-getY1(values1[x-1])-100, width-x-1, height-1-getY1(values1[x])-100);
line(width-x, height-1-getY2(values2[x-1])-100, width-x-1, height-1-getY2(values2[x])-100);
line(width-x, height-1-getY3(values3[x-1])-100, width-x-1, height-1-getY3(values3[x])-100);
line(width-x, height-1-getY4(values4[x-1])-100, width-x-1, height-1-getY4(values4[x])-100);
}
}
int getY1(int val1) {return (int)(val1 / 1023.0f * height) - 1;};
int getY2(int val2) {return (int)(val2 / 1023.0f * height) - 1;};
int getY3(int val3) {return (int)(val3 / 1023.0f * height) - 1;};
int getY4(int val4) {return (int)(val4 / 1023.0f * height) - 1;};
Arduino code:
// Definiamo il pin 0 (RX) che trasmette dati
//#define ANALOG_IN 0
// Setup
void setup()
{
// Impostiamo la comunicazione seriale
Serial.begin(9600);
}
int value1;
// Loop
void loop()
{
// Leggiamo il valore del pin 0 (RX)
int val = analogRead(ANALOG_IN);
// Stampiamo un valore di 255: 8 bits impostati a 1 (11111111)
Serial.print("|velocita'=");
Serial.write( 0xff );
Serial.print(val, DEC);
Serial.write("\r\n");
}
Hi, programmers. I'm still stuck in this code. It seems the "if" statement can't evaluate the readings. So, the video won't stop. I don't know what I'm missing...
Here's the Processing code to play/stop only one video:
import processing.serial.*;
import processing.video.*;
Serial myPort;
String val;
Movie video1, video2, video3;
void setup() {
//fullScreen(SPAN);
size(640,480);
video1 = new Movie(this,"estas ahi.mp4");
//video2 = new Movie(this,"estas lejos.mp4");
//video3 = new Movie(this,"me gustaria estar aqui.mp4");
video1.loop();
//video2.loop();
//video3.loop();
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil('\n');
}
void serialEvent (Serial myPort) {
if (myPort.available() > 0) {
val=myPort.readStringUntil('\n');
}
if (val.equals("SI")) {
video1.stop();
//video2.stop();
//video3.stop();
} else {
video1.loop();
//video2.loop();
//video3.loop();
}
}
void draw() {
background(0);
//image(video1,0,0);
//image(video2,0,0);
//image(video3,0,0);
image(video1,320,120,640,480);
//image(video2,1600,200,640,480);
//image(video3,2880,120,640,480);
println(val);
}
void movieEvent(Movie video) {
video.read();
}
Please if you could check and point out what's wrong and so I can continue with the rest. It's my last week before I present this project! :-SS [-O<
What line gives you null?
Anyhow, the problem is sort of both. If you make your life easier, you change both codes. On the arduino side, you send a new line character after sending data. On the Processing side, you read the input data from your serial and you split your data based on the new line char. Then you process each of your tokens in the case you get multiple tokens which you can always check by inquiring for the length of the array in the split function (split/splitTokens ===> Reference)
By the way, notice in your setup: myPort.bufferUntil('\n');
but you are not sending this character in your arduino code. Furthermore, in serialEvent()
you should be working with https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html For the time being:
void serialEvent(final Serial s) {
val = s.readStringUntil('\n');
if (val==null)
return;
String in=split(val, '\n');
if (in.length!=0) {
if (in[0].equals("SI")) { //Processing only first token in case of multiples
video1.stop();
video2.loop();
video3.loop();
} else {
video1.loop();
video2.stop();
video3.stop();
}
}
}
Check the following post for reference:
https://processing.org/reference/split_.html
https://forum.processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_1
https://forum.processing.org/two/discussion/22640/can-anyone-help-me-find-what-s-wrong-with-my-code-for-a-video-player-based-off-sensor-reading#latest
Kf
Hi there.
I'm trying to play three different videos in three different monitors for an art project. I'm using three LED TVs which I set them in 1280x720 pixels each. The code (and the project) includes an ultrasonic sensor that allows to play or stop the videos, but the main issue is that I can't use the fullScreen(P2D, SPAN);
function(?). It gaves me the "Framebuffer objects are not supported by this hardware (or driver)..." error. What can I do?!. I searched for info but I didn't find anything.
So...If you remember any thread or info or a past project with this issue, please comment
Thank you thank you thank you!!
Here's the code:
//import processing.serial.Serial;
import processing.video.*;
//Serial myPort;
//String val;
Movie video1, video2, video3;
void settings() {
fullScreen(P2D,SPAN);
}
void setup() {
background(0);
video1 = new Movie(this, "estas ahi.mp4");
video2 = new Movie(this, "estas lejos.mp4");
video3 = new Movie(this, "me gustaria estar aqui.mp4");
video1.stop();
video2.loop();
video3.stop();
//String portName = Serial.list()[0];
//myPort = new Serial(this, portName, 9600);
//myPort.bufferUntil('\n');
}
//void serialEvent (Serial myPort) {
//if (myPort.available() > 0) {
//val=myPort.readStringUntil('\n');
//}
//}
//void serialEvent(final Serial s) {
//val = s.readString().trim();
//}
void draw() {
//println(val);
image(video1,480,320,640,480);
image(video2,120,0,640,480);
image(video3,200,0,640,480);
}
void movieEvent(Movie video) {
video.read();
}
I've used this code for multiple projects at this point without many issues but am now running into an unexpected problem which I am unsure how to solve. I have little to no experience with Processing but was getting some help from people a few months back ago when I started to use this code.
The project is a large LED panel installation on 40 panels, the code will run through processing on a raspberry pi so I am using this pi compatible version someone helped me find. I have one video which almost successfully runs on these 40 panels with a bit of lag (which isn't the worst for the project) but it is not the video I intend to use! In comparing it to the video which I do want to use I am not sure what the difference is. I made the first video which works a few months ago so I can't remember if I did anything different but both videos are made in final cut (mov) and then converted to mp4 online. In final cut I made the dimensions of both videos 640 x 360 and both were H264. The first video moves a bit slower and looks more lo-res but I believe those to be qualities from the original source before I imported and edited in FinalCut.
I believe the problem to be related to the amount/size of data being processed. I should mention that both videos will work according to processing and be recognized by the panels but the issue is that the video I want to use does not play consistently. One teensy may send part of the video to the LEDs mostly correct while another teensy's LEDs start to pick up after 10 seconds and then flickers a spectrum of random colors. None of the teensys seem to be synced up with this second video (though all teensys are mounted on the OctoBoards and connected to each other to sync.)
I'm wondering if anyone has any advice for what to do to the video to make it play correctly.
I have tried using HandBrake to also change some qualities of the video but have no clue what I'm doing really and all of my test have yielded no results.
I also noticed that the flickering happens more when theres a bigger change in color supposed to be happening
I suppose the problem could relate to how many rows I have, not sure, but the other video does seem to be working mostly properly. I am inserting some photos that show the project, some of the panels still need some work so thats why one is pink (wrong RGB ordered lights) and the all white ones just have no data input yet.
Anyone have any ideas?
I hope I entered this code correctly...
`/* OctoWS2811 movie2serial.pde - Transmit video data to 1 or more
Teensy 3.0 boards running OctoWS2811 VideoDisplay.ino
http://www.pjrc.com/teensy/td_libs_OctoWS2811.html
Copyright (c) 2013 Paul Stoffregen, PJRC.COM, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// To configure this program, edit the following sections:
//
// 1: change myMovie to open a video file of your choice ;-)
//
// 2: edit the serialConfigure() lines in setup() for your
// serial device names (Mac, Linux) or COM ports (Windows)
//
// 3: if your LED strips have unusual color configuration,
// edit colorWiring(). Nearly all strips have GRB wiring,
// so normally you can leave this as-is.
//
// 4: if playing 50 or 60 Hz progressive video (or faster),
// edit framerate in movieEvent().
//import processing.video.*;
import gohai.glvideo.*;
import processing.serial.*;
import java.awt.Rectangle;
GLMovie myMovie;
float gamma = 1.7;
int numPorts=0; // the number of serial ports in use
int maxPorts=24; // maximum number of serial ports
Serial[] ledSerial = new Serial[maxPorts]; // each port's actual Serial port
Rectangle[] ledArea = new Rectangle[maxPorts]; // the area of the movie each port gets, in % (0-100)
boolean[] ledLayout = new boolean[maxPorts]; // layout of rows, true = even is left->right
PImage[] ledImage = new PImage[maxPorts]; // image sent to each port
int[] gammatable = new int[256];
int errorCount=0;
float framerate=0;
void setup() {
String[] list = Serial.list();
delay(20);
println("Serial Ports List:");
println(list);
//serialConfigure("/dev/ttyACM1"); // change these to your port names
//serialConfigure("/dev/cu.usbmodem3550481");
serialConfigure("/dev/tty.usbmodem3999691");
//serialConfigure("/dev/cu.usbmodem3550481");
//serialConfigure("/dev/ttyACM0");
serialConfigure("/dev/tty.usbmodem3645941");
serialConfigure("/dev/tty.usbmodem3694501");
serialConfigure("/dev/tty.usbmodem3766451");
if (errorCount > 0) exit();
for (int i=0; i < 256; i++) {
gammatable[i] = (int)(pow((float)i / 255.0, gamma) * 255.0 + 0.5);
}
size(480, 400, P2D); // create the window
String mpath = sketchPath() + "/../../../media/x.mp4";
println(mpath);
myMovie = new GLMovie(this, mpath);
myMovie.loop(); // start the movie :-)
}
// movieEvent runs for each new frame of movie data
void movieEvent(GLMovie m) {
// read the movie's next frame
m.read();
//if (framerate == 0) framerate = m.getSourceFrameRate();
framerate = 30; // TODO, how to read the frame rate???
for (int i=0; i < numPorts; i++) {
// copy a portion of the movie's image to the LED image
int xoffset = percentage(m.width, ledArea[i].x);
int yoffset = percentage(m.height, ledArea[i].y);
int xwidth = percentage(m.width, ledArea[i].width);
int yheight = percentage(m.height, ledArea[i].height);
ledImage[i].copy(m, xoffset, yoffset, xwidth, yheight,
0, 0, ledImage[i].width, ledImage[i].height);
// convert the LED image to raw data
byte[] ledData = new byte[(ledImage[i].width * ledImage[i].height * 3) + 3];
image2data(ledImage[i], ledData, ledLayout[i]);
if (i == 0) {
ledData[0] = '*'; // first Teensy is the frame sync master
int usec = (int)((1000000.0 / framerate) * 0.75);
ledData[1] = (byte)(usec); // request the frame sync pulse
ledData[2] = (byte)(usec >> 8); // at 75% of the frame time
} else {
ledData[0] = '%'; // others sync to the master board
ledData[1] = 0;
ledData[2] = 0;
}
// send the raw data to the LEDs :-)
ledSerial[i].write(ledData);
}
}
// image2data converts an image to OctoWS2811's raw data format.
// The number of vertical pixels in the image must be a multiple
// of 8. The data array must be the proper size for the image.
void image2data(PImage image, byte[] data, boolean layout) {
int offset = 3;
int x, y, xbegin, xend, xinc, mask;
int linesPerPin = image.height / 8;
int pixel[] = new int[8];
for (y = 0; y < linesPerPin; y++) {
if ((y & 1) == (layout ? 0 : 1)) {
// even numbered rows are left to right
xbegin = 0;
xend = image.width;
xinc = 1;
} else {
// odd numbered rows are right to left
xbegin = image.width - 1;
xend = -1;
xinc = -1;
}
for (x = xbegin; x != xend; x += xinc) {
for (int i=0; i < 8; i++) {
// fetch 8 pixels from the image, 1 for each pin
pixel[i] = image.pixels[x + (y + linesPerPin * i) * image.width];
pixel[i] = colorWiring(pixel[i]);
}
// convert 8 pixels to 24 bytes
for (mask = 0x800000; mask != 0; mask >>= 1) {
byte b = 0;
for (int i=0; i < 8; i++) {
if ((pixel[i] & mask) != 0) b |= (1 << i);
}
data[offset++] = b;
}
}
}
}
// translate the 24 bit color from RGB to the actual
// order used by the LED wiring. GRB is the most common.
int colorWiring(int c) {
int red = (c & 0xFF0000) >> 16;
int green = (c & 0x00FF00) >> 8;
int blue = (c & 0x0000FF);
red = gammatable[red];
green = gammatable[green];
blue = gammatable[blue];
return (green << 16) | (red << 8) | (blue); // GRB - most common wiring
}
// ask a Teensy board for its LED configuration, and set up the info for it.
void serialConfigure(String portName) {
if (numPorts >= maxPorts) {
println("too many serial ports, please increase maxPorts");
errorCount++;
return;
}
try {
ledSerial[numPorts] = new Serial(this, portName);
if (ledSerial[numPorts] == null) throw new NullPointerException();
ledSerial[numPorts].write('?');
} catch (Throwable e) {
println("Serial port " + portName + " does not exist or is non-functional");
errorCount++;
return;
}
delay(250);
String line = ledSerial[numPorts].readStringUntil(10);
if (line == null) {
println("Serial port " + portName + " is not responding.");
println("Is it really a Teensy 3.0 running VideoDisplay?");
errorCount++;
return;
}
String param[] = line.split(",");
if (param.length != 12) {
println("Error: port " + portName + " did not respond to LED config query");
errorCount++;
return;
}
// only store the info and increase numPorts if Teensy responds properly
ledImage[numPorts] = new PImage(Integer.parseInt(param[0]), Integer.parseInt(param[1]), RGB);
ledArea[numPorts] = new Rectangle(Integer.parseInt(param[5]), Integer.parseInt(param[6]),
Integer.parseInt(param[7]), Integer.parseInt(param[8]));
ledLayout[numPorts] = (Integer.parseInt(param[5]) == 0);
numPorts++;
}
// draw runs every time the screen is redrawn - show the movie...
void draw() {
if (myMovie.available()) {
movieEvent(myMovie);
}
// show the original video
image(myMovie, 0, 80);
// then try to show what was most recently sent to the LEDs
// by displaying all the images for each port.
for (int i=0; i < numPorts; i++) {
// compute the intended size of the entire LED array
int xsize = percentageInverse(ledImage[i].width, ledArea[i].width);
int ysize = percentageInverse(ledImage[i].height, ledArea[i].height);
// computer this image's position within it
int xloc = percentage(xsize, ledArea[i].x);
int yloc = percentage(ysize, ledArea[i].y);
// show what should appear on the LEDs
image(ledImage[i], 240 - xsize / 2 + xloc, 10 + yloc);
}
}
// respond to mouse clicks as pause/play
boolean isPlaying = true;
void mousePressed() {
if (isPlaying) {
myMovie.pause();
isPlaying = false;
} else {
myMovie.play();
isPlaying = true;
}
}
// scale a number by a percentage, from 0 to 100
int percentage(int num, int percent) {
double mult = percentageFloat(percent);
double output = num * mult;
return (int)output;
}
// scale a number by the inverse of a percentage, from 0 to 100
int percentageInverse(int num, int percent) {
double div = percentageFloat(percent);
double output = num / div;
return (int)output;
}
// convert an integer from 0 to 100 to a float percentage
// from 0.0 to 1.0. Special cases for 1/3, 1/6, 1/7, etc
// are handled automatically to fix integer rounding.
double percentageFloat(int percent) {
if (percent == 33) return 1.0 / 3.0;
if (percent == 17) return 1.0 / 6.0;
if (percent == 14) return 1.0 / 7.0;
if (percent == 13) return 1.0 / 8.0;
if (percent == 11) return 1.0 / 9.0;
if (percent == 9) return 1.0 / 11.0;
if (percent == 8) return 1.0 / 12.0;
return (double)percent / 100.0;
}`
The code segments below come from Exploring Arduino Chapter 6. The Aruino code is Listing 6-6, and the processing code is listing 6-7. I run Listing 6-7. in the Processing IDE. But the IDE keeps reporting "Error opening serial port COM3: Port not found". I think its wants me to put the serial library into the same directory with the code directory. But I dont know how to find the serial library, to copy it to there. Please help, I am brand new to Processing.
Here is my Arduino code
//Sending POT value to the computer
const int POTpin=0; //POT on analog pin 0
int val; //For holding the measured pot value
void setup()
{ // put your setup code here, to run once:
Serial.begin(9600); //Start the serial I/O on COM 3
}
void loop()
{ // put your main code here, to run repeatedly:
val=map(analogRead(POTpin),0,1023,0,255); //Read and MAP the A/D value to the color range
Serial.println(val); //Send val to computer to be read by processor
delay(100); //delay keeps from overruning the buffer at the computer
}
//Processing Sketch to read Arduino POT value and Change color on screen
//import and initialize serial port library
import processing.serial,*;
Serial port;
float brightness = 0; //for holding the value from the pot
void setup()
{
size(500,500); //window size
port = new Serial(this, "COM3", 9600); //set up serial
port.bufferUntil('\n'); //set up port to read until newline
}
void draw ()
{
background(0,0,brightness); //update the color in the window
}
void serialEvent (Serial port)
{
brightness = float(port.readStringUntil('\n')); //get a value from arduino
}
Hello strangers, I tried to programm a ball running on my screen in the direction of my Joystick (connected to the Arduino). I wrote the full programm but now I got an error that says: "ArrayIndexOutOfBoundsException: 1". The confusing about this error is, that like every 5 tries to start the programm are a success and I can play my little game as long as I want without any issues. I have no idea what could be wrong so maybe someone of you can help me.
The error has to be in line 28 of my Processing Code.
Arduino Code:
const int xPin = 0;
const int yPin = 1;
int xVal;
int yVal;
String data;
void setup() {
Serial.begin(9600);
pinMode(xPin,INPUT);
pinMode(yPin,INPUT);
}
void loop() {
xVal = analogRead(xPin);
yVal = analogRead(yPin);
data = dataNormalize(xVal,yVal);
Serial.println(data);
delay(50);
}
String dataNormalize(int x,int y) {
String xString = String(x);
String yString = String(y);
String data = xString+","+yString;
return data;
}
Processing Code:
import processing.serial.*;
Serial mySerial;
String myString;
int nl = 10;
float xVal;
float yVal;
int x=500;
int y=50;
void setup() {
frameRate(20);
size (1000,1000);
String myPort = Serial.list() [0];
mySerial = new Serial (this, myPort, 9600);
}
void draw() {
background(#5EC67C);
while (mySerial.available() > 0) {
myString = mySerial.readStringUntil(nl);
if (myString != null) {
String [] data = splitTokens(myString,",");
xVal = (float(data [0])-513)/50;
yVal = (float(data [1])-509)/50;
}
}
stroke(0);
fill(255,0,0);
ellipse(x,y,20,20);
x += xVal;
y += yVal;
println("X-Position: "+x+" Y-Position: "+y+" xVal: "+xVal+" yVal: "+yVal);
}
non capisco
English please
here, when you click the mouse, values are plotted AND in the table
import processing.serial.*;
ArrayList<PVector> points = new ArrayList();
Serial myPort; // The serial port:
float val;//i datiricevutidalla porta seriale
int lf = 10; // ASCII linefeed
int i;
void setup() {
size(900, 800);
for (int i = 0; i < 13; i++) {
// points.add(new PVector(i, 20 + 10*noise(i*0.1)));
}//for
//background(190);
//myPort = new Serial(this, "com3", 9600);
//myPort.bufferUntil(lf);
}
void draw() {
// background(190);
// show data
int y=50;
//for (PVector val : points) {
for (int i2 = 0; i2 < points.size(); i2++) {
PVector val = points.get(i2);
// show data
text(1, 350, y);
text(+val.y, 400, y);
y+=20; //next line
// middle line
// line( 190, 30, 190, 440);
if (i2 < points.size()-1) {
PVector val2 = points.get(i2+1);
stroke(255, 2, 2);
line(val.x*3, val.y,
val2.x*3, val2.y);
}
}//for
}//
void mousePressed() {
val = float(mouseX);
points.add(new PVector(i, val));
i++;
}
void serialEvent (Serial myPort) {
// // get the ASCII string:
// // ottiene la stringa ASCII:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// // trim off any whitespace:
inString = trim(inString);
val = float(inString);
println(val);
}
}
Ciao, grazie per il tuo interessamento, il grafico usa le cordinate "val e xPos( aggiornato ad ogni "mousePressed"), non capisco come nello stesso momento compilare la tabella con "val" aggiornati ad ogni azione di "mousePressed"... Grazie...
import processing.serial.*;
ArrayList<PVector> points = new ArrayList();
Serial myPort; // The serial port:
float val;//i datiricevutidalla porta seriale
int lf = 10; // ASCII linefeed
void setup() {
size(900, 800);
for (int i = 0; i < 13; i++) {
points.add(new PVector(i, 20 + 10*noise(i*0.1)));
}//for
//background(190);
myPort = new Serial(this, "com3", 9600);
myPort.bufferUntil(lf);
}
void draw() {
// background(190);
// show data
int y=50;
for (PVector val : points) {
// show data
text(1, 350, y);
text(+val.y, 400, y);
y+=20; //next line
// middle line
// line( 190, 30, 190, 440);
}//for
}//
void serialEvent (Serial myPort) {
// // get the ASCII string:
// // ottiene la stringa ASCII:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// // trim off any whitespace:
inString = trim(inString);
val = float(inString);
println(val);
}
}
Hi, I'm a processing pivot. I have a problem I have a value taken from Arduino, and I visualize it in a graph, but I would also like to be able to visualize it through a numerical table. How can I do? thank you!
import processing.serial.*;
import grafica.*;
Serial myPort; // The serial port:
float val;//i datiricevutidalla porta seriale
int xPos=0;
//Stringa di input dalla porta seriale:
int lf = 10; // ASCII linefeed
GPlot plot1, plot2;
void setup() {
size(440, 520);
// Create the first plot Crea il primo grafico
plot1 = new GPlot(this);
plot1.setPos(0, 0);
plot1.setMar(0, 100, 0,100);
plot1.setDim(300, 300);
plot1.setAxesOffset(4);
plot1.setTicksLength(4);
// Create the second plot with the same dimensions
//Crea il secondo grafico con le stesse dimensioni
plot2 = new GPlot(this);
// Prepare the points Prepara i punti
int val =0;
GPointsArray points = new GPointsArray(val);
for (int i = 15; i < 13; i++) {
points.add(i, 20 + 10*noise(i*0.1));
if(xPos==13){
}
}
// Set the points, the title and the axis labels
// Imposta i punti, il titolo e le etichette degli assi
plot1.setPoints(points);
plot1.setTitleText("Temperature");
plot1.getYAxis().setAxisLabelText("MASSA ARIA (?)");
plot1.getXAxis().setAxisLabelText("APERTURA VALVOLA (mm)");
plot1.activatePanning();
myPort = new Serial(this, "com3", 9600);
myPort.bufferUntil(lf);
}
void draw() {
background(255);
println(val);
fill(0);
text("val "+val,200,450);
//Disegna la prima trama
plot1.beginDraw();
plot1.drawBox();
plot1.drawXAxis();
plot1.drawYAxis();
plot1.drawTitle();
//plot1.drawPoints();//pallinorosso sul valore
plot1.drawGridLines(GPlot.BOTH);
plot1.drawLines();
plot1.endDraw();
}
void serialEvent (Serial myPort) {
// // get the ASCII string:
// // ottiene la stringa ASCII:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// // trim off any whitespace:
inString = trim(inString);
val = float(inString);
println(val);
}
}
void mousePressed(){
GPoint lastPoint = plot2.getPointsRef().getLastPoint();
if (lastPoint == null) {
plot1.addPoint(xPos, +val, "(" + str(xPos) + " , " + str(val) + ")");
}
else if (!lastPoint.isValid() || sq(lastPoint.getX() - xPos) + sq(lastPoint.getY() + val) > 2500) {
}
if( mouseButton == LEFT ) xPos+=1;
fill(0);
text ("val"+val,200,480);
}
I have a simple push button connected to pin 2 on my arduino. I can read this value and print it to the serial monitor without any trouble:
``` int pushButton = 2;
void setup() {
Serial.begin(9600);
pinMode(pushButton, INPUT);
}
void loop() {
int buttonState = digitalRead(pushButton);
Serial.println(buttonState);
delay(1); // delay in between reads for stability
}
```
This prints a value of 0 when the button is not pressed, and 1 if it is.
In processing I want to check if the value is zero or one, and perform some conditional logic. But I can't get the equality right:
``` Serial myPort; String resultString;
void setup(){
size(640,480);
printArray(Serial.list());
String portName = Serial.list()[2];
myPort = new Serial(this,portName,9600);
myPort.bufferUntil(10);
}
void draw(){
//
}
void serialEvent(Serial myPort){
String inputString = myPort.readStringUntil(10); //until newline or ("\n");
inputString = trim(inputString);
println(inputString);
if (inputString == "1"){ //this doesn't work, even though println will render ("1") plus the newline if button is pushed.
println("on");
} else {
println("off");
}
}
```
How do I set up this conditional to do one thing if the button is pushed, and another if it is not?
I've tried converting the string to an int using
inputInt = Integer.parseInt(inputString);
and then performing the check, but it didn't work.
Hello, everyone. I am using an Arduino MEGA 2560 and ArduCAM OV2640 to take a JPEG picture and display it on my PC through a Processing sketch. The current Arduino and Processing sketches let me take single shots only. I am wondering how I can make it so the camera will take pictures continuously and display it on the PC, similar to a video stream. Here are the sketches, courtesy of Vernel. (https://github.com/Vernel/ArduCAM_OV562
Processing sketch:
import processing.serial.*; // import the Processing serial library
Serial myPort; // The serial port
//NEW ADDITIONS
byte rawBytes[];
int sensorNum = 0;
PImage img;
long picNum = 0;
void setup() {
size(640, 480);
// List all the available serial ports
// if using Processing 2.1 or later, use Serial.printArray()
printArray(Serial.list());
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
myPort = new Serial(this, Serial.list()[0], 250000);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');
// draw with smooth edges:
smooth();
img = createImage(320, 240, RGB);
img.loadPixels();
//frameRate(600);
myPort.write(0x10);
}
void draw() {
background(255);
image(img, 0, 0, width, height-100);
fill(0);
text("Help:", 20, height-80);
text("- Press key 1-7 to change image resolution", 30, height-60);
text("- Press key S to capture a still photo", 30, height-40);
text("- Press key C to enable/disable StopMotion capture", 30, height-20);
}
// serialEvent method is run automatically by the Processing applet
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():
void serialEvent(Serial myPort) {
while (myPort.available() > 0) {
String incoming[];
String myString = myPort.readStringUntil('\n');
myString = trim(myString);
incoming = split(myString, ',');
if (incoming.length > 1) {
if (incoming[0].equals("FifoLength:")) {
//initialize raw data byte array to the size of the picture
rawBytes = new byte[int(incoming[1])];
println("Picture Size: "+incoming[1]+" bytes");
} else if (incoming[0].equals("Image:")) {
int x = 0;
for (int i = 1; i < incoming.length; i++) {
try {
//add raw jpeg incoming bytes to byte array
rawBytes[x]= (byte)int(incoming[i]);
x++;
}
catch(RuntimeException e) {
println(e.getMessage());
}
}
try {
//Save raw data to file
String fname = "capture#"+picNum+"_"+day()+month()+year()+".jpg";
saveBytes("data/capture/"+fname, rawBytes);
// Open saved picture for local display
img = loadImage("/data/capture/"+fname);
picNum++;
}
catch(RuntimeException e) {
println(e.getMessage());
}
} else if (incoming[0].equals("Ready:")) {
myPort.write(0x10);
println("Starting Capture");
}
} else {
println(myString);
}
}
}
void keyPressed() {
switch(key) {
case 's':
myPort.write(0x10);
println("Starting Capture");
break;
case 'c':
myPort.write(7);
break;
case '1':
myPort.write(0);
println("Set Image Resolution: 320x240");
break;
case '2':
myPort.write(1);
println("Set Image Resolution: 640x480");
break;
case '3':
myPort.write(2);
println("Set Image Resolution: 1024x768");
break;
default:
println("Unknown Command: "+key);
break;
}
}
Arduino Sketch:
#include <Wire.h>
#include <ArduCAM.h>
#include <SPI.h>
#include "memorysaver.h"
//This code can only work on OV5642_MINI_5MP_PLUS platform.
#if !(defined (OV2640_MINI_2MP))
#endif
#define FRAMES_NUM 0x00
// set pin 4 as the slave select for SPI:
const int CS1 = 7;
bool CAM1_EXIST = false;
bool stopMotion = false;
ArduCAM myCAM1(OV2640, CS1);
long int streamStartTime;
void setup() {
// put your setup code here, to run once:
uint8_t vid, pid;
uint8_t temp;
Wire.begin();
Serial.begin(250000); //921600
Serial.println(F("ArduCAM Start!"));
// set the CS output:
pinMode(CS1, OUTPUT);
// initialize SPI:
SPI.begin();
//Check if the 4 ArduCAM Mini 5MP PLus Cameras' SPI bus is OK
while (1) {
myCAM1.write_reg(ARDUCHIP_TEST1, 0x55);
temp = myCAM1.read_reg(ARDUCHIP_TEST1);
if (temp != 0x55)
{
Serial.println(F("SPI1 interface Error!"));
} else {
CAM1_EXIST = true;
Serial.println(F("SPI1 interface OK."));
}
if (!(CAM1_EXIST)) {
delay(1000); continue;
} else
break;
}
while (1) {
//Check if the camera module type is OV5642
myCAM1.rdSensorReg8_8(OV2640_CHIPID_HIGH, &vid);
myCAM1.rdSensorReg8_8(OV2640_CHIPID_LOW, &pid);
if ((vid != 0x26 ) && (( pid != 0x41 ) || ( pid != 0x42 ))) {
Serial.println(F("Can't find OV2640 module!"));
delay(1000); continue;
} else {
Serial.println(F("OV2640 detected.")); break;
}
}
//Change to JPEG capture mode and initialize the OV2640 module
myCAM1.set_format(JPEG);
myCAM1.InitCAM();
//myCAM1.write_reg(ARDUCHIP_TIM, VSYNC_LEVEL_MASK); //VSYNC is active HIGH
myCAM1.clear_fifo_flag();
//myCAM1.write_reg(ARDUCHIP_FRAMES, FRAMES_NUM);
//myCAM1.OV5642_set_JPEG_size(OV5642_640x480); delay(1000);
myCAM1.OV2640_set_JPEG_size(OV2640_320x240); delay(1000);
delay(1000);
myCAM1.clear_fifo_flag();
Serial.println("Ready:,1");
}
void loop() {
if (CAM1_EXIST && stopMotion) {
streamStartTime = millis();
myCAMSendToSerial(myCAM1);
double fps = ((millis() - streamStartTime) / 1000);
Serial.println("fps: " + String(1 / fps ));
}
}
void myCAMSendToSerial(ArduCAM myCAM) {
char str[8];
byte buf[5];
static int i = 0;
static int k = 0;
uint8_t temp = 0, temp_last = 0;
uint32_t length = 0;
bool is_header = false;
myCAM.flush_fifo(); //Flush the FIFO
myCAM.clear_fifo_flag(); //Clear the capture done flag
myCAM.start_capture();//Start capture
while (!myCAM.get_bit(ARDUCHIP_TRIG , CAP_DONE_MASK));
length = myCAM.read_fifo_length();
Serial.print(F("FifoLength:,"));
Serial.print(length, DEC);
Serial.println(",");
if (length >= MAX_FIFO_SIZE) //8M
{
Serial.println(F("Over size."));
return ;
}
if (length == 0 ) //0 kb
{
Serial.println(F("Size is 0."));
return ;
}
myCAM.CS_LOW();
myCAM.set_fifo_burst();
Serial.print("Image:,");
while ( length-- )
{
temp_last = temp;
temp = SPI.transfer(0x00);
//Read JPEG data from FIFO
if ( (temp == 0xD9) && (temp_last == 0xFF) ) //If find the end ,break while,
{
buf[i++] = temp; //save the last 0XD9
//Write the remain bytes in the buffer
myCAM.CS_HIGH();
for (int i = 0; i < sizeof(buf); i++) {
Serial.print(buf[i]); Serial.print(",");
}
Serial.println();
Serial.println(F("Image transfer OK."));
is_header = false;
i = 0;
}
if (is_header == true)
{
//Write image data to buffer if not full
if (i < 5) {
buf[i++] = temp;
} else
{
//Stream 5 bytes of raw image data to serial
myCAM.CS_HIGH();
for (int i = 0; i < sizeof(buf); i++) {
Serial.print(buf[i]); Serial.print(",");
}
i = 0;
buf[i++] = temp;
myCAM.CS_LOW();
myCAM.set_fifo_burst();
}
}
else if ((temp == 0xD8) & (temp_last == 0xFF))
{
is_header = true;
buf[i++] = temp_last;
buf[i++] = temp;
}
}
}
void serialEvent() {
if (Serial.available() > 0) {
uint8_t temp = 0xff, temp_last = 0;
uint8_t start_capture = 0;
temp = Serial.read();
switch (temp)
{
case 0:
temp = 0xff;
myCAM1.OV2640_set_JPEG_size(OV2640_320x240);
Serial.println(F("OV2640_320x240")); delay(1000);
myCAM1.clear_fifo_flag();
break;
case 1:
temp = 0xff;
myCAM1.OV2640_set_JPEG_size(OV2640_640x480);
Serial.println(F("OV2640_640x480")); delay(1000);
myCAM1.clear_fifo_flag();
break;
case 2:
temp = 0xff;
myCAM1.OV2640_set_JPEG_size(OV2640_1024x768);
Serial.println(F("OV2640_1024x768")); delay(1000);
myCAM1.clear_fifo_flag();
break;
case 3:
{
if (stopMotion)
stopMotion = false;
else
stopMotion = true;
Serial.println("Stop Motion Enabled: " + String(stopMotion));
}
break;
case 0x10:
if (CAM1_EXIST) {
streamStartTime = millis();
myCAMSendToSerial(myCAM1);
double fps = ((millis() - streamStartTime) / 1000);
Serial.println("Total Time: " + String(fps));
}
break;
default:
break;
}
}
}
Thanks in advance!