Adafruit thermal printer with p5.js not printing

i have this code on arduino

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <NewPing.h>

#include "Pos_Printer.h"
#include "SoftwareSerial.h"

LiquidCrystal_I2C lcd1(0x25, 16, 2);
LiquidCrystal_I2C lcd2(0x26, 16, 2);

#define TRIGGER_PIN 4
#define ECHO_PIN 2
#define MAX_DISTANCE 200

#define TX_PIN 7 // Arduino transmit  YELLOW WIRE  labeled RX on printer
#define RX_PIN 8 // Arduino receive   GREEN WIRE   labeled TX on printer

SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
Pos_Printer printer(&mySerial);          // Pass addr to printer constructor

const int buttonPin1 = 3;
const int buttonPin2 = 5;
const int buttonPin3 = 6;

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;

int incomingByte;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  pinMode(9, OUTPUT);
  digitalWrite(9, LOW);
  Serial.begin(9600);
  lcd1.init();
  lcd1.backlight();
  lcd2.init();
  lcd2.backlight();
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
 
  printer.begin(); 
  printer.println(F("STO STAMPANDOOOO"));
  printer.feed(8);
}

void loop() {
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

  if (buttonState1 == HIGH) {
    Serial.println("1");
  }
  if (buttonState2 == HIGH) {
    Serial.println("2");
  }
  if (buttonState3 == HIGH) {
    Serial.println("3");
  }

  int distance = sonar.ping_cm();

  if (distance > 0) {
    Serial.println("YES");
    lcd1.setCursor(0, 0);
    lcd1.print("Seleziona");
    lcd1.setCursor(0, 1);
    lcd1.print("prodotto");
    lcd2.setCursor(0, 0);
    lcd2.print("Seleziona");
    lcd2.setCursor(0, 1);
    lcd2.print("prodotto");
  } else {
    Serial.println("NO");
    lcd1.clear();
    lcd2.clear();
  }

  if (Serial.available() > 0) {
    incomingByte = Serial.read();

    Serial.print("Received: ");
    Serial.println(incomingByte);

    if (incomingByte == 7) {
      Serial.println("Received 7; Printing...");
      printer.println(F("HO STAMPATO DOPO SCENA 7"));
      printer.feed();
      

      printer.sleep();      // Tell printer to sleep
      delay(3000L);         // Sleep for 3 seconds
      printer.wake(); 
      printer.setDefault(); // Restore printer to defaults
    }
  }
}

the serial communication is working fine but the thermal printer is not printing not even in the setup. The printer is working fine when loading the example code from the adafruit library. We tried also to use 2 different type of library: adafruit_thermal and pos_printer. Both arent working.

Why don't You use the body of the code that's printing and incorporate that into Your code?

Thank you we solved the problem.
But we have another one. With whatever code we are using to print, there is always an "8" before each printed line. ( and sometimes "88")

Please post that code! It might be a Rubout, Ctrl H, and it has the binary representation of 8. Write/Print mistake.

this is the code

#include "Pos_Printer.h"
#include "hal9klogo.h"


#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <NewPing.h>

LiquidCrystal_I2C lcd1(0x25, 16, 2);
LiquidCrystal_I2C lcd2(0x26, 16, 2);

#define TRIGGER_PIN 4
#define ECHO_PIN 2
#define MAX_DISTANCE 200


#include "SoftwareSerial.h"
#define TX_PIN 8 
#define RX_PIN 7 

SoftwareSerial mySerial(RX_PIN, TX_PIN); 
Pos_Printer printer(&mySerial);    



const int buttonPin1 = 3;
const int buttonPin2 = 5;
const int buttonPin3 = 6;

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;

int incomingByte;
bool printFlag1 = false;
bool printFlag2 = false;
bool printFlag3 = false;

String minutes = "";
String seconds = "";

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);


void setup() {


  pinMode(7, OUTPUT); digitalWrite(7, LOW);

  mySerial.begin(9600); 
  //Serial1.begin(9600);
  printer.begin();        

  printer.printBitmap(Hal9klogo_width, Hal9klogo_height, Hal9klogo_data);
  printer.feed(8);

  delay(1000);

  pinMode(9, OUTPUT);
  digitalWrite(9, LOW);
  Serial.begin(9600);
  lcd1.init();
  lcd1.backlight();
  lcd2.init();
  lcd2.backlight();
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);

  

  


  printer.sleep();     
  delay(3000L);       
  printer.wake();       // MUST wake() before printing again, even if reset
  printer.setDefault(); // Restore printer to defaults
}

void loop() {
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

  if (buttonState1 == HIGH) {
    Serial.println("1");
  }
  if (buttonState2 == HIGH) {
    Serial.println("2");
  }
  if (buttonState3 == HIGH) {
    Serial.println("3");
  }

  int distance = sonar.ping_cm();

  if (distance > 0) {
    Serial.println("YES");
    lcd1.setCursor(0, 0);
    lcd1.print("Seleziona");
    lcd1.setCursor(0, 1);
    lcd1.print("prodotto");
    lcd2.setCursor(0, 0);
    lcd2.print("Seleziona");
    lcd2.setCursor(0, 1);
    lcd2.print("prodotto");
  } else {
    Serial.println("NO");
    lcd1.clear();
    lcd2.clear();
  }

  if (Serial.available() > 0) {
      incomingByte = Serial.read();
  
      Serial.print("Received: ");
      Serial.println(incomingByte);

  
      if (incomingByte == 7 && !printFlag1) {
        Serial.println("Received 7; Printing...");
        printFlag1 = true; // Set the flag to true to prevent repeated printing
  
        // Add delay after printing
        printer.println(F("HO STAMPATO DOPO SCENA 7"));
        printer.print(minutes);
        printer.print(seconds);
        printer.feed(9);
        delay(500); // Adjust delay time
  
        printer.sleep();
        delay(3000L);
        printer.wake();
        printer.setDefault();
      }
    

     if (incomingByte == 8 && !printFlag2) {
        Serial.println("Received 8; Printing...");
        printFlag2 = true; // Set the flag to true to prevent repeated printing
  
        // Add delay after printing
        printer.println(F("HO STAMPATO DOPO SCENA 8"));
        printer.print(minutes);
        printer.print(seconds);
        printer.feed(9);
        delay(500); // Adjust delay time
  
        printer.sleep();
        delay(3000L);
        printer.wake();
        printer.setDefault();
      }
    

     if (incomingByte == 9 && !printFlag3) {
        Serial.println("Received 9; Printing...");
        printFlag3 = true; // Set the flag to true to prevent repeated printing
  
        // Add delay after printing
        printer.println(F("HO STAMPATO DOPO SCENA 9"));
        printer.print(minutes);
        printer.print(seconds);
        printer.feed(9);
        delay(500); // Adjust delay time
  
        printer.sleep();
        delay(3000L);
        printer.wake();
        printer.setDefault();
      }
    }
}

if you could resole the problem of the "8" it would be fantastic.
An additional problem is the printing of a timer. On the p5.js file we wrote the serial.write of the minutes and seconds but arduino cant read and print it.
Could you help us with both problems.
Thank you very much!

The "8", or "88" appear on the printout?
What does the Serial.print debug tell?
I suggest You use test-Serial.print to catch it. Know the difference between ASCII character and the binary reperesentation of the character.


this is what the printer prints
i'm not sure i understand what you mean with Serial.print debug. The problem is that this happens with every code i tried using

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.