Unit 6 Lesson 16- Random Pins

Hi,

Student is working on a prototype for U6L16.
He is trying to randomly turn on the leds from different pins, but it’s buggy. Here is his code:
code
It will turn on two of the leds, but not the other two. I have tried putting the pin numbers in quotes and not, and using the pinmode blocks before it.

Any ideas? Happy to add more information.

Hello,

I am moving this post to the CS Discoveries forum. Unfortunately, I do not have a lot of experience with the discoveries U6 curriculum. I hope that someone else can answer you quickly!

@obrienm,

The code declares the four variables, but doesn’t really use them.

If the goal is to turn on randomly either pin 0, 2, 3 or 6, what if he were to declare a variable at the top of the program (something like “choices”) and set that to an array of the pins he wants to use.

Then a 2nd variable could be created (pins) that would randomly select one of the values from the array.

Then inside the BoardEvent, you could use the colorLeds[ ] block to turn on the randomly selected pin.

I think this would work and could be a more simple way to approach it.

Give it a shot and let us know if that works or not and we are happy to chime back in.

Mike

Sorry, I should have posted all the code. The created vars control stuff further down:


I’ll give your suggestion a try. Thanks!

Sounds good … if you are still having problems, it would be helpful if you were to click the share button and send us the link so we can test it and/or remix it if necessary to help troubleshoot!

Good luck!

Mike

Here is the share link: Code.org - App Lab

We tried making an array of the different pins, both in and out of quotes, and then calling them with led.on(). I can’t seem to get the pins to go on with the colorLeds block.

Ok … clarifying question, then …

Are you trying to turn on pins that are connected to external LED’s?

For some reason I assumed you were trying to turn on a random LED of the ones that are built in to the board.

Mike

Yes, trying to use the pins to turn on the external LEDs. We figured out how to randomize the ones on the board.

OK… Sorry. I didn’t completely understand. I don’t currently have any led lights to test it, but I am going to reach out to another teacher I know who has a little more experience with this than I do and see if I can get them to respond.

However, I do think you were on the right path with your commands in the original screenshot you sent in the first place.

I have a couple of thoughts right now, but in the absence of having any LED lights at my current location, I can’t test this myself right now …

I think you will need to set the pins to output using the pinMode statements.
You will then use the digitalWrite statement as you did before. Here’s a screenshot of that part of the code as I think it would (should?) work.

Then, assuming you have connected all 4 data pins to LED’s, I also think you will need to have all 4 of your LED’s connected to a Ground pin. I’m not sure what that would look like since I think there are only three ground pins. (See diagram at the link below). I can’t test it so I’m not sure if you can connect multiple LED’s to a single Ground pin, but maybe this is some food for thought?

I’ll ping the other teacher I know on this to see if they have some ideas.

This link to documentation may also help. It has the diagram of the circuit board as well as the code that should make the LED circuit work.

Mike

Hi, @obrienm ,

I was wondering about your physical set-up - a picture or diagram of the set-up might help. If they are polarized LEDs, it is pretty easy to get them wired in the wrong direction and short them out.

Best, Andrea

This worked. Thank you all for your help!

2 Likes