OPEN IDEAS / PRACTICAL CODE

Code that moves.

A few small examples for exploring motors, sensors and decisions. Adapt them to your own hardware and test safely.

THE STARTING BLOCKS

Take an idea.
Make it yours.

Illustrative snippets, not instructions for a specific AlJazriLab product configuration.

01 / MOTORS

Drive a motor with PWM

A simple Arduino example of variable motor speed. Check your driver and power wiring before use.

// Example pins for a motor driver
const int enablePin = 9;
const int directionPin = 8;

void setup() {
  pinMode(enablePin, OUTPUT);
  pinMode(directionPin, OUTPUT);
}

void loop() {
  digitalWrite(directionPin, HIGH);
  analogWrite(enablePin, 160); // speed: 0–255
  delay(1000);
  analogWrite(enablePin, 0);
  delay(1000);
}
02 / SENSORS

Read an ultrasonic sensor

Measure the time of an echo pulse and convert it to an approximate distance in centimeters.

const int triggerPin = 7;
const int echoPin = 6;

void setup() {
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  long pulse = pulseIn(echoPin, HIGH, 30000);
  if (pulse > 0) {
    float cm = pulse * 0.0343 / 2;
    Serial.println(cm);
  }
  delay(150);
}
03 / LOGIC

React to distance

A small, hardware-agnostic Python example of turning sensor readings into a decision.

def choose_action(distance_cm):
    if distance_cm < 20:
        return "stop and turn"
    return "continue forward"

for distance in (42, 18, 30):
    print(distance, choose_action(distance))
04 / LOGIC

Keep a build log

Record test observations so you can compare changes across your build iterations.

tests = []

def record_test(change, result):
    tests.append({"change": change, "result": result})

record_test("lower motor speed", "turns more smoothly")
record_test("move sensor", "detects edges sooner")

for test in tests:
    print(test["change"], "→", test["result"])

Pin assignments and driver requirements vary. Verify your components and wiring before powering a build.

THE NEXT EXPERIMENT

What would you build next?

The code library will grow with the project. Tell us which topics you want to explore.

Register interest