In need of help with fixing code for 8 pin dot matrix printer

I am currently in need of advice on how could I fix the code for my 8 pin dot matrix printer (it does not accept Epson language, it only accepts raw bytes (only send ascii characters, never ever under any circumstance send the written out forms of bytes), where the top pin is the LSB of a byte, and the bottom pin is the MSB of the byte). There is some weird behavior. I will provide the characters I use as input in a text file, including the link to the Wokwi project page. And no, I will never ever make it accept Epson commands and whatnot.

Here is the text file:
collection of symbols.txt (2,0 KB)

Here is the link to the Wokwi page:

What are the fixes that I could put into it other than abstraction, abstraction, code simplification, and any other form of code shortening or untangling?

Totally void of any information required for help! What is Epson language? Is it more than 8 bits wide? What is "written out forms of bytes" . Give examples if nothing else! Do you have an available link to documentation for what you are trying to do?

What needs to be fixed? Lots of acronyms but little info to go on. I took a quick look at the code, and my hunch is it would benefit greatly by applying some of those acronyms. That begs the question, Why haven't you?

By Epson language I mean the stuff that gets sent to Epson printers.

By that I mean the representation of bytes (when you use Serial.print(), it writes the value as multiple letters (example: 0x30) instead of sending an ASCII letter that matches the value of the byte (example: 0x30 is ASCII for 0)).

Go to the Wokwi project page through the link I provided, and send it 1000 letters through the serial monitor. So the answer is no, I do not have any link to any documentation because I should not have to bother with that, so please converse like a normal person, and please no rudeness.

What needs to be fixed is the behavior of the pins (pins that should not strike are seen firing, as if they received an alternating pattern of turn on and turn off). Run the thing, feed it 50 groups of 20 letters, and use Pulseview to look at the .vcd file the logic analyzer generates

Use Serial.write()

1 Like

Thing is, I will not be using another Arduino to send stuff to my printer.

I don't understand what it is that you want to change. When you posted about Serial.Print() not sending a direct copy of the value its given, I assumed you wanted a cure for that. So I posted how to fix that. Your answer to my post has confused me.

It is formally called ESC/P , there are several versions of it and supported set per printer differs IIRC. ESC/P looks a lot like ANSI escape codes.

1 Like

@ardurider50hz

Long ago I wrote a library for parallel printers. It is based upon a basic parallel printer interface that has STROBE line to clock the bits and further only a BUSY and OOP (out of paper) line.

The class implements the (Arduino) Print interface which makes it pretty easy to use.
Might work for your printer too.

  for (int i = 0; i <= maxpix; i = i + 1) {
    naTlac[i] = 0;
  }

Writing outside the bounds of an array.

/home/xxx/Arduino/dotMatrixPrinter/dotMatrixPrinter.ino: In function 'stepLinefeed':
/home/xxx/Arduino/dotMatrixPrinter/dotMatrixPrinter.ino:148:15: warning: iteration 1000 invokes undefined behavior [-Waggressive-loop-optimizations]
     naTlac[i] = 0;
               ^
/home/xxx/Arduino/dotMatrixPrinter/dotMatrixPrinter.ino:147:21: note: within this loop
   for (int i = 0; i <= maxpix; i = i + 1) {
                     ^

I will have to look into this, but I do not think that this is the reason for any problems

If anything, this should acutally clear a value instead of causing weird behavior of the pins

No. Writing beyond the boundaries of an array results in undefined behavior. When you do that, the overwritten location(s) may be a part of any other data object, or, if the array was created on the stack, it could be a return address, a return value, a temporarily allocated stored value for a register, or ???. You. Do. Not. Know.

Fixing it so it does not write outside the boundaries did not fix anything. You can look at it if you want

Not unexpected; fixing the problem may require more than one code fix.

Please post a complete code that we may then transport to the IDE for testing.

Can you see anything I did wrong (except the comments) in the code?

Here it is

// Note: You can use a translator on the comments

int maxpix = 1000;  // maximálna poloha v pixeloch
int stpx = 16;  // lineárna závislosť medzi krokmi a bodmi
int stlf = 205;  // množstvo krokov na posunutie o jeden pixelový riadok

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
byte senseb1 = 0; // detekcia bitu 1
byte senseb2 = 0; // detekcia bitu 2
byte senseb3 = 0; // detekcia bitu 3
byte senseb4 = 0; // detekcia bitu 4
byte senseb5 = 0; // detekcia bitu 5
byte senseb6 = 0; // detekcia bitu 6
byte senseb7 = 0; // detekcia bitu 7
byte senseb8 = 0; // detekcia bitu 8
byte num1 = 0;
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, INPUT_PULLUP);  // koncový kalibračný spínač
  pinMode(6, OUTPUT);  // tlačiaca ihlička 1
  pinMode(7, OUTPUT);  // tlačiaca ihlička 2
  pinMode(8, OUTPUT);  // tlačiaca ihlička 3
  pinMode(9, OUTPUT);  // tlačiaca ihlička 4
  pinMode(10, OUTPUT);  // tlačiaca ihlička 5
  pinMode(11, OUTPUT);  // tlačiaca ihlička 6
  pinMode(12, OUTPUT);  // tlačiaca ihlička 7
  pinMode(13, OUTPUT);  // tlačiaca ihlička 8

  Serial.begin(9600);

  Serial.println();  // tento riadok môžete vynechať

  //shlulls();

  for(int v = 0; v < maxpix - 1; v = v + 1) {
    naTlac[v] = 0;
  }

}

void loop() {

  if (Serial.available()) {

    naTlac[printpix] = Serial.read();

    printpix = printpix + 1;
  }

  if (printpix > maxpix - 1) {
    Serial.write(0x13);
    printpix = 0;
    toprint = 1;
  }

  if (toprint == 1 && printdir == 0) {
    stepMovePrint();
  }

  if (toprint == 1 && printdir == 1) {
    stepMovePrintReverse();
  }

  while (clearing <= maxpix - 1) {
    naTlac[clearing] = 0;
    clearing = clearing + 1;
  }

}

void stepMovePrint() {  // tlačenie jedného riadku
  tarstep = pixel * stpx;
  digitalWrite(2, LOW);

  if (toprint == 1) {
    while (pixel <= maxpix - 1) {
      tarstep = pixel * stpx;
      val = naTlac[pixel];
      while (tarstep > step) {
        tarstep = pixel * stpx;
        digitalWrite(3, HIGH);
        delayMicroseconds(40);
        digitalWrite(3, LOW);
        delayMicroseconds(40);

        if (tarstep - step <= stpx - 10) {
          digitalWrite(6, LOW);
          digitalWrite(7, LOW);
          digitalWrite(8, LOW);
          digitalWrite(9, LOW);
          digitalWrite(10, LOW);
          digitalWrite(11, LOW);
          digitalWrite(12, LOW);
          digitalWrite(13, LOW);
        }

        else {
          prhdwt();
        }

        step = step + 1;
      }

      if (tarstep <= step) {
        pixel = pixel + 1;
      }

    }

  }
  toprint = 0;
  digitalWrite(5, LOW);
  printdir = 1;
  stepLinefeed();
}

void stepLinefeed() {
  stplnfdamnt = stlf;

  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 <= maxpix - 1; i = i + 1) {
    naTlac[i] = 0;
  }

  Serial.write(0x11);
}

void stepMovePrintReverse() {  // tlačenie jedného riadku
  tarstep = pixel * stpx;
  digitalWrite(2, HIGH);

  if (toprint == 1) {
    while (pixel >= 0) {
      tarstep = pixel * stpx;
      if (pixel > -1) {
        val = naTlac[pixel];
      }
      if (pixel == -1) {
        val = naTlac[0];
      }
      while (tarstep < step) {
        tarstep = pixel * stpx;
        digitalWrite(3, HIGH);
        delayMicroseconds(40);
        digitalWrite(3, LOW);
        delayMicroseconds(40);

        if (step - tarstep <= stpx - 10) {
          digitalWrite(6, LOW);
          digitalWrite(7, LOW);
          digitalWrite(8, LOW);
          digitalWrite(9, LOW);
          digitalWrite(10, LOW);
          digitalWrite(11, LOW);
          digitalWrite(12, LOW);
          digitalWrite(13, LOW);
        }

        else {
          prhdwt();
        }

        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;
  }

}

void prhdwt() { /* Print head write (writes a byte to the whole printhead) */

  num1 = naTlac[pixel];

  senseb1 = ~num1 & 0b00000001;
  senseb2 = ~num1 & 0b00000010;
  senseb3 = ~num1 & 0b00000100;
  senseb4 = ~num1 & 0b00001000;
  senseb5 = ~num1 & 0b00010000;
  senseb6 = ~num1 & 0b00100000;
  senseb7 = ~num1 & 0b01000000;
  senseb8 = ~num1 & 0b10000000;

  if(senseb1 > 1) {
    senseb1 = 1;
  }

  if(senseb2 > 1) {
    senseb2 = 1;
  }

  if(senseb3 > 1) {
    senseb3 = 1;
  }

  if(senseb4 > 1) {
    senseb4 = 1;
  }

  if(senseb5 > 1) {
    senseb5 = 1;
  }

  if(senseb6 > 1) {
    senseb6 = 1;
  }

  if(senseb7 > 1) {
    senseb7 = 1;
  }

  if(senseb8 > 1) {
    senseb8 = 1;
  }

  digitalWrite(6, senseb1);
  digitalWrite(7, senseb2);
  digitalWrite(8, senseb3);
  digitalWrite(9, senseb4);
  digitalWrite(10, senseb5);
  digitalWrite(11, senseb6);
  digitalWrite(12, senseb7);
  digitalWrite(13, senseb8);

  senseb1 = 0;
  senseb2 = 0;
  senseb3 = 0;
  senseb4 = 0;
  senseb5 = 0;
  senseb6 = 0;
  senseb7 = 0;
  senseb8 = 0;

  num1 = 0;

}

Thanks, heading for the lab. I hate phones for this.