I am just a beginner. This my processing code; stil lhave a little trouble with the clear part of the input function . but main thing is the arduino write.out. My arduino code works. with the serial port. The processing code connects to the arduino but the data is not registering. Thank you for any help.
//generate color barcode by typing input ,
//save it to computer and broadcast the colors
import processing.net.*;
import processing.serial.*;
import java.util.*;
import java.lang.reflect.*;
import java.security.*;
import java.awt.Graphics2D;
import controlP5.*;
ControlP5 cp5;
import processing.serial.*;
Serial arduinoPort;
//Start with an array to store the colorbars.
int[] colorLines;
int[] red;
int[] green;
int[] blue;
int n;
int r1;
int g1;
int b1;
int time;
int rate1;
int rate2;
int rate3;
boolean hasBeenDrawn = false;
boolean hasBeenInput = false;
String inputText;
boolean colorCodeClear = false;
String textValue;
void setup(){
size(400,280);
// Create the array for width of lines.
{n = 400;
colorLines = new int[n];
red = new int[n];
green = new int[n];
blue = new int[n];
// Populate the array with some values.
for( int i=0; i < colorLines.length; i++){
strokeCap(SQUARE);
int sW = int(random(20,40));
strokeWeight(sW);
colorLines[i] = sW;
}
//noLoop();
}
// set arduino port
arduinoPort = new Serial(this,"/dev/tty.usbmodem1411", 9600 );
//create input field via CP5
cp5 = new ControlP5(this);
cp5.addTextfield("Enter Name")
.setPosition(0, 10)
.setSize(200,30)
.setAutoClear(false)
.setFont(createFont("times",20))
.setColor(255);
//cp5.getController("Enter Name").getCaptionLabel().setColor(color(148,0,211) );
cp5.getController("Enter Name").getCaptionLabel().setSize(14);
cp5.addBang("clear")
.setPosition(240,10)
.setSize(100,20)
.getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
;
}
void draw()
{
background(0);
inputText = cp5.get(Textfield.class,"Enter Name").getText();
text(inputText,50,70);
}
void keyPressed() {
if (keyCode == ENTER)
{
{colorCode();
broadcast();
println(inputText);
if (hasBeenDrawn == false)
{
{saveColorCode();
hasBeenDrawn = true;
cp5.getController("Enter Name").getCaptionLabel().setSize(2);
}
}
}
}
}
void colorCode()
{
//Draw each line.
for( int i = 0; i < 400; i += 16 )
{
// Give each line a random color and width each time it's drawn.
strokeWeight(colorLines[i]);
float r = int(random(255));
red[i]=int(r);
float g = int(random(255));
green[i] = int(g);
float b = int(random(255));
blue[i] = int(b);//blue
stroke(r, g, b);
line(i, 60 ,i, height);
}
}
//ready for led turn ons using Arduino RGB control 2
void broadcast()
{
for( int i = 0; i < 400; i += 16 )
{
r1 = red[i];
g1 = green[i];
b1 = blue[i];
time = colorLines[i];
byte out[] = new byte[4];
out[0] = byte(r1);
out[1] = byte(g1);
out[2] = byte(b1);
//send carriage return character
out[3] = byte('\r');
println(out);
arduinoPort.write(out);
//println(out);
}
}
void saveColorCode() {
g = createGraphics(400,200);
//this.height = dim;
//this.width = dim;
g.beginDraw();
for (int i = 0; i < n; i += 16 )
{
strokeWeight(colorLines[i]);
stroke(red[i],green[i],blue[i]);
line(i, 0, i , 250);
//put input on top of colorcode
}
g.textSize(48);
g.fill(red[0],green[0],blue[0]);
g.text(inputText,50,50);
g.endDraw();
save(inputText);
println("screenshot saved");
}
public void clear() {
cp5.get(Textfield.class,"Enter Name").clear();
cp5.getController("Enter Name").getCaptionLabel().setSize(14);
colorCodeClear = true;
}
void mousePressed(){
g.beginDraw();
g.clear();
g.endDraw();
//rect(0,20,400,200);
}
void controlEvent(ControlEvent theEvent) {
if(theEvent.isAssignableFrom(Textfield.class)) {
println("controlEvent: accessing a string from controller '"
+theEvent.getName()+"': "
+theEvent.getStringValue()
);
}
}
public void input(String theText) {
// automatically receives results from controller input
println("a textfield event for controller 'input' : "+theText);
}
this is my arduino code
// Example 4 - Receive a number as text and convert it to an int
const byte numChars = 4;
char receivedChars[numChars]; // an array to store the received data
byte values[3];
boolean newData = false;
int i = -1;
int dataNumber = 0;
int colorData;
// new for this version
const int redPin = 5;
const int greenPin = 6;
const int bluePin = 3;
void setup() {
Serial.begin(9600);
//
Serial.println("<Arduino is ready>");
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
recvWithEndMarker();
showNewNumbers();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
if (Serial.available() > 0) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewNumbers() {
if (newData == true) {
i++;
Serial.println(i);
dataNumber = atoi(receivedChars);
Serial.print("This just in ... ");
Serial.println(receivedChars);
Serial.print("Data as Number ... "); // new for this version
Serial.println(dataNumber);
values[i] = dataNumber;
Serial.println(values[i]);
Serial.println(i);
if (i == 2) {
Serial.print("i is ...");
Serial.println(i);
broadcastNewColor();
i = -1;
}
}
newData = false;
}
void broadcastNewColor(){
Serial.print("colors as Number ... ");
analogWrite(redPin, values[0]);
Serial.println(values[0]);
analogWrite(greenPin,values[1]);
Serial.println(values[1]);
analogWrite(bluePin, values[2]);
Serial.println(values[2]);
delay(10);
}```
I think I am still not understanding delay just yet either.