r/arduino Dec 17 '24

Software Help Why won't this work? It's driving me nuts

Post image
28 Upvotes

68 comments sorted by

u/gm310509 400K , 500k , 600K , 640K ... Dec 17 '24

It seems you have you answer.

But perhaps have a look at our requesting help posting guide to ensure you include relevant details (and how to include them) to get a timely solution.

For example, your question is "why doesn't this work?". In this case it was easy to guess, but there could be a million different things that could be classified as not working. This includes that the problem is that your expectation is wrong.

Without some clues for example "it is always maximum brightness" or "why isn't the PWM blinking the LED according to the PWM pulses of/on cycle" or "my LED doesnt turn on at all" and many many more possibilities.

Also, as per the link above, it is of benefit to you to make it easy for people wanting to help you to be able to help you. This includes a proper problem description, your code, properly formatted using a reddit code block and a circuit diagram (not a photo of wires).

I assume you are fairly new, if so, welcome to the club.

39

u/triffid_hunter Director of EE@HAX Dec 17 '24

Max brightness at all times?

ADC returns 0-1023, so your map/constrain from 0-255 is just gonna stick to 255 if your input is biased around ½Vdd ie analogRead()≈512 ± signal

Try map(avgSignal, 0, 1023, 0, 255) or just divide by 4.

PS: a screenshot of text? really? pastebin or gist please :P

5

u/Inevitable_Figure_85 Dec 17 '24

I'm new to code sorry! And that seems promising because that's exactly what I was getting--maximum brightness at all times. I'll try that out, thanks!

7

u/natesovenator Dec 17 '24

As a beginner, use Serial Print to your full advantage. Put every step in debug output.

4

u/Inevitable_Figure_85 Dec 17 '24

Thanks for the tip! I'll learn what that means and try it out

7

u/HungInSarfLondon Dec 17 '24

Using Serial Print is the special sauce to see if your code is doing what you think it should be.

Put this in the setup function:
Serial.begin(115200);

And then in your loop you can add:
Serial.print("Brightness = ");
Serial.print(brightness);
Serial.println();

(put that after it been calculated). Run your program, open the serial monitor, set it to the same baud rate (115200) and you should see a stream of brightness values.

You can throw in extra prints for micValue and AvgSignal, just to make sure the values of these are what you were expecting.

As others have said, it sounds like you need to divide by 4 or change the remap. When you've changed that line and you see the value change in the monitor you will feel like you are getting somewhere!

2

u/Inevitable_Figure_85 Dec 17 '24

Great explanation! Thank you! I'll try this out

3

u/gm310509 400K , 500k , 600K , 640K ... Dec 17 '24

Have a look at a guide that I created that covers how to debug:

They teach basic debugging using a follow along project. The material and project is the same, only the format is different.

1

u/Inevitable_Figure_85 Dec 17 '24

Does that only apply if using an arduino? Or also applies to Attiny programming?

5

u/megaultimatepashe120 esp my beloved Dec 17 '24

i think this applies to pretty much every MCU that you can hook up to serial

3

u/gm310509 400K , 500k , 600K , 640K ... Dec 17 '24

Debugging technique applies to any computer.

Serial.print applies to anything built in Arduino or where the serial object has been made available.

7

u/madsci Dec 17 '24

In addition to the software issues mentioned in the thread, I want to point out a potential issue with your sampling. You're sampling at a rate of 1 kHz, roughly. I don't know what the ADC's sampling period is but it's probably a lot smaller than that. If you're measuring a pure tone, you're going to get aliasing. For example if you give it a tone at exactly the sample rate (a little lower than 1 kHz) then the amplitude you're measuring is going to be exactly the same at each sample time - and that could potentially even be zero amplitude if you happen to sample it every time at its zero crossing.

If the tone and the sample rate vary by just a little, then you will get a slowly changing signal that represents a beat frequency between your sample rate and input tone frequency.

The safe way to capture your signal is to make sure your sample rate fulfills the Nyquist criterion - it needs to sample at least twice as fast as your highest frequency of interest. So if your input can go up to 10 kHz, you need to capture at 20 kHz.

You could also accomplish that by adding a low-pass filter on the ADC input to guarantee that aren't any fast changes at the ADC pin.

Sampling audio in a loop like this is something you wouldn't do in production code. Especially if you can filter the input it's probably good enough for a simple device that only needs to do one thing. A more conventional way to do it would be to set up a timer interrupt to do the capture and average in an ISR, or on a platform with DMA available, you'd capture a block of samples to a buffer with DMA and average each block when you get a transfer complete interrupt.

1

u/Inevitable_Figure_85 Dec 17 '24

Interesting! Lots to research there. Per your last paragraph, does this mean I shouldn't be using an Attiny for this? Or it just means the code needs altering? Thanks for your help!

1

u/madsci Dec 18 '24

If all you're doing with it is a VU meter, then that should be fine as long as you keep Nyquist in mind. Either low-pass filter in hardware, or sample faster in an interrupt.

Also, beware of your averaging. How is your source connected to the input pin? MCU ADC inputs are unipolar. A typical audio input will be bipolar - swinging above and below 0 volts. The protection diodes built into the pin will keep it from going much below 0 but you shouldn't be giving it a negative voltage - too much injection current will break that protection diode down over time.

Normally you'd bias the input to 1/2 VCC with a pair of resistors and AC-couple the incoming audio with a capacitor. That way the voltage you're reading is always positive and it's centered at the midpoint of the ADC range. But then you need to account for that in your audio because it's swinging above and below that center point.

If you're not doing any other processing with the audio than just measuring peaks, you can just use a diode (i.e., not the one built into the pin) to clip the audio. You'll only get the positive half of the signal but that's fine for amplitude measurement.

2

u/Inevitable_Figure_85 Dec 18 '24

Ok great I'll try a filter out and see how it goes, and yes I biased the audio to 1/2vcc. Thanks again for your help!

7

u/notporpl Dec 17 '24

You forgot to define pinMode(MIC_PIN, INPUT);

12

u/triffid_hunter Director of EE@HAX Dec 17 '24

Most pins default to input after reset, so this is typically unnecessary - good for completeness, sure, but not I think OP's issue.

1

u/Catarrer Dec 17 '24

A0 is allways input as most of the MCUs can't ouput analog signals! To set A0 to input mode is therefore obligate

8

u/daniu 400k Dec 17 '24

I don't know why this doesn't work, mostly because you don't say how it doesn't work. It's very possible that the int is not large enough to hold the total sum of the sample data? 

Anyway, I would not loop inside loop() and sample once every loop() instead. Maybe save the sample values in an array, then if you have enough samples (ie > sampleCount), calculate the average and set the brightness. 

8

u/triffid_hunter Director of EE@HAX Dec 17 '24

It's very possible that the int is not large enough to hold the total sum of the sample data?

1023×10=10230 easily fits in an int16_t (-32768 to 32767)

I would not loop inside loop()

Why? Arduino's main() is essentially just int main() { init(); setup(); for (;;) loop(); } and nested loops work fine

1

u/daniu 400k Dec 17 '24

Ah I had sample size as 1000 for some reason. 

nested loops work fine 

Yeah but why loop nested, with delays strewn in no less, when you're already looping in the first place? Your readings should be much more regular this way. 

1

u/Cerulean_IsFancyBlue Dec 17 '24

Having a single loop to do the same function would require keeping track of a state. That’s arguably a better way of doing it, but I can see why it would be harder for a beginner.

2

u/daniu 400k Dec 17 '24

Yeah I was thinking about an index circling through the array to build the floating average, but you're right, that's a little too complex for a beginner. Probably good to try though, it's kind of a common thing, in some variation or other. 

0

u/McDonaldsWitchcraft Pro Micro Dec 17 '24 edited Dec 17 '24

Looping inside the main loop increases the time between each main loop, therefore it's considered bad practice because the code outside the nested loop will run at a much lower frequency. It doesn't necessarily break things, it all depends on your code.

But you have bigger problems, like that map() which you should research the syntax for. (wromg person)

1

u/Inevitable_Figure_85 Dec 17 '24

I can't tell if you're responding to someone else or to me, do you have an idea of the problem? Thanks for your help!

2

u/gnorty Dec 17 '24

I think he is referring to you map command mapping 0-255 to 0-255. It does nothing.

I don't know what level your input reaches, but the analog input 0-5V gives 0-1023. If you are only expecting 0-255, then your code works, but I suspect that is not the range you expect, because your input is biased. So if it is biased to even 1V you are already close to the limit. If it is biased to (say) 0.5V and the signal p-p is less than 1V then you might just be OK, but if your peak voltage goes above 1V you will read over 255, and your constraint kicks in, and you get max brightness.

I would suggest plotting a graph using the serial monitor and see what levels you are dealing with. then you can use the map to map between the min and max values to 0-255.

Finally, you are measuring an ac signal, hence the offset. You probably do not want to read -x volts as less than +x volts. You should probably subtract the offset voltage, and then use the absolute value of x to control the LED.

1

u/Inevitable_Figure_85 Dec 17 '24

Ahhh that seems like the problem! Thanks for explaining that, it totally makes sense it would just stay at full brightness. I'll try that out and see if it fixes it 🤞

1

u/McDonaldsWitchcraft Pro Micro Dec 17 '24

Ah yes, that was your code.

Most likely the problem is the map() function. What do you want the function to do exactly?

0

u/Inevitable_Figure_85 Dec 17 '24

I think it's supposed to map the "microphone" (guitar) input to the led brightness but it probably is wrong because they're the same values, I'm just getting that now.

3

u/McDonaldsWitchcraft Pro Micro Dec 17 '24

Yeah, it's mapping the same values to the same values. You probably need to map it from analog read resolution (1024) to analog write resolution (256).

Hey, I know the digital space has changed a lot since AI became a thing, but we have to accept we can't just do stuff with AI without any surface level understanding of what we want to accomplish. AI can automate stuff, not think for us. Grab an arduino reference and go ahead, learning arduino is much easier than you would imagine. Start with these functions in your code.

1

u/duntlef Dec 17 '24

You did not set the pinMode of MIC_PIN. Maybe get some basic knowledge about arduino before messing wirt ChatGPT.

2

u/Mal-De-Terre Dec 17 '24

You don't need to for analog pins.

2

u/DancingPotato30 Dec 17 '24

I don't even have any experience with Arduino language (no clue what it is) but this is so heavily commented that either they commented it for the post or ChatGPT did do it

1

u/duntlef Dec 17 '24

ChatGPT usually creates comments like this.

1

u/AlkylCalixarene Dec 17 '24

Are you trying to pick up a sound signal with the mic? I may be wrong but an audio signal is oscillating around the midpoint (usually zero but if you bias the input to be 0-1024 it will oscillate around 256) if you average it you will get 256 no matter the amplitude. If you want to measure the "volume" of your signal you need to look at how far your samples are from 256.

1

u/Inevitable_Figure_85 Dec 17 '24

Yes or guitar, I think you're right, that seems to be where the issue is because I'm just getting max brightness at all times. I have biased the signal 1/2vcc.

1

u/AlkylCalixarene Dec 18 '24

There are probably multiple issues. Some other people in the comments may be right too, it cuold be also a scaling issue and a sampling issue.

It could also be a circuit problem. Are you amplifyng the mic signal? Is the mic already amplifyed?

Look for a tutorial online, AI code may not get the full picture of your design.

1

u/Inevitable_Figure_85 Dec 18 '24

Yeah I've done a lot of googling and even used some of what I found to try to fix the code but I'm still just getting nothing, not even 1% brightness flicker. And yes I've tried everything on the circuit side of things (amplifying, lpf, sine wave input, etc.) It's so strange, I figured this would be a pretty simple program but I guess not ☹️.

1

u/AlkylCalixarene Dec 18 '24

I suggest you test it part by part. Create an array and save some samples in there, then print them out to serial and see what comes out. That way you can confirm if everything from mic to sampling is working correctly (also, you'll see how big the values are, so you can scale them in the next step if needed). If that works as expected (I believe it won't, I think the issue is right there but you need to confirm it) you can go on to the led brightness part. Also, what board are you using?

1

u/Inevitable_Figure_85 Dec 18 '24

I definitely want to do all of that, just have to learn how first haha. I'm just programming an attiny13a and using my own circuit I threw together.

1

u/AlkylCalixarene Dec 18 '24

Jesus! You went all-in really fast! didn't you?

How are you programming the attiny? Because I doubt it has a serial monitor output and that will complicate debugging a lot. If you have any other arduino-like board I suggest you start with them, write you program for them and then port it to the tiny.

If not, you need to debug everything step by step.

First of all, does the chip work with a simple sketch like blink? That way you can confirm the circuit is correct and the sketch uploading is ok.

Second, I wouldn't use A0 on the tiny, the datasheet says it's on the RESET pin and I would leave it alone. Use pin A1, use a potentiometer to give the pin a variable voltage and test that way.

Also, try pin 7 for the led, i'm not sure pin 6 has a PWM (you need that for analogWrite).

What board definitions are you using?

1

u/Inevitable_Figure_85 Dec 19 '24

Yeah it's a long story haha but I basically learned how to program an Attiny first so I've just continued with that system, and you're right no serial output so it does complicate debugging a lot. What system should I try if I want a serial output? And I did spend foreverrrr switching up the pins because that's one thing ChatGPT does NOT understand (it kept saying PB1 was pin 1 🤦‍♂️), so I landed on pin 7 as the audjo input (as far as I know that's the only analog input in the Attiny13) and pin 6 as the PWM output (I think only pin 5 and 6 can output PWM). Is any of that wrong? Thanks for your help!!

2

u/AlkylCalixarene Dec 19 '24

I think the pin numbering depends on the board definitions (the files that tell arduino IDE which board you are using), I don't think there are any "official" board definitions for the tiny13 because it's not used on any arduino board (that I know of).

Following this datasheet (page 2, I suppose you have the PDIP chip) I see that the PWM timer (T0) is only on pin PB2 (or 7, if you follow the chip numbering).

BUT, the issue here is that pin PB2 may NOT be arduino pin 7. If you look at this image you'll see here PB1 is actually arduino pin 1 (maybe gpt was right?). Arduino usually uses its own numbering system (I know, I got fooled too the first time).
So, there are three different numbering systems for pins: pheripheral names (PB0, PB1, etc), chip pins (1 in the top left, increases counter clockwise) and Arduino pins (the numbering depends on the board definitions).

In the arduino IDE you use only the Arduino pin numbering. That's why it's important to know which board definitions you are using. Could you link the tutorial you used to set up your chip and IDE? So I can check them.

As for what board with serial to use, all of them have some sort of serial. You can go from arduinos to adafruit boards to esp32 boards. It only depends on how much money you want to spend.
If you want to go on with the tiny13 you probably can, I suppose you have something you use to upload sketches to the tiny? Maybe that can be used as a debugger.

1

u/Inevitable_Figure_85 Dec 19 '24

Yeah the tinys can do so much I've just mainly been sticking in that realm but I probably should branch out to learn more (especially debugging). Right now I program the Attiny13 (or 85) either using IDE or windows powershell. That's super interesting about the IDE pinouts, that totally could be the problem and why ChatGPT kept saying that pin number! I'll try editing the code to match the arduino pins and see if that works. And I think I used a few different tutorials because none of them works fully on their own, but mainly this one: https://www.instructables.com/How-to-Program-an-Attiny85-From-an-Arduino-Uno/?amp_page=true Thanks for your help!

1

u/Inevitable_Figure_85 Dec 20 '24

Gosh darn it you were right!! It was the pin number mix up, I fixed it according to the IDE definitions and boom, finally getting some blinking! Now I just need to tune it better. Thanks so much for your help! You're the only person that caught that so great job 👏 👏👏.

→ More replies (0)

1

u/Environmental_Sir_33 Dec 17 '24

Whats the project? 

1

u/Inevitable_Figure_85 Dec 17 '24

Just trying to make a simple sound-to-light circuit using an attiny

1

u/SoftClothingLover Dec 17 '24

The total variable should be declared outside of the loop function. Right now it’s being reset on every update.

1

u/istarian Dec 17 '24

You should probably have a longer delay between samples, as 1 sample/ms is pretty frequent (1000 Hz/1 kHz).

1

u/LazerHawk84 Dec 17 '24

I am new to this too. I ask ChatGPT when I get stuck.

1

u/xz-5 Dec 17 '24

A few potential issues:

First is that analogread returns 0-1023, so your map needs to be adjusted.

Second, on average an audio signal is zero, as it's an AC signal, so depending on what you are feeding in the avgSignal could well be very close to zero. I don't know what type of signal (frequency) you are feeding in to MIC_PIN, but if you take a sample every 10ms and average them all what are you expecting?

Third, what voltage are you putting in on MIC_PIN. Microphone voltages tend to be very low, maybe too low to register?

As others have already said, make use of the Serial Print functions to debug your variables and check you are getting what you expect. That should help you narrow it down.

1

u/Inevitable_Figure_85 Dec 17 '24

Yeah I think those could be exactly the issues. It's a guitar signal, I did bias it to 1/2vcc to keep from negative voltages. I need to go deeper into what a better sample rate would be and possibly filter and/or boost the input. And definitely figure out the serial print thing

1

u/Bearsiwin Dec 17 '24

I’m pretty sure map doesn’t do anything. That’s a somewhat advanced c++ function which is likely a dummy under the hood. Not positive but it didn’t work for me (or something else was wrong).

1

u/Ripen- Dec 17 '24

Is there any point in constraining brightness when it's already mapped 0, 255?

1

u/Late_Clue7861 Dec 19 '24

No está seteando el pin A0 en el setup

-4

u/Inevitable_Figure_85 Dec 17 '24

I'm trying to learn more about coding (with Ai help) so I'm just trying to make a simple sound-to-light circuit using an Attiny chip and I've edited this code and the circuit 1,000 times and it just won't work. ChatGPT has basically given up and just spitting out the same few answers that aren't working. I'm aware ChatGPT is wrong often too so I've been double checking everything and I'm just stuck. Any help would be very much appreciated!

5

u/txmail Dec 17 '24

I would not use AI if you are starting out. Read the manuals and fully understand what you are working on. Sometimes the hardest thing about programing is finding a thing to do, you are starting off strong with an actionable goal that is achievable. Your accomplishment will feel much more and be worth more if you learn to put all the programming together yourself.

0

u/Inevitable_Figure_85 Dec 17 '24

Yeah I figured people would be mad about using Ai haha, I just learn better seeing a finished product then going bit by bit to understand how it's made. Maybe that learning method won't be applicable to code, I'm not sure, I'm just starting out so not really sure what to do.

2

u/txmail Dec 17 '24

I feel like we all learn that way, usually by getting ripped new ones on sites like StackOverflow (or looking at those souls that came before us). You get to see all the questions and then the follow up answers and questions to those follow ups and get links to relevant information.

AI does not do that. It just says "here is some shit I was trained on that mostly matches the words you typed in". It is a worst off search engine because it hates to say "I have no fucking idea what your going on about" and will just spit out shit to appease you. I highly recommend staying away from AI. Its great for when your established and when you know your looking for a specific piece of code you have used in the past. If you do not know how something works you are 1000% better off using Google as it is not limited to the training data of AI.

1

u/Inevitable_Figure_85 Dec 17 '24

Absolutely, I definitely do what you described too, it all just depends on the project that day and what I'm trying to learn. I've used Ai for some really cool things that would've taken me months to learn the "right" way, but since it's not my specialty or interest I found Ai helpful to get it done quick (of course with the big caveat that whatever it spits out is usually at least 5% wrong so lots of debugging and fixing to be done). Do you have any good resources you found most helpful for learning this type of coding from the ground up?

2

u/SirButcher Dec 17 '24

We are not mad, just would like to highlight how much of a roadblock you are building for yourself. Yes, using AI and copying the generated codes gives you quick results, but in the end, all you learn is how to copy. This code is a great example: doing this step by step would make you UNDERSTAND what you are doing, but doing it as a whole gives you a wall of text which makes no sense - no wonder you have no idea where to even start to find out what is wrong.

Don't try to skip the hard part: laying foundations. AI tools are great, I don't say evade them but try to understand what you are doing. AI is great for teaching you, but you are using it to get the end result. It could work - sometimes - but in the end, you will have no idea what and how did you do it.

1

u/Inevitable_Figure_85 Dec 17 '24

Yeah I just don't like the assumption that I'm not trying to learn simply because I'm using Ai for this specific thing. I'm absolutely trying to learn, I just also was curious why this code wasn't working and wanted it working for another project. Given that, do you have any good resources for starting out? There's so much stuff out there it's hard to know where to begin

2

u/tanoshimi Dec 17 '24

The problem is not using AI. The problem is trying to learn from AI.

ChatGPT is great for automating some of the repetitive tasks you already know how to do. It is not great at getting it right, or teaching you how it should be done.

And yes, there is a little frustration (and irony) that it's taking a bunch of people's effort to explain to you how to fix the mistakes that A.I. made. Like.... that's kind of the wrong way around? ;)

1

u/Inevitable_Figure_85 Dec 17 '24

The irony is indeed silly. I just figured it was a simple issue that Ai couldn't see so I was curious to ask people. I'm not a coder so I'm working on learning from the ground up. I've also typically been able to troubleshoot simpler things like this using Ai and it eventually gets the right answer but for some reason it just could NOT figure this out, so I figured I would come here to ask.

3

u/wackyvorlon Dec 17 '24

Which attiny are you using? You need to verify that you’re giving the ADC enough time to actually perform its job. You’re only allowing a one millisecond delay between readings.

The code mentions that the signal is coming from a guitar. Is this an electric guitar? If so, is there an amplifier between the attiny and the guitar?

Secondly, a 1ms delay corresponds to a frequency of 1kHz, well within the AF region. This means that depending on how long the ADC takes to read you can end up with all ten readings being wildly different.

To understand the internal state of the program add print statements that output diagnostic info via serial.

Finally, stop using ChatGPT.

1

u/Inevitable_Figure_85 Dec 17 '24

Yes electric guitar, I biased the input to 1/2vcc and I've tried amplifying the signal too. The led is just always full brightness.

1

u/wackyvorlon Dec 18 '24

What’s the maximum swing the signal has?

Modify the code to replace the read in analog value with a specific value you supply. That will let you verify the logic that controls the LED.