Using Chat GPT AI to Code an Arduino MIDI Controller

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
- Hello. So in this video you're gonna have your mind blown because we are going to write a full code for a MIDI controller using AI, using Chat GPT. So let's get it started. In the last couple months, the internet was stormed by platforms such as DALL-E 2 and Midjourney, where you can use AI to generate art, images fake photographs, et cetera. I got really addicted to Midjourney. I did a bunch of things, but now there's something new in the market, which is Chat GPT. But what is Chat GPT? Well, let's ask Chat GPT what Chat GPT is. Hello, Chat GPT. Can you explain to my audience, audience, what Chat GPT is? I'll explain. So sure, GPT or Generative Pre-trained Transformer is a type of machine learning model that has been trained on a large data set of text. This training allows the model to generate human-like text that is often difficult to distinguish from text written by a person. I'm not going to read you everything, but do you realize that I just made a question and it answered to me? It answered to me, but one really nice thing is that it can keep track of the conversation. So can you summarize this in one paragraph like a five year old kid? Chat GPT is a computer program that can talk to you and understand what you say. It can make up new sentence, stories like a human does. It's like having a robot friend that can talk to you and help you with things. Okay, but where the Arduino, the code thing enters in this conversation? Can you write Arduino code? Yes, I can certainly help you with writing Arduino code. Arduino's a popular open source platform, and even explain you what Arduino is. So I have been messing around with that and it's pretty mind blowing. And today I'm going to attempt to write a full code for an Arduino MIDI controller, only asking Chat GPT to write it for me. So let's see what we can do. So I have here this Arduino with a couple buttons and an LED. So let's try something more simple first. I have this LED on pin nine. So blink an LED on pin nine in Arduino. So it said, sure it can write it for me. So pin mode, yeah, nice and outputs for the LED, then digital right pin nine high. So it looks like a perfect code to me. So let's copy this code. Let's open the Arduino IDE. And even explains you what it did. So let me select my board. Okay, Leonardo, and let's paste that code and upload. And bam, we got an LED working. We can make the blink faster. So now I asked to make it blink faster. So it explained to me that instead of using delay 1000, it can use delay 500. So yeah, for at least really simple things, it works. Okay, but now let's try to make a MIDI controller. To see first if it can do, let's say, write an Arduino code that sends a mini note every one second. Okay, so it sent, and also sends a note after one second of the note. Does this make sense? So instead of only sending the note on, that's what I asked, I asked to also send a note off. So I guess it did it right. So let's try. So gave me this error. Sometimes you need these, sometimes you don't. And let's see if this is working. Let's open Hairless. Let's open Ableton for the piano. (piano sound) So it's sending a note on and then a note off, only asking Chat GPT. Let's see if we can do something different. Send a G major chord instead of only one note. Good lord. Let's try this. Okay, let's comment this. (piano sound) What the heck? Okay, so we see that we can make a generator of MIDI notes and then it's up to you to play more with that. But let's write to write the MIDI controller code, okay? So let's see. Write. So write an Arduino code for a MIDI controller with three buttons. So this is the code and we can see that it's not really going to work. First one thing I'd like to change is that it created separate variables. I'd like to see this in erase in lists. If the button is pressed, send a note on and then a delay. We don't want this delay. It knows that it has to do a debouncing. Debouncing is that when you press a button, sometimes it bounces and it sends multiple notes. So you have to add this debounce algorithm, but that's not how you do it. But let's just upload this just to see what happens. So it's sending an error, it's not even working, so let's try to fix this. So for me, what the problems are. So let's try to make it use erase and get rid of this delay. Use erase and for loops, add a debounce algorithm. You can see here that already changed this to this which is the neat way of doing. Now it is using input pull up, which is the right way of declaring a button wired this way. If you don't know what I'm talking about, please check, my course is here below. So let's see what it's doing here. Let's copy this code and try again. We're getting there. So see what happens. Let's see what happens. Let's see what happens in Ableton. So if you press (piano sound), it even chose the same arpeggio for us. So it's sending multiple, multiple notes and that's a common thing you do in your code if you're not really aware of the right way of doing. So we need to explain Chat GPT what's the right way of doing it. So only send a note on once when the button is pressed and send a note off once when the button is released. So you want to press and just send once, want to release and just send the note off once. So let's see if that fixed this problem. So let's see now. Let's turn this on, and. (piano sounds) No. (piano sounds) Still doing the same thing. Let's see if we can fix this. So let's try to rewrite the code in a way. So rewrite the code in a way that note on is only sent once when a button is pressed and a note off only once when a button is released. So trying to say the same thing but in a different way. Let's see it again. I'm not really sure. (piano sounds) Ha, it worked. Okay, what else can we do? Now we have buttons working perfectly, but let's see if we can make this code more organized. Put everything related to the buttons in a void function called buttons. Okay, so we have our code with a different function where a function like it's a container which contains all this chunk of code, and now this chunk of code is inside this function or this container code, handle buttons. So now we need to add the potentiometers. So add to this code two potentiometers that send MIDI control change when the potentiometer is moved. Use erase. I'm already asking it to use erase. So, okay. So now it added a new function and I didn't even ask, so thank you, Chat GPT. So let's see what it's doing. It's reading the potentiometer and it's sending control change every 20 milliseconds. That will not work properly but let's see how it looks like. So you can see that gave me an error, probably because it's sending so many messages that it's just getting overwhelmed and Hairless can't deal with that. So let's try something more specific. So the algorithm for that is something like, so only send a control change when it's moved more than a certain threshold. And then when it's moved, send control change for, let's say 300 milliseconds and then stop and only send again when this happens again. So let's try to explain that. So change the potentiometer part to, when I move the potentiometer more than a threshold, keep sending CC for 300 milliseconds. If the potentiometer is not moving, then a threshold, don't send CC. So that's basically the algorithm. So this time it stopped writing and then I asked it to keep going and then it kept going. I don't know if there's a limit, a character limit, but let's see what it did. Let's write it out and copy the rest and upload. Still getting an error. So let's ask to delete the potentiometer part and let's try to do it again. Okay, so we are back to what we had before. So let's try to write the code for the potentiometer separately and let's try to mix everything together later. So write an Arduino code that has two potentiometers, use erase, when I move a potentiometer more than a threshold, start a timer. While this timer is less than 300 milliseconds, send MIDI control change. So I change to every time I move a potentiometer more than a threshold, so it means that every time we move more than a threshold, we are going to restart that timer. So let's see how that goes. Okay, so let's see if that works or still not. And we are getting an error here. Okay, so let's see what's happening here. We are storing the value of the potentiometer in this variable, and then we are calculating what's the variation of the potentiometer. So we store the value in this variable, which is storing, let's say... The value now we are storing in the variable called potValue. So the next time, we are going to compare this potValue, which is our previous value, with our actual value, which is the current potValue. So if this is bigger than the threshold, we start the timer and we store that in the potValue. So if the time now less than time before is still less than 300 milliseconds, send that value, send the control change. So that should work, I guess. Maybe it's a Hairless MIDI thing. So let's try to print this. So what I'm doing here is checking if the problem is with Hairless or with our code. Let's start the serial monitor. So it's actually working. It's actually working. The problem is with Hairless MIDI. So let's see if only adding the about rate with Hairless MIDI, it will work. Okay, let's see now. So we're still having a problem with Hairless MIDI and I truly hate Hairless MIDI. Hairless is always a pain. So let's try to do something that I already tried it and it's really hard, which is, instead of using the MIDI library, let's try to use the MIDI USB library. So use the MIDI USB instead. So it really didn't add the correct functions of the MIDI USB library. One time that I was doing, it actually did, so I don't know exactly how to reproduce that but maybe you're gonna have to do some manual labor. But before, let's try to mix both codes. So, okay, now it got me one code with both mixed but we still have the problem that is not using the MIDI USB library properly. So I'm gonna do some other tries, try to write in a different way to see if we can get it. Okay. So it's generated me two different functions using that MIDI event packet and things like that which I think is correct but there's still one thing that I know it's not correct which is this. It's not MIDI, it's MIDI USB. So when sending a MIDI value, change the word MIDI for MIDI USB. So here instead of MIDI is MIDI USB. In this way I spelled. Okay. So I think we might be getting there. So it did that thing again. I had to ask it to keep going, to keep going writing the code, but it can just copy this first part and then copy the second. And let's verify. So this, it's not necessary for the MIDI USB library, so I'm just going to comment. And it compiles. So let's see if it works now. Okay, so we are not using Hairless MIDI anymore, so we can check our MIDI message in MIDI Monitor. So it's in our Arduino. So the potentiometer is working, but the buttons are not. (piano sound) So let's see what's going on with the buttons. So let's try to print something here just to see if actually the algorithm is right. Only the MIDI messages that's not, so let's see if that's the problem. (piano sound) So yeah, that's the... The problem is with the MIDI message. Let's find out what is the problem. So if you see how I'm doing my code, every time you send a note or whatever, you need to add this to your message. After the message, you need to add this MidiUSB.flush. So now it's separated in three messages, but I just asked, add MidiUSB.flush. I didn't even add, I said where, and it actually added to the right place, which is right after the MIDI message. So, okay, let's copy that and paste it. Let's see if it compiles. Okay, we don't need this MIDI USB begin. So let's try again. Okay, done uploading. So MIDI Monitor, let's see. (piano sound) A note. A note off. (piano sound) Oh my goodness. Ah, I think it's working. Okay, okay, okay, let's open Ableton. Ooh, ooh, okay. (piano sounds) Okay, so we have the notes. So let's map this to this and this to this. So. (piano sounds) A reverb and a delay. (piano sounds) Oh my God. It's working one note at a time, but it's sort of working, not working with all the notes at the same time. So let's ask why. So I asked it to fix it. The code is not allowing me to play multiple notes at the same time, so fix it. Let's see if it can. So it gave me a new function for handle buttons and also this to this variable. So let's add that. And let's copy the void function. Okay, let's upload and see what we get. So. (piano sounds) Still not working really fine. I mean we could still keep going and asking to change that, this and that, but in the end, we might be spending more time asking Chat GPT to write for us then writing this ourselves. So what are my final thoughts with it? To be honest, I'm utterly amazed by what Chat GPT can do. It can talk to you, it can write original content in basically any fields that there is. It can write computer code in many different language. It can give you ideas for new stuff. And we still have a long time until we can figure out everything we can do with this. And there's so much more things that we can do that we don't even thought about it. YouTube now is full of videos of people doing all sorts of different stuff with Chat GPT. And today I tried to make a complete code of a MIDI controller using the Arduino. Although I couldn't do it 100%, the fact that I almost did everything only asking Chat GPT is amazing. So what is ahead of us? This will replace a lot of people. So not only customer service, for example, you can see this can program, this can write code and the platform is only in it's early beginnings. It's still a prototype. Imagine what it can do in one year, five years, 10 years. And here, to be honest, I spent more time trying to explain it than I would do if I wrote myself. So the way I see it now, at least for me, it is as a great learning tool. So imagine, I don't know, how to write a code for a display. I can ask it to write, write a code for a display, blah, blah, blah, with the words, hello world. That would be a great starting point for me. But if you want to make something more complex like I did here, you really need to know how to code because you need to know how to explain the machine what to do. You need to know how to think as a programmer. And if you know it, you probably know how to write the code yourself. So I don't think that good programmers will be replaced right yet, but beginners, yeah, you might. But to be honest, the easiest way for you to build a MIDI controller, I don't think that right now is using Chat GPT, but taking my course, The Nerd Musician Pro, yeah, there I teach you how to build a MIDI controller with buttons, potentiometers, motorized faders, router encoders, displays, LEDs. You can make MIDI controllers with MIDI cable, USB or even Bluetooth. And now that you know how to use Chat GPT, it can help you solve doubts that you have along the way because then you get the best of both worlds, a course with step by step lessons, me, and the community to help you out, and now you have this amazing tool to help you solve your doubts and even teach you how to write some chunks of code. And of course, do everything else that Chat GPT can. So have you used Chat GPT? What do you think is the future for people like us, programmers, people that are building stuff or creating content? Tell me here in the comments below, okay? So please subscribe, if you didn't do, you help me a lot, and give a like to this video. So see you in the next one. Ciao. (upbeat music)
Info
Channel: Nerd Musician
Views: 5,988
Rating: undefined out of 5
Keywords:
Id: dnrnOscJY2E
Channel Id: undefined
Length: 21min 34sec (1294 seconds)
Published: Thu Jan 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.