Putting Codes Together

In this section, we will explore different ways to mix and match sensor and actuator codes to make a functioning program.

Contact + LED

link: https://makecode.com/_YtWfxLTdL1Fp

This code uses a contact sensor to turn on an LED. The code has three simple parts: setting the sensor, sensing the sensor, and displaying the output through an actuator.

  1. Mentioned previously in the contact sensor section, this first portion is setting up the contact sensor.
  2. Here, you are seeing if an input was made with an if-statement.
  3. If the statement is true, or contact was sensed, you turn on the LED by inserting a digital write for the pin where the LED is connected. You can refer to the LED actuator page for more details.
  4. Lastly, if the statement is not true, the pin in which the LED is connected is set to LOW.

Potentiometer + Motor

link: https://makecode.com/_DyP1sbhecYxX

This code uses a potentiometer as an input and the motor as an output. Once the potentiometer goes above a certain value, the motor will turn on.

  1. Here, we are checking if the potentiometer value, connected to the A6 pin is greater than equal to 400. If this is true, the digital write pin to A1, the motor, will turn on
  2. The else-statement is if the potentiometer value is less than 400. If this is the case, the digital write block will set to low and stop the motor.

Capacitive Touch + SPEAKER

link: https://makecode.com/_4r0goyVDCEmU

This code will allow you to use the capacitive touch to change the sound coming out of the speaker. You can revisit the sensor code page to see the capacitive touch code and the actuator code page for the speaker.

  1. On start, you will set the speaker volume to be at 255, or max, and the threshold of the capacitive touch sensor.
  2. If the capacitive sensor at A6 is sensed, then a jump up sound will play.
  3. If the capacitive sensor at A7 is sensed, then a jump down sound will play.
  4. If neither of the above two conditions satisfy, the speaker will stop all sounds.

Accessing separated code

Below, you can find both sensor and actuator codes in blocks separated from the combined code. You can reference these blocks to create your own code.

link: https://makecode.com/_3b13hA8JfAAR