Intermediate Logic and Algorithms: Smarter Code and Circuits
This lesson steps up from single actions to structured problem-solving. You will generate random numbers, write your first real algorithm with a bubble sort, and combine several sensors into one decision-making system. You will also meet interrupts, which let the board react instantly to an event no matter what else it is doing.
Random numbers are not truly random
Computers cannot invent true randomness on their own, so they generate pseudo-random numbers from a starting value called a seed. Give the same seed and you get the same sequence every time, which is useless for a game or a simulation. The trick on the board is to seed from noise: randomSeed(analogRead(A0)) reads the tiny electrical fluctuation on an unconnected analog pin, which differs slightly every time, giving a fresh seed on each run. After seeding, random(0, 101) returns a value from 0 to 100. Note the upper bound is exclusive, so 101 means the highest possible result is 100.
What an algorithm is
An algorithm is a set of step-by-step instructions to solve a problem, but the deeper idea is efficiency: how few steps can you solve it in. Finding the largest number in a list is a simple algorithm, assume the first is largest, then compare each remaining value and keep the biggest. Sorting a list is a richer problem, and there are many sorting algorithms, each better or worse depending on the situation. Studying algorithms is an entire field, because the same result can be reached in wildly different amounts of work.
Bubble sort
Bubble sort is the classic teaching algorithm for putting a list in order. It walks the list comparing each pair of neighbors and swapping them if they are out of order, so the largest value bubbles to the end on each pass. It uses a loop inside a loop: the outer loop runs once per pass, and the inner loop does the neighbor comparisons. Swapping two values needs a temporary variable to hold one while you overwrite it. It is not the fastest sort, but it is the clearest way to see how a sorting algorithm actually works.
Combining sensors for a decision
Real systems rarely act on one input. By reading a temperature sensor and a light sensor together, you can make a decision that depends on both: if it is hot and dark, do one thing; if it is cool and bright, do another; otherwise fall back to a default. A mid-scale threshold around 512 splits each 0 to 1023 reading into low and high. This is where conditionals, logical operators, and analog reads all come together. Note that true analog input pins like A0 and A1 do not need a pinMode line, unlike digital pins.
Interrupts: reacting instantly
Normally the board runs loop top to bottom, so it only notices a button when the code happens to check it. An interrupt changes that: it lets the board drop whatever it is doing the instant an event occurs, run a small handler function, then return to exactly where it left off. You attach one with attachInterrupt, giving it the pin, the handler function to run, and whether to trigger on a rising or falling edge. Because the handler can fire at any moment, any variable it changes is marked volatile, which tells the compiler the value can change unexpectedly. Interrupts are ideal for catching button presses or time-critical events reliably.
Working through it
Seed and print random numbers. In setup call randomSeed(analogRead(A0)) so each run differs. In loop, print random(0, 101) once per second and watch the Serial Monitor produce values from 0 to 100.
Sort an array with bubble sort. Start with an unsorted array like {5, 3, 8, 6, 2}, call a bubbleSort function that swaps out-of-order neighbors across repeated passes, then print the array to confirm it comes out in order.
Wire the multi-sensor system. Connect a temperature sensor to A0, a photoresistor with a voltage divider to A1, and an RGB LED to pins 9, 10, and 11. The two analog pins read continuously; the RGB pins are outputs.
Drive the RGB LED from combined logic. Read both sensors, then decide: if the temperature reading is above 512 and the light reading is below 512 (hot and dark), show red; if cool and bright, show green; otherwise show blue. This turns two readings into one clear behavior.
Build the interrupt-driven buzzer. Wire a button to pin 2 with a 10k pull-down resistor and a buzzer to pin 9. Attach an interrupt on the button that sets a volatile flag; in loop, when the flag is set, sound the buzzer and clear the flag. The button is caught the instant it is pressed.