CODING:  ALARM

Now that we know how to use an LED and Ultrasonic Sensor let’s combine the two and make a warning system.
When something gets too close to TURL-X the LED flashes a warning.
Before we jump into the code let’s learn some more things about C++
We are starting to use many more pins on the Arduino and it is getting confusing in the code. So we need to learn about something called
VARIABLES.
We declare variables at the very beginning of the code. It is like creating a box and putting a label on it. We can use whatever we put in the box later in the code, all we need to do is use the label.
We put all the pin numbers at the very top of the code inside variables (like little named boxes) because:
The names are easier to understand than just numbers (trigPin makes more sense than 13)
If we ever want to change which pin we use, we only change one number at the top — not 20 places in the code.
  1. To declare a variable the first thing we do is say what kind of data type it is. We will use int which is short for integer – that’s a number that is positive, zero or negative but not a fraction.
  2. Secondly we give it a name (label), in our example we call it BLUE_LED.
  3. and lastly we give it a value, what we put in the box, here we use 1 which is the pin number 
 
Have a look at the code below and see if it makes sense to you.
➕➕
filename.cpp
int BLUE_LED = 1; 
int RED_LED = 2;
int GREEN_LED = 3; 


void setup() {
    pinMode(RED_LED, OUTPUT);
    pinMode(BLUE_LED, OUTPUT);
    pinMode(GREEN_LED, OUTPUT);
}


void loop() {
  digitalWrite(RED_LED, HIGH);  
  delay(1000);                 
  digitalWrite(BLUE_LED, HIGH);  
  delay(1000);
  digitalWrite(GREEN_LED, HIGH);           
         
}

Now let’s create that Warning System using variables in our code.

Copy and Upload the code below to TURL-X. Each line of code is explained in the code after the double forward slashes.
➕➕
filename.cpp
int trigPin = 13;    // create variable for Trigger pin on pin 13
int echoPin = 12;   // create variable for Echo pin on pin 12
int ledPin = 2;     // LED connected to pin 2


void setup() {
  Serial.begin(9600);         // Open Serial Monitor
  pinMode(trigPin, OUTPUT);   // Trig = output
  pinMode(echoPin, INPUT);    // Echo = input
  pinMode(ledPin, OUTPUT);    // LED pin = output
  digitalWrite(ledPin, LOW);  // Start with LED off
}

void loop() {
  // Send trigger pulse
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Measure echo time
  long duration = pulseIn(echoPin, HIGH);

  // Calculate distance in cm
  int distance = duration * 0.034 / 2;

  // Show distance on Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Turn LED on if closer than 30 cm
  if (distance < 30 && distance > 0) {  // > 0 avoids invalid readings
    digitalWrite(ledPin, HIGH);         // LED ON
    Serial.println("Too close! LED ON");
  } else {
    digitalWrite(ledPin, LOW);          // LED OFF
    Serial.println("All clear - LED OFF");
  }

  delay(300);  // Wait a little before next measurement
}
Experiment with the code by making your own changes. Try increasing or decreasing the activation range.

If you remember half of what we have covered so far and can recognise it in the code, then you are doing very well 🙂

Remember there is an issue with the new Arduino IDE. You may have problem uploading code the second time (for example, to make changes). If this happens save your work, close the IDE and unplug the TURL-X. Then restart the IDE and plug TURL-X back in. Sometimes you have to do this many times before it works. I do not know why but turning on TURL-X’s power, and not just having the USB power the Arduino helps.  

We use something called an H-Bridge to control DC Motors. Learn about that in the next chapter.. 

NEXT UP..