FDP Arduino Programs
FDP Arduino Programs
Code:
// Variable and pins declared and initialized
void setup()
// This loop repeats only once
{
// set up all the LEDs as OUTPUT
pinMode(led_red, OUTPUT);
pinMode(led_yellow, OUTPUT);
pinMode(led_green, OUTPUT);
void loop()
// This loop repeats continuously
{
// turn the green LED on and the other LEDs off
digitalWrite(led_red, LOW);
digitalWrite(led_yellow, LOW);
digitalWrite(led_green, HIGH);
Serial.println("Green light is ON");
delay(2000); // wait 2 seconds
// turn the yellow LED on and the other LEDs off
digitalWrite(led_red, LOW);
digitalWrite(led_yellow, HIGH);
digitalWrite(led_green, LOW);
Serial.println("Yellow light is ON");
delay(1000); // wait 1 second
Link- Problem_Statements_1
Problem Statement - 2
(Measure the distance of an object/wall using the ultrasonic sensor)
Code:
// Pin
const int trigPin = 4; // Trigger pin of ultrasonic sensor
const int echoPin = 5; // Echo pin of ultrasonic sensor
void setup() {
// Start serial communication for monitoring
Serial.begin(9600);
void loop() {
// Clear the trigger pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
Link- Problem_statements_2
Problem Statement - 3
(Control the DC motor using the Ultrasonic sensor data (using Digital PIN))
Note: For real-time, we have to use an H-bridge motor driver L298N to control the motor.
Code:
// pin numbers
const int triggerPin = 2; // Trigger pin for ultrasonic sensor
const int echoPin = 4; // Echo pin for ultrasonic sensor
const int motorPin = 12; // Pin to control the DC motor
void setup()
{
// pin modes
pinMode(motorPin, OUTPUT); // Set motor control pin as output
pinMode(triggerPin, OUTPUT); // Set trigger pin as output
pinMode(echoPin, INPUT); // Set echo pin as input
void loop()
{
long duration; // Duration of the pulse from the sensor
long distance; // Calculated distance in cm
delay(500); // Delay
}
Link- Problem_Statements_3
Problem Statement - 4
(Control the DC motor using the Ultrasonic sensor data (using PWM PIN))
Code:
// pin numbers
const int triggerPin = 2; // Trigger pin for ultrasonic sensor
const int echoPin = 4; // Echo pin for ultrasonic sensor
const int motorPin = 9; // PWM pin to control the DC motor speed
void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop()
{
long duration;
long distance;
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm | Motor Speed: ");
Serial.println(motorSpeed);
delay(500);
}
Link- Problem_statements_4