I've been using the image code generated from Github to print a picture, but there are some errors when i'm trying to upload the code.
I've changed the code text and added some comments in it.
I'm new to this environment
![]()
Thanks everyone for helping me out
![]()
we accidently broke our switch during soldering , so we uses pushbutton to pause
// main code
#include "pen.h"
#include "image.h"
#define imageX 392
#define imageY 504
#define Switch 7
#define pushButton 3
#define led2 5
#define led1 4
int on1 = 0;
int on2 = 0;
int dir = 0;
long pixelNum = 0;
void setup () {
pinMode (Switch, INPUT);
pinMode (pushButton, INPUT);
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
//Serial.begin (115200);
}
void loop () {
Pen pen;
on2 = 0;
// pause until pushbutton is pushed
while (on2 == 0)
{
// blink blue led while pausing
on2 = digitalRead (pushButton);
delay (100);
digitalWrite (led2, LOW);
delay (100);
digitalWrite (led2, HIGH);
}
for (int i=0; i<3; i++)
{
// blink green led before starting
digitalWrite (led1, HIGH);
delay (100);
digitalWrite (led1, LOW);
delay (100);
}
delay (100);
for(int i=0; i<imageY; i++)
{
for(int j=0; j<imageX; j++)
{
on2 = digitalRead (pushButton);
// if pushbutton is pressed during printing
if (on2 == 1)
{
// blink Geen and Blue leds
on2 = digitalRead (pushButton);
digitalWrite (led2, HIGH);
digitalWrite (led1, HIGH);
delay (1000);
digitalWrite (led2, HIGH);
digitalWrite (led1, HIGH);
delay (500);
while (1) {
// the loop keeps on until the pushbutton is pushed again
// blink Blue led
digitalWrite (led2, HIGH);
delay (100);
digitalWrite (led2, LOW);
delay (100);
on1 = digitalRead (Switch);
on2 = digitalRead (pushButton);
// if slide switch is on, reset the printer
// move the cnc back
if (on1 == 0)
{
digitalWrite (led1, HIGH); // led Green on
pen.accelerate (i, 1, (dir+1)%2); // the X axis in opposite direction
delay (1000);
pen.accelerate (j, 0, (dir+1)%2); // the Y axis in opposite direction
delay (500);
goto Finished;
}
// if pushbutton is pushed again
// continue to print
if (on2 == 1)
{
// blink Blue and Green leds
digitalWrite (led2, HIGH);
digitalWrite (led1, HIGH);
delay (1000);
digitalWrite (led2, LOW);
digitalWrite (led1, LOW);
delay (500);
break; // break the loop
}
}
}
digitalWrite (led2, HIGH);
// check the pixel
// if the binary is 0, dot
if(!pen.checkPixel(pixelNum)){
pen.dot ();
delay(500);
}
pen.movePixel (1, dir); // move a pixel
pixelNum++;
}
pen.accelerate (imageX, 1, (dir+1)%2); // move the X axis back
delay(500);
}
// finished
// move the cnc back to orgin spot
delay (1000);
pen.accelerate (imageX, 1, (dir+1)%2); // the X axis in opposite direction
delay (1000);
pen.accelerate (imageY, 0, (dir+1)%2); // the Y axis in opposite direction
Finished:
delay (100);
}
// pen.h
#include <Servo.h>
#include "image.h"
Servo penMotor;
// define a pen Libary for stepper motor and Servos
class Pen{
private:
//array of pins
const int motorStep[2] = {12, 10};
const int motorDir[2] = {11, 9};
//Servo penMotor;
const int ServoPin = 8;
//distance, high, low
const int high = 10; // default height
const int drop = 45; // default depth
//original: 10, 20
const int steps [2] = {10, 10};
//original 5000, 5000
const int delaytime [2] = {5000, 5000};
const int minDelay [2] = {5000, 5000};
public:
void movePixel (int, int); // moving in pixels
void accelerate (int, int, int); // moving backwards faster
void dot (); // dot functopn
int checkPixel (long); // checking if it's 0 or 1 (print or not)
// for testing
void down ();
void up ();
// initialize constructor
Pen (){
for (int i=0; i<2; i++)
{
pinMode (motorStep [i], OUTPUT);
pinMode (motorDir [i], OUTPUT);
}
penMotor.attach (ServoPin);
penMotor.write (high);
}
};
void Pen::movePixel (int motor, int dir){
for (int i=0; i<steps[motor]; i++)
{
digitalWrite (motorDir[motor], dir);
digitalWrite (motorStep[motor], LOW);
digitalWrite (motorStep[motor], HIGH);
delayMicroseconds (delaytime[motor]);
digitalWrite (motorStep[motor], LOW);
}
}
void Pen::accelerate (int Step, int motor, int dir) {
for (int i=0; i<Step*steps[motor]; i++)
{
digitalWrite (motorDir[motor], dir);
digitalWrite (motorStep[motor], LOW);
digitalWrite (motorStep[motor], HIGH);
delayMicroseconds (minDelay[motor]);
digitalWrite (motorStep[motor], LOW);
}
}
void Pen::up () {
penMotor.write (high);
}
void Pen::down () {
penMotor.write (drop);
}
void Pen::dot (){
penMotor.write (drop);
delay (100);
penMotor.write (high);
delay (100);
}
int Pen::checkPixel (long pixel) {
unsigned long byteNum = pixel / 8; //Take the actual pixel number and divide it by 8 to get the Byte location in the Char array
int remainder = pixel % 8; //Take the modulus of the pixel number by 8 to get the bit inside of the byte location
unsigned char bufferByte = pgm_read_byte(&bmp[byteNum]); //Use the byte location to save the byte into a Buffer to read the bits
int activePixel = bitRead(bufferByte, 7 - remainder); //We need the bits from left to right so the remained is subtracted by 7 to get the proper bit
return activePixel; //Return the 1 or 0 in the bit location
}
const unsigned char bmp [] PROGMEM = {elements in hex}
the error message
"redefinition of 'const unsigned char bmp []'"