Is my code good to go?

I wanted to know if my code chord to generate random chord progressions (and print it on a thermal printer) is good to go.

#include "Adafruit_Thermal.h"
#include <Wire.h>
#include "SoftwareSerial.h"



int randomNum1 = random(0, 4);
int randomNum2 = random(0, 82);
#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

byte customChar[8] = {
  0b01110,
  0b10001,
  0b11111,
  0b11001,
  0b11001,
  0b11111,
  0b10001,
  0b01110
};





String msgs1[]= {"1 beat of","2 beats of","3 beats of","1 measure of","2 measures of"};
String msgs2[]= {"Cmaj7","C#maj7","Dmaj7","D#maj7","Emaj7","Fmaj7","F#maj7","Gmaj7","G#maj7","Amaj7","A#maj7","Bmaj7","C-7","C#-7","D-7","D#-7","E-7","F-7","F#-7","G-7","G#-7","A-7","A#-7","B-7","C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B", "Cmaj7/9", "C#maj7/9","Dmaj7/9","D#maj7/9","Emaj7/9" "Fmaj7/9" "F#maj7/9", "Gmaj7/9", "G#maj7/9", "Amaj7/9","A#maj7/9","Bmaj7/9","C/D" ,"C#/D#","D/E","Eb/F","E/F#","F/G","F#/G#","G/A","G#/A#","A/B","A#/C","B/C#", "C-7/9", "C#-7/9", "D-7/9", "D#-7/9", "E-7/9", "F-7/9", "F#-7/9", "G-7/9", "G#-7/9", "A-7/9", "A#-7/9", "B-7/9", "C7", "C#7", "D7", "D#7", "E7", "F7", "F#7", "G7", "G#7", "A7", "A#7", "B7"};



//I need to replace these "msg1" and msgs 2 with more logical (and more through) matches! 
// as of oct 17 this has been (i think) completed


void setup()
{
  randomSeed(analogRead(A0));
  Serial.begin(19200);  
  pinMode(7, OUTPUT); digitalWrite(7, LOW); 
  pinMode(4, INPUT);           // set pin to input
  digitalWrite(4, HIGH);       // turn on pullup resistors

}

void loop()
{
  if(digitalRead(4) == LOW)
  {
    randomNum1 = random(0, 4);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs1[randomNum1]);
    printer.feed(1);
    
    randomNum2 = random(0, 35);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs2[randomNum2]);
    printer.feed(1);

    printer.justify('C');
    printer.println("------------------------------");
    printer.feed(2);
    delay(2000);

    //this is the first chord and beat combonation

    randomNum1 = random(0, 4);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs1[randomNum1]);
    printer.feed(1);
    
    randomNum2 = random(0, 35);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs2[randomNum2]);
    printer.feed(1);

    printer.justify('C');
    printer.println("------------------------------");
    printer.feed(2);
    delay(2000);

    //this is the second chord and beat combonation
   
    randomNum1 = random(0, 4);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs1[randomNum1]);
    printer.feed(1);
    
    randomNum2 = random(0, 35);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs2[randomNum2]);
    printer.feed(1);

    printer.justify('C');
    printer.println("------------------------------");
    printer.feed(2);
    delay(2000);

    //this is the third chord and beat combonation

    randomNum1 = random(0, 4);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs1[randomNum1]);
    printer.feed(1);
    
    randomNum2 = random(0, 35);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs2[randomNum2]);
    printer.feed(1);

    printer.justify('C');
    printer.println("------------------------------");
    printer.feed(2);
    delay(2000);

    //this is the fourth chord and beat combonation

    randomNum1 = random(0, 4);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs1[randomNum1]);
    printer.feed(1);
    
    randomNum2 = random(0, 35);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs2[randomNum2]);
    printer.feed(1);

    printer.justify('C');
    printer.println("------------------------------");
    printer.feed(2);
    delay(2000);

    //this is the fifth chord and beat combonation

    randomNum1 = random(0, 4);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs1[randomNum1]);
    printer.feed(1);
    
    randomNum2 = random(0, 35);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs2[randomNum2]);
    printer.feed(1);

    printer.justify('C');
    printer.println("------------------------------");
    printer.feed(2);
    delay(2000);

    //this is the sixth chord and beat combonation

    randomNum1 = random(0, 4);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs1[randomNum1]);
    printer.feed(1);
    
    randomNum2 = random(0, 35);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs2[randomNum2]);
    printer.feed(1);

    printer.justify('C');
    printer.println("------------------------------");
    printer.feed(2);
    delay(2000);

    //this is the seventh chord and beat combonation

    randomNum1 = random(0, 4);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs1[randomNum1]);
    printer.feed(1);
    
    randomNum2 = random(0, 35);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs2[randomNum2]);
    printer.feed(1);

    printer.justify('C');
    printer.println("------------------------------");
    printer.feed(2);
    delay(2000);

    //this is the eigth chord and beat combonation

    randomNum1 = random(0, 4);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs1[randomNum1]);
    printer.feed(1);
    
    randomNum2 = random(0, 35);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs2[randomNum2]);
    printer.feed(1);

    printer.justify('C');
    printer.println("------------------------------");
    printer.feed(2);
    delay(2000);

    //this is the ninth chord and beat combonation

    randomNum1 = random(0, 4);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs1[randomNum1]);
    printer.feed(1);
    
    randomNum2 = random(0, 35);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs2[randomNum2]);
    printer.feed(1);

    printer.justify('C');
    printer.println("------------------------------");
    printer.feed(2);
    delay(2000);

    //this is the tenth chord and beat combonation

    randomNum1 = random(0, 4);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs1[randomNum1]);
    printer.feed(1);
    
    randomNum2 = random(0, 35);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs2[randomNum2]);
    printer.feed(1);

    printer.justify('C');
    printer.println("------------------------------");
    printer.feed(2);
    delay(2000);

    //this is the eleventh chord and beat combonation

    randomNum1 = random(0, 4);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs1[randomNum1]);
    printer.feed(1);
    
    randomNum2 = random(0, 35);
    printer.setSize('L');  
    printer.justify('C');
    printer.println(msgs2[randomNum2]);
    printer.feed(1);

    printer.justify('C');
    printer.println("------------------------------");
    printer.feed(2);
    delay(2000);

    //this is the twelth chord and beat combonation
    //beneath it is the copyrights, etc. 
    //I used a custom char (as seen at the top) to make a copyright symbol
    //howevever, I am not too sure if this will print on a thermal printer
    //I will findout sooner or later, though. 
    
    printer.doubleHeightOn();
    printer.setSize('L');  
    printer.justify('C');
    printer.println("That's all!");
    printer.feed(4);
    delay(3000);

    printer.doubleHeightOn();
    printer.setSize('L');  
    printer.justify('C');
    printer.write((uint8_t)0);
    printer.feed(4);
    printer.println("noah hellman");
    delay(800);
     
    }
  else;
  {
    noah();
  }    
}

void noah()
{}

I wanted to know if my code chord to generate random chord progressions (and print it on a thermal printer) is good to go.

Why are you asking us? Does it make the hardware do what you want?

String msgs2[]= {"Cmaj7","C#maj7","Dmaj7","D#maj7","Emaj7","Fmaj7","F#maj7","Gmaj7","G#maj7","Amaj7","A#maj7","Bmaj7","C-7","C#-7","D-7","D#-7","E-7","F-7","F#-7","G-7","G#-7","A-7","A#-7","B-7","C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B", "Cmaj7/9", "C#maj7/9","Dmaj7/9","D#maj7/9","Emaj7/9" "Fmaj7/9" "F#maj7/9", "Gmaj7/9", "G#maj7/9", "Amaj7/9","A#maj7/9","Bmaj7/9","C/D" ,"C#/D#","D/E","Eb/F","E/F#","F/G","F#/G#","G/A","G#/A#","A/B","A#/C","B/C#", "C-7/9", "C#-7/9", "D-7/9", "D#-7/9", "E-7/9", "F-7/9", "F#-7/9", "G-7/9", "G#-7/9", "A-7/9", "A#-7/9", "B-7/9", "C7", "C#7", "D7", "D#7", "E7", "F7", "F#7", "G7", "G#7", "A7", "A#7", "B7"};

There is NO reason to use Strings for constant literals. There is NO reason to not use the damned enter key once in a while.

    randomNum1 = random(0, 4);

The range of values should be based on the number of items in the array, NOT a constant.

It's a pretty stupid printer that needs to keep being told to center justify the text. Does it really?

  else;
  {
    noah();
  }

What does the semicolon after the else do?

Wouldn't you be better off using a loop rather than copy/pasting code 11 times?

Now that you've got the stray '342' out of that code, why don't you test it. If it doesn't work, then post a question here and at that time explain what you mean by "good to go".

Pete

Our users shall know fear and cower before our software! Ship it! Ship it and let them flee like the dogs they are!

Outstanding PaulMurrayCbr. Wish I had this information earlier in my career. :slight_smile:

G

Hi,
Welcome to the forum.

I wanted to know if my code chord to generate random chord progressions (and print it on a thermal printer) is good to go.

Have you tried any of your code at all?
Did you write it all at once and you are NOW ready to test it?

I hope you developed your code in stages and keep intermediate copies to build up to what you need.

First code for thermal printer control, and get it working.
Second code for random chord progressions, using monitor as output device, and get it working.
Combine the two codes and get it working.

I notice you have serial.begin setup but don't use serial.print to monitor your code.

My suggestion, give your code a go..... :slight_smile:

Tom... :slight_smile:

el_supremo:
Now that you've got the stray '342' out of that code, why don't you test it. If it doesn't work, then post a question here and at that time explain what you mean by "good to go".

Pete

Sorry...
I am 16 and just getting into this. I wanted to know if my code would work before I ordered a thermal printer.
All the best,
Noah

A bit of context helps.
I'd suggest that you change all the statements so that they print their info to the Serial monitor (make a backup of the code first!).
You can then see if the code is producing what you expect. If it does, it should work with the printer.

Pete

Your code can also benefit from an additional function to send data to the printer so you can get rid of all the repeating stuff. The exact implementation depends on how the printer works.

e.g.

void printIt(char size, char justify, int feed, char *text)
{
    printer.setSize(size);
    printer.justify(justify);
    printer.println(text);
    printer.feed(feed);

}

And in loop() you can use something like

printIt('L', 'C', 1, msgs1[randomNum1);

;