Wednesday, October 23, 2024

Final Week's Challenge: Blinking Lights with Zazzle

My goal for this final project was to get one light to be always on, two additional lights to alternate, and the fourth light to randomly come on and off.

The setup was straightforward.  Could be there was a better way to wire this, but I just repeated the same pattern four times.





Looking at the code, the first three LEDs were able to be programmed as expected, but in order to make the fourth LED alternate, I needed to store its current state (1 == HIGH) by declaring the variable outside both functions.

/*
Blinking Lights with Zazzle

Light 13 is always on
Lights 12 and 11 alternate
Light 10 turns on or off randomly
*/

// Declare this variable out here so that we can use it in both functions
int currentState = 1;

I ran into some trouble getting the random number generator to work so I ended up having to display the values to the serial monitor.  The code is now commented out, but I left it in for good measure.  I also put in a random seed so the pattern would be different every time.

void setup() {
  // initialize digital pins as an output
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);

  // Start the serial communication
  // Commenting this out - this was for troubleshooting
  // Serial.begin(9600);

  randomSeed(analogRead(A0)); // set the random seed to a pin we're not connected to

  digitalWrite(13,HIGH);  // set first red LED on permanently
  digitalWrite(10,currentState);  // set second red LED on temporarily
}

It turns out that the random() function in Arduino always generates integers, which was not what I wazs expecting.  As such, I told it to randomly generate a number from 1 to 100, and then for values greater than 50, to toggle the light.  The modulo arithmetic (with the percent sign) was an alternative so I didn't have to put in another if statement.  You can also see where I commented out the serial communication again.

void loop() {

  // declare randomNumber as integer
  int randNumber = random(1,100);

  // Print the random number to the serial monitor
  // Commenting this out - this was for troubleshooting
  // Serial.println(randNumber);

  if (randNumber > 50){
      currentState = (currentState + 1) % 2; // flips it on vs off
      digitalWrite(10,currentState);   // set second red LED on
  }

  digitalWrite(12,HIGH);  // set first yellow LED on
  digitalWrite(11,LOW);   // set second yellow LED off
  delay(500);            // wait half a second

  digitalWrite(12,LOW);  // set first yellow LED off
  digitalWrite(11,HIGH);   // set second yellow LED on
  delay(500);            // wait another half a second
}

All in all, I finally got everything working, and I'm happy with the result!



Covering My Bases: The Circuit and Coding Playing Challenges

Not that anyone is asking for this, but I'm documenting my discoveries for myself so I don't lose track of them!  Also this is a good review to make sure I know what I'm doing!

Circuit Play

  1. What happens if you turn the LED around (reverse the wiring)?
    It still flashes on the Arduino board, but the LED doesn’t anymore.

  2. What happens if you remove the positive lead from the breadboard? Does the circuit still work?
    Yes, it still works, not sure why!

  3. What happens if you place the resistor to the positive side of the LED and simply used a wire to run back from the LED to ground? When you do this, you will need to change up the wiring a little so check this closely to make sure you have not shorted out the circuit.
    I’m not sure what this means, but if it means can you put the resistor on the other side of the LED, the answer is yes!

  4. What happens if you move the wire from port 13 to port 12 on the Arduino?
    The light stops flashing because it’s not connected to the port that’s alternating anymore.

Code Play

  1. If you moved the wire from port 13 to port 12 on the Arduino, what do you need to change in the code?
    If you move the wire from port 13 to 12 without changing the code, it won’t work.  You need to change these lines.

digitalWrite(12,HIGH)

digitalWrite(12,LOW)

Now the light will flash again, but not nearly as brightly (which I’m not entirely sure why).  But I'll find out later!!! -- you also have to change it in the setup() function!

  1. What happens if you change the two delay code lines from delay(1000) to delay(2000)? Take out a stop watch or timer of some sort and time the rate of blinking for each of these settings. How many times does the LED blink in a minute for each of these settings? What have you learned about the value that is placed between the parenthesis after delay()? What value (parameter) would you place in delay() if you wanted the LED to blink at a rate of once every 3 seconds? How about every half second?
    The delay(1000) gives 30 blinks in one minute.
    The delay(2000) gives 15 blinks in one minute.
    The delay parameter is in 1 one-thousandth of a second, so if I wanted a blink every 3 seconds, I would need to use delay(3000), and if I wanted it for every half-second, I would need to use delay(500).

  2. What happens if you place // before the words void setup()?
    Trying to comment out the setup will not allow the code to compile.

  3. What happens if you place // before the words void loop()?
    Trying to comment out the primary loop will not allow the code to compile.

  4. What happens if you remove the last curly brace “}” in the program?
    Dropping any {}s will get you a compilation error.

  5. What happens if you place a // before pinMode(13,HIGH) in setup()?
    If you mean in the loop, then the pin will never turn on.  We don’t actually turn on the pinMode to HIGH in setup.  If you mean OUTPUT in the setup(), then the light ends up really low – this answers my previous question, because when I changed the pin to 12 from 13, I didn’t change it in the setup() – yay!

  6. What happens if you changed HIGH to high on the pinMode(13,HIGH) line?
    The code is case sensitive, so it won’t compile.

  7. What happens if you change the word pinMode to pinmode in pinMode(13,HIGH)?
    The code is case sensitive, so it won’t compile.

 

Practical Goal: Why is this working?

Not wanting to ever have to do anything by hand, I searched for hours for a way to avoid drawing a circuit diagram.  I came across all kinds of options, and was >>>this close<<< to finding out how to actually simulate an Arduino board in Matlab.  I finally gave up, though, head hung in shame.  I did, however, install all these add-ons, which somehow contributed in one way or another!

  • Simulink
  • Simscape
  • Simulink support package for Arduino hardware (I have high hopes for this one day!)
  • Simscape electrical

I did, however, manage to at least figure out how to draw circuits in Simscape, an add-on to Matlab.  (No, this should not have taken me the better part of a work day, but that's what we do...)

Between Google and reaching far back into memory, I finally was able to tell myself the story of the little LED that blinked:

There are three main components on our breadboard:

  1. The LED (Light Emitting Diode) has two legs: the anode (longer, positive leg) and cathode (shorter, negative leg).  The word diode means that current can only flow in one direction, so if you put it in “backwards” the current can’t travel through the circuit.  For current to travel through a diode, it has to go into the positive side and out the negative side.
  2. The wires move the current around the circuit.
  3. The resistor reduces the current so the LED doesn’t burn out.

The Arduino sends a signal to the LED (through the resistor so it doesn’t burn it out) via the pin #13.  That is, the Arduino turns “on” pin 13, and then it goes into the positive leg of the LED.  Because I’m a crazy person, I wanted the current to go through the resistor “first” so it gets stepped down before going into the LED.  Clearly this is entirely unnecessary because that’s not how circuits work, but it makes my brain happy. (In the manual example, the resistor came after the diode.  Also, I couldn't figure out how to change the text from 1 to 13.)

So in my imaginary world where this isn’t all happening instantaneously, the current goes through the resistor (so it gets stepped down) and then it goes into the positive leg of the LED.  This powers on the light.  The current continues to ground (which is basically the “zero reference point” for the circuit).  From ground, it goes back to the Arduino into the pin that is also marked GND for ground.


In our physical model, there is also a (yellow) wire going from the 5V pin from the Arduino to the breadboard, but it doesn't seem to have any impact on the functionality of the system, so I'm not entirely sure why it's there.

Tuesday, October 22, 2024

First Programming Goal - Side Quest - Programming Arduino Board in Matlab

I have attended one or two Professional Development sessions where I'd heard about how Matlab and Arduino can work together.  Our school never had the add-in though, so I never worried about it.  This year, however, there was a license change, and I thought, "Hmm.... I wonder...."

And there it was, the Arduino Add-in!

True to form, I installed it and just started clicking buttons to see what happened!  After installation, it seemed to want to talk to the Arduino, so I just plugged it in and let it.  Again, after many bad button clicks (which have all been edited out of this video) I managed to get what seemed like the "baseline app" open and some kind of libraries installed, although admittedly I have no idea what I just did.  But that's the spirit of making, right!  Adventure!

At some point, there were enough buttons that I got scared and needed a Skittles break.



Coming back, I realize that there are Pins labeled in the Matlab app, so I try putting HIGH under Pin 13.  Of course, it wants a number, so as a shot in the dark, I try 1.  Yay, no error!  Then I see a button that says "Generate Script."  That sounds promising, let's click that. 

I got a bunch of code and in a bit I realized I could run it from Matlab and it would send the code to the Arduino board.  I was able to turn the light on, but it wasn't blinking because I hadn't asked it to.  I put in a quick for loop so that it would blink 10 times, sent it to the board, and voila, it worked!

(The video doesn't show all the missteps or the actual blinking, but I swear it happened!)


Then finally I wanted to prove to myself I could do something a little different, so I did five double-blinks.  In this video you can see me click Run, and then see the board respond.

Here is the code:

for i = 1:5
writeDigitalPin(arduinoObj, "D13", 1)
pause(.25)
writeDigitalPin(arduinoObj, "D13", 0)
pause(.25)
writeDigitalPin(arduinoObj, "D13", 1)
pause(.25)
writeDigitalPin(arduinoObj, "D13", 0)
pause(1)
end

And here is the video:






First Programming Goal - Basic Blinking in Arduino IDE!

 

Had some fun with getting things set up, again, because apparently I'm only reading directions as a last resort!  Took awhile to realize I was supposed to take the plastic part off the back of the Arduino board before connecting it to the black backing.  Putting the wires in all the right places was fun with my 40+ eyes, until I realized if I turned the page, I'd see a better and more clear diagram!

I opened up the Arduino IDE and began writing in the code.  (The video is edited without all the tons and tons of mistakes, so it looks like I'm super-efficient!)


As you can see - it now blinks happily away!




Unboxing my new Arduino!

Super-excited to be getting started with the new Arduino for my Makerspace class!  Of course, step 1 was to immediately ignore all the directions and just begin taking everything out of its package!



Of course, my laptop only has USB-C ports so I had to go chase down a converter, but overall looking forward to this adventure!

Nooooo, don't make me reflect on my learning!

 ... I just want to stick wires in breadboards and make froggies dance!!! Okay, but seriously, that stupid dancing frog was clearly my most ...