// Pozrite sa na to množstvo premenných!!!
byte naTlac[1000]; // alokovaných 1000 bajtov, kde sa budú písať hodnoty
long step = 0; // kroky motora tlačovej hlavy
long tarstep = 0; // cieľová pozícia motora tlačovej hlavy
long printpix = 0; // pixel na tlač
long pixel = 0; // horizontálna poloha v pixeloch
byte pxlnfeed = 0; // má sa posunúť riadok v úrovni pixelov?
long stplnfdamnt = 0; // relatívne množstvo krokov na posuv papiera o riadok
byte val = 0; // hodnota čítaná z array naTlac
byte toprint = 0; // má sa tlačiť? Platí len pre jeden riadok pixelov
long retstep = 0; // kroky návratu tlačovej hlavy
byte printdir = 0; // smer tlače
unsigned int clearing = 0;
void setup() {
pinMode(2, OUTPUT); // stepper 1 dir
pinMode(3, OUTPUT); // stepper 1 step
pinMode(4, OUTPUT); // stepper 2 step
pinMode(5, OUTPUT); // tlačiaca ihlička
pinMode(6, INPUT_PULLUP); // koncový kalibračný spínač
Serial.begin(115200);
Serial.println(); // tento riadok môžete vynechať
//shlulls();
}
void loop() {
if (Serial.available()) {
if (Serial.read() == '1') {
naTlac[printpix] = 1;
}
if (Serial.read() == '0') {
naTlac[printpix] = 0;
}
printpix = printpix + 1;
}
if (printpix > 999) {
Serial.write(0x13);
printpix = 0;
toprint = 1;
}
if (toprint == 1 && printdir == 0) {
stepMovePrint();
}
if (toprint == 1 && printdir == 1) {
stepMovePrintReverse();
}
while (clearing <= 1000) {
naTlac[clearing] = 0;
clearing = clearing + 1;
}
}
void stepMovePrint() { // tlačenie jedného riadku
tarstep = pixel * 16;
digitalWrite(2, LOW);
if (toprint == 1) {
while (pixel <= 999) {
tarstep = pixel * 16;
val = naTlac[pixel];
while (tarstep > step) {
tarstep = pixel * 16;
digitalWrite(3, HIGH);
delayMicroseconds(40);
digitalWrite(3, LOW);
delayMicroseconds(40);
if (tarstep - step <= 9) {
digitalWrite(5, LOW);
}
else {
digitalWrite(5, val);
}
step = step + 1;
}
if (tarstep <= step) {
pixel = pixel + 1;
}
}
}
toprint = 0;
digitalWrite(5, LOW);
printdir = 1;
stepLinefeed();
}
void stepLinefeed() {
stplnfdamnt = 27;
while (stplnfdamnt >= 1) {
digitalWrite(4, HIGH);
delayMicroseconds(100);
digitalWrite(4, LOW);
delayMicroseconds(100);
stplnfdamnt = stplnfdamnt - 1;
}
printpix = 0;
clearing = 0;
for (int i = 0; i <= 1000; i = i + 1) {
naTlac[i] = 0;
}
Serial.write(0x11);
}
void stepMovePrintReverse() { // tlačenie jedného riadku
tarstep = pixel * 16;
digitalWrite(2, HIGH);
if (toprint == 1) {
while (pixel >= 0) {
tarstep = pixel * 16;
if (pixel > -1) {
val = naTlac[pixel];
}
if (pixel == -1) {
val = naTlac[0];
}
while (tarstep < step) {
tarstep = pixel * 16;
digitalWrite(3, HIGH);
delayMicroseconds(40);
digitalWrite(3, LOW);
delayMicroseconds(40);
if(step - tarstep <= 9) {
digitalWrite(5, LOW);
}
else {
digitalWrite(5, val);
}
step = step - 1;
}
if (tarstep >= step) {
pixel = pixel - 1;
}
}
}
digitalWrite(5, LOW);
toprint = 0;
printdir = 0;
stepLinefeed();
}
void shlulls() {
digitalWrite(2, HIGH);
while (digitalRead(6) == HIGH) {
digitalWrite(3, HIGH);
delayMicroseconds(2500);
digitalWrite(3, LOW);
delayMicroseconds(2500);
}
tarstep = 0;
step = 0;
pixel = 0;
printpix = 0;
while (step <= 400) {
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
delayMicroseconds(250);
digitalWrite(3, LOW);
delayMicroseconds(250);
step = step + 1;
}
}