Education

Education

Education

Arduino

Robo Explorers

Let’s look into the main complete code programmed into our Blue Elixer Board:

Check out the video below for complete understanding of the code!

Here the detailed code:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <dht11.h>
#include <Servo.h>

dht11 DHT;
Servo myservo;

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define DHT11_PIN 11
#define OLED_RESET -1
#define trigPin 10
#define echoPin 12
#define ledPin1 4
#define ledPin2 5
#define ledPin3 6
#define ledPin4 7
#define ledPin5 8

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int pos =0;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  myservo.attach(9);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }

  display.clearDisplay();
  display.setTextSize(5);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println("LEAP");
  display.display();
  delay(2000);
  

}

void loop() {


  int chk = DHT.read(DHT11_PIN);

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);

  display.println("TEMPERATURE IS");
  display.println(DHT.temperature);
  display.println("HUMIDITY IS");
  display.println(DHT.humidity);
  display.display();

  long duration, distance;
  // Clear the trigger pin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  // Set the trigger pin high for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Measure the duration of the pulse from the echo pin
  duration = pulseIn(echoPin, HIGH);
  
  // Calculate distance in centimeters
  distance= duration * 0.034 / 2; // Speed of sound is 0.034 cm/µs
  
  // Print the distance to the serial monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  if (distance <10){
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, HIGH);
    digitalWrite(ledPin5, HIGH);
  }

  if (distance >10 && distance<40 ){
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin5, LOW);
  }

  if (distance >40){
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin5, LOW);
  }
  
  myservo.write(180);
  delay(1000); // Delay between measurements
  myservo.write(90);
  delay(1000);

}

In this board, our Arduino microcontroller is connected to 5 different components. These are:

Ultrasonic sensor

Temperature sensor

OLED screen

LEDs

Servo motor

Let’s dive into how arduino is communicating and controlling these components!

Learn from a Professional

Book a Class

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+919560372931

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+919560372931

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us