# Coding problem using Processing with Arduino

**URL:** https://discourse.processing.org/t/coding-problem-using-processing-with-arduino/36354
**Category:** Electronics (Arduino, etc.)
**Created:** [April 15, 2022, 2:59pm UTC](https://discourse.processing.org/t/coding-problem-using-processing-with-arduino/36354 "2022-04-15T14:59:53Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![remi\_chen](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@remi\_chen](https://discourse.processing.org/u/remi_chen)
#### Post date: [April 15, 2022, 2:59pm UTC](https://discourse.processing.org/t/coding-problem-using-processing-with-arduino/36354/1 "2022-04-15T14:59:53Z")

</div>

Hi guys! Recently, I am working on an art project that needs Processing and Arduino. Now that I have some questions, I want to turn the following flowchart into automatic code behavior. I already have some basic code and it works. But I don’t know how to automatically get the updated QR code data and update the header file through the code in Processing. Can anybody help me?  
Here’s the code in Processing :

```auto
// THIS IS NOT ARDUINO CODE! Runs in Processing IDE (www.processing.org).
// Convert image to C header file suitable for the Adafruit_Thermal library.

void setup() {
  // Select and load image
  selectInput("Select image file to convert:", "processImage");
}

void processImage(File image) {
  String filename, basename;
  PImage img;
  PrintWriter output;
  int pixelNum, byteNum, bytesOnLine = 99,
              x, y, b, rowBytes, totalBytes, lastBit, sum;
  println("Loading image...");
  filename = image.getPath();
  img = loadImage(image.getPath());

  // Morph filename into output filename and base name for data
  x = filename.lastIndexOf('.');
  if(x > 0) filename = filename.substring(0, x); // Strip current extension
  x = filename.lastIndexOf('/');
  if(x > 0) basename = filename.substring(x + 1); // Strip path
  else basename = filename;
  filename += ".h"; // Append '.h' to output filename
  println("Writing output to " + filename);

  // Calculate output size
  rowBytes = (img.width + 7) / 8;
  totalBytes = rowBytes * img.height;
  // Convert image to B&W, make pixels readable
  img.filter(THRESHOLD);
  img.loadPixels();

  // Open header file for output (NOTE: WILL CLOBBER EXISTING .H FILE, if any)
  output = createWriter(filename); 

  // Write image dimensions and beginning of array
  output.println("#ifndef _" + basename + "_h_");
  output.println("#define _" + basename + "_h_");
  output.println();
  output.println("#define " + basename + "_width " + img.width);
  output.println("#define " + basename + "_height " + img.height);
  output.println();
  output.print("static const uint8_t PROGMEM " + basename + "_data[] = {");

  // Generate body of array
  for(pixelNum=byteNum=y=0; y<img.height; y++) { // Each row...
    for(x=0; x<rowBytes; x++) { // Each 8-pixel block within row...
      lastBit = (x < rowBytes - 1) ? 1 : (1 << (rowBytes * 8 - img.width));
      sum = 0; // Clear accumulated 8 bits
      for(b=128; b>=lastBit; b >>= 1) { // Each pixel within block...
        if((img.pixels[pixelNum++] & 1) == 0) sum |= b; // If black pixel, set bit
      }
      if(++bytesOnLine >= 10) { // Wrap nicely
          output.print("\n ");
          bytesOnLine = 0;
      }
      output.format(" 0x%02X", sum); // Write accumulated bits
      if(++byteNum < totalBytes) output.print(',');
    }
  }

  // End array, close file, exit program
  output.println();
  output.println("};");
  output.println();
  output.println("#endif // _" + basename + "_h_");
  output.flush();
  output.close();
  println("Done!");
  exit();
}

```

Here’s the code in Arduino:

```auto
#include "Adafruit_Thermal.h"
#include "QRCODE.h"
#include "SoftwareSerial.h"
#define TX_PIN 6 // Arduino transmit YELLOW WIRE labeled RX on printer
#define RX_PIN 5 // Arduino receive GREEN WIRE labeled TX on printer

SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
Adafruit_Thermal printer(&mySerial); // Pass addr to printer constructor
const int Pushbutton = 3;
int value = 0;
int debounce_delay = 300; //Debounce delay

void setup() {
 pinMode (Pushbutton, INPUT);
 pinMode (7, OUTPUT); 
 mySerial.begin(19200);
 }

void loop() {
  
value = digitalRead(Pushbutton);
if (value == HIGH){
 delay(debounce_delay); 
 digitalWrite(7, HIGH);
 printer.begin();      
 printer.setSize('M');
 printer.println(F("Royal College of Art"));
 printer.setSize('S');
 printer.println(F("Information Experience Design"));
 printer.setLineHeight(50);
 printer.printBitmap(QRCODE_width, QRCODE_height, QRCODE_data);
 printer.setLineHeight(50);
 printer.println(F("Enjoy your memory ghost!"));
 printer.setLineHeight(150);
 printer.println(F(" "));

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

```

 ![coding](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/4/7/471e2dd8e09c60812d9bdf579cf6697d56adc55c.jpeg)

---

<div class="post-metadata">

### Author: ![ChengkaiXu](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chengkaixu/32/22081_2.png) [@ChengkaiXu](https://discourse.processing.org/u/ChengkaiXu)
#### Post date: [April 16, 2022, 6:01am UTC](https://discourse.processing.org/t/coding-problem-using-processing-with-arduino/36354/2 "2022-04-16T06:01:13Z")

</div>

Even though I am also studying Arduino with Processing right now, I think I could offer a possible solution based on my experience.

**Thoery Part:**  
“.h” file is a library file for the coding language C. These files you’ve included in the beginning of your Arduino codes are complied into a “machine language”, and then send to the arduino board. This process means that when you click the run button in the Arduino IDE, the program is fixed, which means it will no longer read these “.h” again. To achieve the dynamic read&print process, “.json” file may be a relatively good choice, which have already been used for data storage for years.

**Pratical Suggestion**  
If I understand your program workflow correctly, QRcode here is an input which will be read by the Processing program. Then the Processing program deliver that information to the Arduino for printing.  
You may need to change the “.h” file part. Instead, all the QRcode data can be stored in a “.json” file as an array. Then, in the Arduino program, you can parse those data from the “.json” file dynamically.

**Library resources you may need:**  
Processing Side: [https://processing.org/reference/JSONObject.html](https://processing.org/reference/JSONObject.html)  
Arduino Side: [https://arduinojson.org/](https://arduinojson.org/)

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [April 18, 2022, 9:04pm UTC](https://discourse.processing.org/t/coding-problem-using-processing-with-arduino/36354/3 "2022-04-18T21:04:24Z")

</div>

Hello @remi_chen ,

Which Arduino are you using?

`:)`

---

<div class="post-metadata">

### Author: ![remi\_chen](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@remi\_chen](https://discourse.processing.org/u/remi_chen)
#### Post date: [April 19, 2022, 1:01pm UTC](https://discourse.processing.org/t/coding-problem-using-processing-with-arduino/36354/4 "2022-04-19T13:01:54Z")

</div>

Hi Chengkai

Thank you for your reply! I still have some questions ,do you mind if I ask for your email? I would like to have more communication with u!

---

<div class="post-metadata">

### Author: ![remi\_chen](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@remi\_chen](https://discourse.processing.org/u/remi_chen)
#### Post date: [April 19, 2022, 1:35pm UTC](https://discourse.processing.org/t/coding-problem-using-processing-with-arduino/36354/5 "2022-04-19T13:35:47Z")

</div>

Arduino IDE with mega 2560

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [April 20, 2022, 2:32pm UTC](https://discourse.processing.org/t/coding-problem-using-processing-with-arduino/36354/7 "2022-04-20T14:32:10Z")

</div>

The Mega has 3 extra real serial ports, you don’t need to use the Software Serial. See pin 14…
