Hello everyone
I've built a nice little x/y plotter (Dropbox - Error) with 2 stepper motors that "prints" 128x128 bitmap images.
Right now, as you can see in the code, the Image is stored as bytes in a huge 2d array.
I would like a way to use Processing to import a 128x128 png image, convert it to a binary image, and then send it to the arduino via serial connection.
Sadly i'm a huge noob in processing and serial connections and also couldn't find anything on the internet that does such a thing. Could someone please help me create the processing application and change the arduino code?
thanks in advance
dabre
ps.: sorry for my bad english , i'm swiss.
this is my code:
#include <Stepper.h>
#include <avr/pgmspace.h>
#define STEPS 48
int penPlusA = 10;
int penPlusB = 11;
int penMinusA = 12;
int penMinusB = 13;
Stepper stepperX(STEPS, 0,1,2,3);
Stepper stepperY(STEPS, 4,5,6,7);
int limitPin = 8;
int stopSwitch = 9;
int x_inc = 0;
int y_inc = 0;
int z_pos = 0;
int stepSizeX = 6;//max 8
int stepSizeY = round(stepSizeX*1.6);
int maxSteps = 1075;
int homePos = (maxSteps/2)-((128*stepSizeX)/2);
String color = "color";// color, draft, line, square or cross
int motorSpeed = 280;
const byte image[128][128] PROGMEM = //insert image here, I deleted the array definition because the code would be too big
{};
void setup()
{
if(homePos<0){homePos = 0;}
if(color == "square" ){motorSpeed = 200;}else if(color == "draft" || color == "line"){motorSpeed = 300;}
stepperX.setSpeed(motorSpeed);
stepperY.setSpeed(motorSpeed);
pinMode(penPlusA, OUTPUT);
pinMode(penPlusB, OUTPUT);
pinMode(penMinusA, OUTPUT);
pinMode(penMinusB, OUTPUT);
pinMode(limitPin, INPUT);
digitalWrite(limitPin, HIGH);
pinMode(stopSwitch, INPUT);
digitalWrite(stopSwitch, HIGH);
Serial.begin(9600);
delay(2000);
}
void loop()
{
goHome();
draw();
while(1){
}
}
void penDown()
{
if (z_pos == 0){
digitalWrite(penPlusA, HIGH);
digitalWrite(penPlusB, HIGH);
digitalWrite(penMinusA, LOW);
digitalWrite(penMinusB, LOW);
delay(500);
digitalWrite(penPlusA, LOW);
digitalWrite(penPlusB, LOW);
digitalWrite(penMinusA, LOW);
digitalWrite(penMinusB, LOW);
z_pos = 100;
}
}
void penUp()
{
if(z_pos == 100){
digitalWrite(penPlusA, LOW);
digitalWrite(penPlusB, LOW);
digitalWrite(penMinusA, HIGH);
digitalWrite(penMinusB, HIGH);
delay(500);
digitalWrite(penPlusA, LOW);
digitalWrite(penPlusB, LOW);
digitalWrite(penMinusA, LOW);
digitalWrite(penMinusB, LOW);
z_pos = 0;
}
}
void colorIn(int dir){
penDown();
for(int i = 0;i<stepSizeX;i+=1){
if(i%2 == 0){
stepperY.step(stepSizeY);
}
else{
stepperY.step(-stepSizeY);
}
stepperX.step(1*dir);
}
}
void sqr(int dir){
penUp();
stepperX.step(stepSizeX*dir);
penDown();
stepperY.step(stepSizeY);
stepperX.step(-stepSizeX*dir);
stepperY.step(-stepSizeY);
stepperX.step(stepSizeX*dir);
}
void draft(int dir){
penDown();
for(int i = 0;i<2*stepSizeX;i++){
if (i%2 == 0){
stepperY.step(1);
}else{
stepperX.step(1*dir);
}
}
//penUp();
stepperY.step(-stepSizeX);
}
void line(int dir){
penDown();
stepperX.step(round(stepSizeX*dir));
}
void cross(int dir){
penDown();
for(int i = 0;i<2*stepSizeX;i++){
if (i%2 == 0){
stepperY.step(1);
}else{
stepperX.step(1*dir);
}
}
penUp();
stepperX.step(-stepSizeX*dir);
penDown();
for(int i = 0;i<2*stepSizeX;i++){
if (i%2 == 0){
stepperY.step(-1);
}else{
stepperX.step(1*dir);
}
}
delay(50);
}
void goHome(){
while(digitalRead(limitPin) == HIGH){
stepperX.step(-1);
}
stepperX.step(homePos);
}
void draw(){
for(int row = 0; row < 128; row+=2) {
penUp();
stepperX.step(-x_inc);
x_inc = 0;
for(int column = 0; column < 128; column++){
int pixel = pgm_read_byte(&(image[row][column]));
int thisDir = 1;
if(digitalRead(stopSwitch) == 0){
while(digitalRead(stopSwitch) == 0){
penUp();
delay(10);
}
}
Serial.println(String(row) +"," + String(column) + "," +String(pixel));
if(pixel == 1){
if(color == "color"){colorIn(thisDir);}
else if(color == "square"){sqr(thisDir);}
else if (color == "draft"){draft(thisDir);}
else if(color == "line"){line(thisDir);}
else{cross(thisDir);}
x_inc += stepSizeX;
}
else{
penUp();
stepperX.step(stepSizeX);
x_inc += stepSizeX;
}
}
penUp();
stepperY.step(stepSizeY);
y_inc += stepSizeY;
delay(15);
for(int column = 127; column >= 0; column--){
int pixel = pgm_read_byte(&(image[row+1][column]));
int thisDir = -1;
if(digitalRead(stopSwitch) == 0){
while(digitalRead(stopSwitch) == 0){
penUp();
delay(stepSizeX);
}
}
Serial.println(String(row+1) +"," + String(column)) + "," +String(pixel);
if(pixel == 1){
if(color == "color"){colorIn(thisDir);}
else if(color == "square"){sqr(thisDir);}
else if (color == "draft"){draft(thisDir);}
else if(color == "line"){line(thisDir);}
else{cross(thisDir);}
x_inc -= stepSizeX;
}
else{
penUp();
stepperX.step(-stepSizeX);
x_inc -= stepSizeX;
}
}
penUp();
stepperY.step(stepSizeY);
y_inc += stepSizeY;
delay(15);
}
penUp();
stepperX.step(-x_inc);
stepperY.step(5*stepSizeY);
}