diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index d79b1c06..f344f9c9 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -19,6 +19,10 @@ * [Guitar](/projects/guitar) * [Duct tape wallet](/projects/wallet) * [Watch](/projects/watch) + * [Soil Moisture](/projects/soil-moisture) + * [Voting Machine](/projects/voting-machine) + * [Infection](/projects/infection) + * [Fireflies](/projects/fireflies) * [Inchworm](/projects/inchworm) * [Milk Carton Robot](/projects/milk-carton-robot) * [Milk monster](/projects/milky-monster) diff --git a/docs/examples/plot-analog-pin.md b/docs/examples/plot-analog-pin.md index c9bc725a..57446093 100644 --- a/docs/examples/plot-analog-pin.md +++ b/docs/examples/plot-analog-pin.md @@ -1,10 +1,18 @@ # Plot Analog Pin +Use this program to graph the analog value on pin ``P0``, ``P1`` or ``P2``. +Press ``A`` to scroll the value on the screen. + ```blocks +let reading = 0 basic.forever(() => { + reading = pins.analogReadPin(AnalogPin.P0) led.plotBarGraph( - pins.analogReadPin(AnalogPin.P0), + reading, 1023 ) + if (input.buttonIsPressed(Button.A)) { + basic.showNumber(reading) + } }) ``` diff --git a/docs/examples/servo-calibrator.md b/docs/examples/servo-calibrator.md index 30ffe322..8b3a5acb 100644 --- a/docs/examples/servo-calibrator.md +++ b/docs/examples/servo-calibrator.md @@ -1,14 +1,21 @@ # Servo calibrator +Use this program to calibrate the angles of a servo. +Press ``A`` to reduce the angle by 5 and ``B`` to +increase it by 5. + +The current angle is displayed on the screen +in a loop. + ```blocks let angle = 90 input.onButtonPressed(Button.A, () => { - angle -= 5 + angle -= Math.max(0, 5) pins.servoWritePin(AnalogPin.P0, angle) led.stopAnimation() }) input.onButtonPressed(Button.B, () => { - angle += 5 + angle += Math.min(180, 5) pins.servoWritePin(AnalogPin.P0, angle) led.stopAnimation() }) @@ -16,5 +23,4 @@ basic.forever(() => { basic.showNumber(angle) }) pins.servoWritePin(AnalogPin.P0, angle) -basic.showString("Press A or B to change servo angle") ``` diff --git a/docs/projects.md b/docs/projects.md index d81a7549..5e7bd0f6 100644 --- a/docs/projects.md +++ b/docs/projects.md @@ -100,14 +100,24 @@ Fun games to build with your @boardname@. }] ``` -## More +## STEM ```codecard [{ "name": "Timing gates", "url":"/projects/timing-gates", "imageUrl":"/static/mb/projects/timing-gates.jpg" -}, { +},{ + "name": "Soil Moisture", + "url":"/projects/soil-moisture", + "imageUrl":"/static/mb/projects/soil-moisture.jpg" +}] +``` + +## More + +```codecard +[{ "name": "Compass", "url":"/projects/compass", "imageUrl":"/static/mb/projects/a5-compass.png" @@ -125,5 +135,5 @@ Fun games to build with your @boardname@. ### See Also [Flashing Heart](/projects/flashing-heart), [Smiley Buttons](/projects/smiley-buttons), [Love Meter](/projects/love-meter), [Rock Paper Scissors](/projects/rock-paper-scissors), [Compass](/projects/compass), [Hack your headphones](/projects/hack-your-headphones), [Banana keyboard](/projects/banana-keyboard), [Telegraph](/projects/telegraph), [Guitar](/projects/guitar), [Wallet](/projects/wallet), [Watch](/projects/watch), -[Milk Monster](/projects/milky-monster), [Karel the LED](/projects/karel), [Infection](/projects/infection), -[Fireflies](/projects/fireflies) +[Milk Monster](/projects/milky-monster), [Karel the LED](/projects/karel), [Infection](/projects/infection), [Voting Machine](/projects/voting-machine) +[Fireflies](/projects/fireflies), [Soil Moisture](/projects/soil-moisture) diff --git a/docs/projects/infection.md b/docs/projects/infection.md index 7231f483..b4ed0e2a 100644 --- a/docs/projects/infection.md +++ b/docs/projects/infection.md @@ -390,4 +390,8 @@ basic.forever(() => { basic.showIcon(GameIcons.Pairing) +``` + +```package +radio ``` \ No newline at end of file diff --git a/docs/projects/soil-moisture.md b/docs/projects/soil-moisture.md new file mode 100644 index 00000000..854dbcdc --- /dev/null +++ b/docs/projects/soil-moisture.md @@ -0,0 +1,28 @@ +# Soil Moisture + + +### ~avatar avatar + +Make a funny milky-monster robot! + +### ~ + +![](/static/mb/projects/soil-moisture/soil-moisture.jpg) + +## Materials + +* 1 @boardname@ with battery pack and batteries +* 2 long nails or silver +* 2 crocodile clips + +## Activities + +* [Make](/projects/soil-moisture/make) +* [Code](/projects/soil-moisture/code) +* [Connect](/projects/soil-moisture/connect) + +### ~button /projects/soil-moisture/make + +Let's get started! + +### ~ diff --git a/docs/projects/soil-moisture/code.md b/docs/projects/soil-moisture/code.md new file mode 100644 index 00000000..54f36eeb --- /dev/null +++ b/docs/projects/soil-moisture/code.md @@ -0,0 +1,96 @@ +# Code + +You will need to get a pot of dry dirt and wet dirt for the coding. + +## Step 1: Measuring moisture + +![](/static/mb/projects/soil-moisture/nailsv3.jpg) + +The soil acts as a variable resistor based on the amount of water and nutrient. +The water is not conductive but the nutrient are, so more water, more nutrient, so the soil will be less resistive. + +To measure this, we read the voltage on pin ``P0`` using [analog read pin](/reference/pins/analog-read-pin) +which returns a value between ``0`` (no current) and ``1023`` (max current). The value is graph on the screen +using [led plot bar graph](/reference/led/plot-bar-graph). + +```blocks +basic.forever(() => { + led.plotBarGraph( + pins.analogReadPin(AnalogPin.P0), + 1023 + ) +}) +``` + +### Experiment! + +* insert the nails in the dry dirt and you should see most LEDs off +* insert the nail in the wet dirt and you should see most LEDs on + +## Step 2: Sensor data values + +In the previous program, we only have a rough idea of the value of the sensor... since it is using a tiny screen +to display it! Let's add code that displays the current reading when button ``A`` is pressed. + +This code has to be inserted in the forever loop. We've also added a variable ``reading`` to store the reading value. + +```blocks +let reading = 0 +basic.forever(() => { + reading = pins.analogReadPin(AnalogPin.P0) + led.plotBarGraph( + reading, + 1023 + ) + if (input.buttonIsPressed(Button.A)) { + basic.showNumber(reading) + } +}) +``` + +### Experiment! + +* insert the nails in the dry dirt, press ``A`` and note the value. You should see a value close to ``250`` +for dry dirt. +* insert the nails in the wet dirt, press ``B`` and note the value. You should see a value close to ``1000`` +for dry dirt. + +## Step 3: Don't waste energy! + +![](/static/mb/projects/soil-moisture/nailsp1.jpg) + +We want our probe to work for a long time on batteries so we need to tweak our code to save energy. + +* Our circuit connects directly to the ``3v`` pin so it is always using electricity. Instead, +we will connect it to ``P1`` and turn that pin high only while the measurement is taken. +This saves electricty and also avoids corrosion of the probes. +* We will also lower the brightness of the screen to lower the energy consumption from the LEDs. +* Soil moisture changes very slowly so we don't need to measure all the times!!! Let's add a **sleep** of 5 seconds in the loop as well. + +```blocks +led.setBrightness(64) +let reading = 0 +basic.forever(() => { + pins.analogWritePin(AnalogPin.P1, 1023) + reading = pins.analogReadPin(AnalogPin.P0) + pins.analogWritePin(AnalogPin.P1, 0) + led.plotBarGraph( + reading, + 1023 + ) + if (input.buttonIsPressed(Button.A)) { + basic.showNumber(reading) + } + basic.pause(5000); +}) +``` + +### Experiment! + +* using the dry and wet dirt pots, test that your ciruit still works. Remember you'll have to wait up to 10 seconds to see a change! + +### ~button /projects/soil-moisture/connect + +Connect + +### ~ \ No newline at end of file diff --git a/docs/projects/soil-moisture/connect.md b/docs/projects/soil-moisture/connect.md new file mode 100644 index 00000000..7355af00 --- /dev/null +++ b/docs/projects/soil-moisture/connect.md @@ -0,0 +1,49 @@ +# Connect + +We are going to use radio to send the current moisture +level to a dashboard @boardname@. The dashboard +will display one LED per @boardname@. + +## Moisture sensor + +To make it happen, we need to change the program to +* setup the radio by choosing group 4 and sending the serial number of the device +* send the moisture level **divided by 4** +as the dashboard takes values between ``0`` and ``255``. + +```blocks +radio.setTransmitSerialNumber(true) +radio.setGroup(4) +led.setBrightness(64) +let reading = 0 +basic.forever(() => { + pins.analogWritePin(AnalogPin.P1, 1023) + reading = pins.analogReadPin(AnalogPin.P0) + radio.sendNumber(reading / 4); + pins.analogWritePin(AnalogPin.P1, 0) + led.plotBarGraph( + reading, + 1023 + ) + if (input.buttonIsPressed(Button.A)) { + basic.showNumber(reading) + } + basic.pause(5000); +}) +``` + +## The dashboard + +The dashboard code can be found at [/examples/radio-dashboard](/examples/radio-dashboard). + +Download the code from that example into the @boardname@ that will be used to display the result. + +When the dashboard receives a message from a @boardname@, it find a pixel for that board (and remembers it) +and uses the number received as the brightness of the LED. + +When a message hasn't been received by a board for some time, it's pixel will start to blink. After more time, it will simply turn off. + + +```package +radio +``` \ No newline at end of file diff --git a/docs/projects/soil-moisture/make.md b/docs/projects/soil-moisture/make.md new file mode 100644 index 00000000..ea5929bf --- /dev/null +++ b/docs/projects/soil-moisture/make.md @@ -0,0 +1,14 @@ +# Make + +https://youtu.be/S8NppVT_paw + +* Connect a nail to the ``GND`` pin with a croc clip and insert it in the soil +* Connect the other nail to the ``P0`` pin with a croc clip and insert it in the soil + +That's it! + +### ~button /projects/soil-moisture/code + +Code + +### ~ \ No newline at end of file diff --git a/docs/static/mb/projects/soil-moisture.jpg b/docs/static/mb/projects/soil-moisture.jpg new file mode 100644 index 00000000..fcfa276f Binary files /dev/null and b/docs/static/mb/projects/soil-moisture.jpg differ diff --git a/docs/static/mb/projects/soil-moisture/nailsp1.jpg b/docs/static/mb/projects/soil-moisture/nailsp1.jpg new file mode 100644 index 00000000..bb0ece9b Binary files /dev/null and b/docs/static/mb/projects/soil-moisture/nailsp1.jpg differ diff --git a/docs/static/mb/projects/soil-moisture/nailsv3.jpg b/docs/static/mb/projects/soil-moisture/nailsv3.jpg new file mode 100644 index 00000000..3a0e3634 Binary files /dev/null and b/docs/static/mb/projects/soil-moisture/nailsv3.jpg differ diff --git a/docs/static/mb/projects/soil-moisture/soil-moisture.jpg b/docs/static/mb/projects/soil-moisture/soil-moisture.jpg new file mode 100644 index 00000000..3fbfadd2 Binary files /dev/null and b/docs/static/mb/projects/soil-moisture/soil-moisture.jpg differ