Thermal Printer controlled by a push button

Hello, everyone!
I've recently bought an Arduino Kit and developed a project trying to print some phrase controlled by a push button. With all you guys' helps, following former Q&As , I managed to connect and initiate QR204 Mini Thermal Printer from AliExpress (since ones from Adafruit are no more possible to get), and it works on 1 time without the button.

This is the code:

#include "Adafruit_Thermal.h"
#include "SoftwareSerial.h"
#define TX_PIN 3
#define RX_PIN 2

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

void setup() {

  mySerial.begin(9600);
  printer.begin(9600); 
  printer.wake();       
  printer.setDefault();
    
    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    delay(4000);
    printer.println(F("Text1"));
    delay(3000);
    printer.setLineHeight(50);
    printer.justify('C');
    printer.inverseOn();
    printer.println(F("Text2"));
    printer.inverseOff();

    printer.justify('L');
    printer.setLineHeight(150);
    printer.println(F("Text3"));
    printer.setLineHeight();
    printer.println(F("Text4, "));
    delay (2000);

    printer.setLineHeight(100);
    printer.println(F("Text5")); 
    printer.justify('C');
    printer.setLineHeight(150);
    delay(3000);  


    printer.println(F("Text6"));
    printer.justify('L');
    printer.setLineHeight();
    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    delay(3000);

    printer.setDefault();
    printer.sleep();
}

void loop() {
}

And now I'm trying to make it work when the button is pushed, but it doesn't work.
I cannot find the reason so writing this for help.
Below is the code I failed.

#include "Adafruit_Thermal.h"
#include "SoftwareSerial.h"
#define TX_PIN 3
#define RX_PIN 2 
#define button1 7

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

void setup() {
  pinMode(button1, INPUT_PULLUP);
  Serial.begin(9600); //PC-Arduino Communication
  mySerial.begin(9600); //Arduino-Printer Communication
  printer.begin();
}

void loop() {
  //Pushing button
  if(digitalRead(button1) == LOW){
    //button is pressed
    printer.wake();     

    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    delay(4000);
    printer.println(F("Text1"));
    delay(3000);
    printer.setLineHeight(50);
    printer.justify('C');
    printer.inverseOn();
    printer.println(F("Text2"));
    printer.inverseOff();

    printer.justify('L');
    printer.setLineHeight(150);
    printer.println(F("Text3"));
    printer.setLineHeight();
    printer.println(F("Text4"));
    delay (2000);

    printer.setLineHeight(100);
    printer.println(F("Text5")); 
    printer.justify('C');
    printer.setLineHeight(150);
    delay(3000);  

    printer.println(F("Text6"));
    printer.justify('L');
    printer.setLineHeight();
    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    printer.println(F("  "));
    delay(3000);

    printer.setDefault();
    printer.sleep();

  }
}

Plus, adding next code for the end part also doesn't work.

else { 
printer.setDefault();
    printer.sleep(); }

And this is the circuit.

I'm so novice to Arduino and coding, and also English is not my first language so sorry for all clumsinesses.

Thank you!

There should be 2 wires coming from your "mini breadboard" - one to pin 7 and the other the the UNO GND.

What is the purpose of the 9V battery attached to the mini breadboard? Is it an illuminated pushbutton?

You should be able to connect the pushbutton between UNO Pin 7 and UNO GND.

2 Likes

Look at the Arduino File/Example/02.Digital/Button example, and consider putting much of the stuff in your setup() function into a printLineOnTheThermalPrinter() function per

You might modify the Button example to also call a printLineOnTheThermalPrinter() function ehrn the button is pressed.

1 Like

OMG it works!!!!! I still can't believe this goes well... :face_with_open_eyes_and_hand_over_mouth:
You saved my time, energy, .. and other things!!! Thanks a lot!!!
I'Il do my best to give a lot to beginners like me in the future. Super thanks!

1 Like

Since it works, now I'm going to make it sophisticated by your suggestion and the link. Thank you so much!

Thanks to you and the kind instruction attached, I revised my code and it is now much easier to read and apply. Huge thanks from the bottom of my heart! Have a nice day :grin:

1 Like

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