Go Down

Topic: THANKS I GOT IT (Read 358 times) previous topic - next topic

TIJHE_VV

Aug 11, 2018, 07:24 am Last Edit: Aug 12, 2018, 03:24 am by TIJHE_VV
Hi, I have finished this project, Thanls

PaulRB

Please read the forum guidelines in the sticky post so you know how to post code correctly, using code tags.

You should not use digital pins 0 or 1 on the Mega. They are used to upload your sketch from the PC and for serial monitor. There's no problem using analog pin 0.

You should use an array to hold the pin numbers, otherwise your sketch will be 10 times longer than it needs to be.

This line will not work as you hope
Code: [Select]

potVal = map(potVal, 0, 1023, 0.0, 5.0);

The map() function cannot be used with float or double type variables. In other words variables which can have decimal places. It can only be used with int variables, which can only hold whole numbers. Fortunately, that is what you need. You need to choose one of 12 LEDs. The fact that the voltage on the pin varies between 0.0.and 5.0 volts does not actually matter to you in this sketch. So try
Code: [Select]

potVal = map(potVal, 0, 1023, 0, 11);

Note that the range you are mapping to is 0 to 11, not 1 to 12, because these are the indexes you need for the array holding your pin numbers.

PaulRB

PS. When you are choosing a replacement for digital pin 1, you might want to avoid digital pin 13. That pin already has an led attached to it, on the Mega board. If you drive two LEDs with one pin, you could overload and damage the pin.

TIJHE_VV

Ok thanks. Sorry for the layout of my forum, it is my first one and i am having a little trouble navigating the webpage. Your code works well, but the led turns of slowly on tinkercad, i need the led to shut off quickly instead of dimming down. Thanks for all of the help : )

PaulRB

So you don't need any more help?

I don't care what tinkercad shows. What does a real Arduino do, that is the question. When you use a real Arduino, don't forget to use series resistors for each led, eg. 330R.

Paul__B

PS. When you are choosing a replacement for digital pin 1, you might want to avoid digital pin 13. That pin already has an led attached to it, on the Mega board. If you drive two LEDs with one pin, you could overload and damage the pin.
That's funny!  I thought that the Mega 2560 - like the UNO (and even most fake UNOs) had the LED on pin 13 buffered by the op-amp.


TIJHE_VV:

Please go and read the instructions, then go back and modify your post using the "More --> Modify" option to the bottom right of the post, to mark up the code (but it always needs to be the complete code) as such so we can examine it conveniently and accurately.  Please do not post a ".ino" file as an attachment - that would mean that you are expecting people to actually load it to their IDE to look at it and that is extra unnecessary labour.  In fact, attachments do not always show properly on different operating systems.

If you do not mark it up as code, the code you post could well be garbled and is certainly anything but easy to read.

Note: Also mark up any data in the same way.  This includes error output that you get from the IDE.

And - before you post any code, use "Auto Format" in the Tools menu of the IDE to properly present the code.

Try and avoid unnecessary white space (blank lines).  You should only use these to separate functional blocks of code.

PaulRB

That's funny!  I thought that the Mega 2560 - like the UNO (and even most fake UNOs) had the LED on pin 13 buffered by the op-amp.
I did not know that, thanks. Never had much interest in Uno or Mega, so that's probably why.

Paul__B

I'm with you there.  I have one - maybe even two - but can't see a use for it.

Driving lots of LEDs - no, as it hasn't really got the current driving capability; use MAX7219.  On-board regulator not useful anyway.  More complex code?  Use an ESP8266.  Lots of PWM - specialised chips.

Maybe useful for a few more analog inputs.

Go Up