MIDI sequencer not responing to tempo changes

Hello!

I'm trying to begin to code a MIDI sequencer with Teensy 4.1 and 40x4 LCD. Chat GPT 4 gave me this code, but I have a problem with it and GPT 4 cannot find out what the problem is. Now it's your time to shine and show that you are smarter that GPT 4! LOL

So, here's the code. The problem is that even that I change tempo to whatever BMP, the sequencer playback doesn't respond to the change for some reason. Any ideas what's going on?

This is just a first test with millis-function. I think I'm going to use interrupts with Teensy just like GPT 4 suggested.

Thank you!

// MIDI sequencer test
// TEENSY 4.1 with 40x4 LCD

#include <LiquidCrystal.h>

const int rs = 2, en1 = 3, en2 = 4, d4 = 5, d5 = 6, d6 = 7, d7 = 8;
const int buttonPin = 30, ledPin = 13, playButtonPin = 31;
const int encoderPinA = 9, encoderPinB = 10;

unsigned long totalTicks = 0;

const int backlightPin = 13;
const int backlightToggleButton = 30;

volatile int newTempo = 180;
volatile bool tempoChanged = false;

LiquidCrystal lcd1(rs, en1, d4, d5, d6, d7);
LiquidCrystal lcd2(rs, en2, d4, d5, d6, d7);

unsigned long lastBacklightToggleTime = 0;
unsigned long lastPlayButtonTime = 0;

bool backlightState = true;

volatile int encoderPos = 0;
volatile int lastEncoded = 0;
unsigned long lastEncoderUpdateTime = 0;

bool lastButtonState = HIGH, currentButtonState = HIGH;
bool lastPlayButtonState = HIGH, currentPlayButtonState = HIGH;
unsigned long lastDebounceTime = 0, debounceDelay = 200;

bool isPlaying = false;
unsigned long tempo = 120;
unsigned int ticksPerBeat = 96;
unsigned long tickDuration = 60000 / (tempo * ticksPerBeat);
unsigned long lastTickTime = 0;
int currentTick = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(playButtonPin, INPUT_PULLUP);
  pinMode(encoderPinA, INPUT);
  pinMode(encoderPinB, INPUT);

  lcd1.begin(40, 2);
  lcd2.begin(40, 2);

  encoderPos = 120;

  pinMode(backlightPin, OUTPUT);
  pinMode(backlightToggleButton, INPUT_PULLUP);
  digitalWrite(backlightPin, backlightState);

}

void loop() {
    unsigned long currentTime = millis();

    bool backlightReading = digitalRead(backlightToggleButton);
    if (backlightReading == LOW && currentTime - lastBacklightToggleTime > debounceDelay) {
        backlightState = !backlightState;
        digitalWrite(backlightPin, backlightState);
        lastBacklightToggleTime = currentTime;
    }

    bool playButtonReading = digitalRead(playButtonPin);
    if (playButtonReading == LOW && currentTime - lastPlayButtonTime > debounceDelay) {
        isPlaying = !isPlaying;
        if (isPlaying) {
            lastTickTime = currentTime;
        }
        lastPlayButtonTime = currentTime;
    }

    if (isPlaying && currentTime - lastTickTime >= tickDuration) {
        lastTickTime += tickDuration;
        currentTick = (currentTick + 1) % ticksPerBeat;
        totalTicks++;
        updateSequencerDisplay();
    }
}

void updateSequencerDisplay() {
    int measure = (totalTicks / (ticksPerBeat * 4)) + 1;

    int beat = ((totalTicks % (ticksPerBeat * 4)) / ticksPerBeat) + 1;

    // Tikit yhden iskun sisällä (1-96, jos ticksPerBeat on 96)
    int tickWithinBeat = (totalTicks % ticksPerBeat) + 1;

    char buffer[40];
    sprintf(buffer, "[SONG:01] [M=%03d/%02d/%02d] [T=%03lu 4/4]", measure, beat, tickWithinBeat, tempo);
    lcd1.setCursor(0, 0);
    lcd1.print(buffer);

    lcd1.setCursor(0, 1);
    lcd1.print("   |");
    for (int i = 0; i < 4; i++) {
        lcd1.print((beat - 1 == i) ? char(255) : '.');
        lcd1.print(".......");
    }
    lcd1.print("|");
}



Sorry to say I really hate people using ChatGPT for coding then coming here whining/asking for help because know almost nothing about programming and/or how that sh*tty generated souce code works.

It's like going to your doctor saying that a Google search said I should take some medicine but it didn't work. The doctor would send you to hell.

So, start studying a bit more then come back here with a code you made.

Yes, LOL. I hope you realize that making that kind of statement is srsly unlikely to motivate anyone. But thanks for telling us this pile of statements was chatGPT.

What does this code do that it isn't supposed to, or not do that it should?

I see no MIDI.

Ask chatGPT to help you insert some serial printing to check the values of key variables and confirm that they are properly informing the flow through "your" code.

Copy the first thing, or few things, you told chatGPT about what you want to accomplish. We deserve to know at least as much. Right now even you first goals are entirely unclear, and I for one will not try to reverse engineer a specification out of not working code.

a7

Thanks, guys! Really helpful info here! Please, get some fresh air sirs!

Don't be so touchy if we tell you to start using your brain and learn something about coding, instead of relying to an external tool and whine here that you can't make it work.
ChatGPT won't let you avoid "no pain, no gain" motto!

Heh, what a joke you really are, sir! Try to relax man, no need to get so triggered with GPT4. Everything's ok.

Sorry that I asked, guys! I'm really sorry.

You are welcome.

Yes, yes you are.

a7

When you ask a question, you'll get an answer. Clearly here. Ask coder a question and here we are...I understand.

BTW, did you spent at least a bit of your time to try and understand the code?

For instance, you say:

How exactly are you trying to change the tempo?
And, with just a quick peek at the code, where do the "tempo" variable (and consenquently "tickDuration") changes?

Just to see if you'll give it a try...

Friends, no problem anymore! Problem solved! I managed to find out what the problem was by myself...with...(sorry to say and you are going to hate it but...) help of Chat GPT! Hih. Cannot wait GPT 5 and 6...It's pretty funny that you get more human answers from GPT AI than from the real human...LOL. Ironic and sad, but true.

The problem was that the LCD is not capable of loading so much data so fast.

So, you assumed right away when you heard the magic word "CHAT GPT" that I just copy&pasted the whole code to this forum...ouh...You assume too much, too soon...no wonder you started to flood hate replies immediately! But if GPT is coming to your table...starts eating your bread...it's totally understandable.

No, the original code didn't have any midi functions yet because I was only testing sequencer's timing. No idea to put midi codes there before the timing is correct...you see...So, it was not GPT that left those out, it was BY purpose.

It isn't a matter of "my bread". It's just a matter of "using own brain" instead of relying to a tool. If ChatGPT makes mistakes and you don't know what it made and how it works, the only correct solution is getting back to ChatGPT and use it to solve the issue, as you did now, and not asking us to waste time trying to understand the generated code (without even knowing what you asked it) and give you the "free lunch".

Anyway, I'm happy to see you solved the issue, and I hope you'll start a better learning than waiting for something/someone do the job for you.

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