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:
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.
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!
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.
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!
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… 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.
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.