0% found this document useful (0 votes)
26 views10 pages

Robotics

The document contains various Arduino code snippets for projects involving blinking LEDs, pushbuttons, temperature sensors, photoresistors, piezoelectric buzzers, and ultrasonic distance sensors. Each section provides setup and loop functions demonstrating how to interact with hardware components and read sensor values. The code examples illustrate basic control structures and functionalities for creating interactive electronic projects.

Uploaded by

22230356
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views10 pages

Robotics

The document contains various Arduino code snippets for projects involving blinking LEDs, pushbuttons, temperature sensors, photoresistors, piezoelectric buzzers, and ultrasonic distance sensors. Each section provides setup and loop functions demonstrating how to interact with hardware components and read sensor values. The code examples illustrate basic control structures and functionalities for creating interactive electronic projects.

Uploaded by

22230356
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Blinking LED Wave Project:

int d = 100;
void setup()
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop()
{
for ( int a = 2; a < 7 ;
a++ ) {
digitalWrite(a, HIGH);
delay(d);
digitalWrite(a, LOW);
delay(d);
}
}

Schematic Diagrams and the Pushbutton:


int buttonState = 0;

void setup()
{
pinMode(2, INPUT);
pinMode(13, OUTPUT);
}

void loop()
{
// read the state of the pushbutton
buttonState = digitalRead(2);
// check if pushbutton is pressed.
if it is, the
// button state is HIGH
if (buttonState == HIGH) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
delay(10);
}
Pull-Up Resistor & The Serial Monitor:
void setup()
{
pinMode(8, OUTPUT);
pinMode(2, INPUT_PULLUP);
Serial.begin(9600);
}

void loop(){
int reading =
digitalRead(2);
if(reading == HIGH){
digitalWrite(8,HIGH);
}else{
digitalWrite(8,LOW);
Serial.println("Button
Pressed");
}
delay(100);
}

Temperature Sensor & Boolean Variables:


int baselineTemp = 0;
int celsius = 0;
int fahrenheit = 0;

void setup() {
pinMode(A0, INPUT);
Serial.begin(9600);

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}

void loop() {
// set threshold temperature to activate LEDs one after the other
baselineTemp = 20;

// measure temperature in Celsius


int val = analogRead(A0);

// map(returned sensorValue, sensorMin(returned by analogRead()), sensorMax(returned by


analogRead()), min temp. sensor value, max temp. sensor value)
celsius = map((val-20)*3.04, 0, 1023, -40, 125);

// convert to Fahrenheit
fahrenheit = (((celsius * 9) / 5) + 32);

Serial.print(celsius);
Serial.print(" C, ");
Serial.print(fahrenheit);
Serial.println(" F");

if (celsius < baselineTemp) {


digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
if (celsius >= baselineTemp && celsius < baselineTemp + 10) {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}

if (celsius >= baselineTemp + 10 && celsius < baselineTemp + 20) {


digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}
if (celsius >= baselineTemp + 20) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(1000);
}
The Photoresistor:
int sensorValue = 0;
void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
pinMode(10, OUTPUT);
}

void loop(){
//read the value from the sensor (from 0 to 1024)
sensorValue = analogRead(A0);
// print the sensor reading so you know its range
Serial.println(sensorValue);
// map the sensor reading to a range for the LED
// analogWrite(): setting the brightness of the LED
analogWrite(9, map(sensorValue, 0, 1023, 0, 255));
delay(100); // Wait for 100 millisecond(s)
}

Exercise on Photoresistors & Pushbutton:


int photoresistorPinA = A0;
int photoresistorPinB = A1;
int buttonPin = 2;
int greenLedPin = 12;
int blueLedPin = 11;
int redLedPin = 13;

bool systemActive = false;

void setup() {
pinMode(buttonPin, INPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(blueLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
if(digitalRead(buttonPin) == HIGH){
delay(50);
systemActive = !systemActive;
}
if(systemActive){
digitalWrite(greenLedPin, HIGH);
Serial.print("Photoresistor A: ");
Serial.println(analogRead(A0));
Serial.print("Photoresistor B: ");
Serial.println(analogRead(A1));

int blueBrightness = map(analogRead(A0), 6, 679, 255, 0);


analogWrite(blueLedPin, blueBrightness);

if(analogRead(A0)>200 && analogRead(A1)>200){


digitalWrite(redLedPin, HIGH);
}
}
else{
digitalWrite(greenLedPin, LOW);
digitalWrite(blueLedPin, LOW);
digitalWrite(redLedPin, LOW);
}
delay(100);
}
The Piezoelectric Buzzer:
int piezo = 3, button = 12, count=0, button2=5;
int melody[] = {262, 196, 196, 220, 196, 0, 247,
262};

void setup(){
Serial.begin(9600);
pinMode (piezo, OUTPUT);
pinMode (button, INPUT);
pinMode (button2, INPUT);
}

void loop(){
if(digitalRead(button2)){
count++;
Serial.println(count);
delay(100);
}
if((digitalRead(button)) && (count%2 ==1)){
tone(piezo, melody[3], 250);
}
}

Reading From The Serial Monitor:


/*
When the arduino starts, the piezo plays a sound for one time, the serial monitor
displays "Enter a number" then keeps displaying a dot until the user enter a
number N, the arduino uses N to set the brightness of the LED.
Each time you push the button the brightness of the LED should be increased by a value
of + 50. If the increment will make the brightness > 255 then set it to 0.
*/

#define LED 5
#define BUTTON 7
int N;

void setup() {
Serial.begin(9600);
tone(3, 330, 100);
Serial.println("Enter a number: ");

while (Serial.available() == 0) {
Serial.println(".");
delay(100);
}
}
void loop() {
if (Serial.available() > 0) {
N = Serial.parseInt();
Serial.println(N);
analogWrite(LED, N);

}
if (digitalRead(BUTTON) == HIGH ) {
N += 50;
if (N <= 255)
analogWrite(LED, N);
else {
analogWrite(LED, 0);
N = 0;
}
}
delay(100);
}

Ultrasonic Distance Sensor :


int cm = 0, led = 3, piezo = 4;

void setup(){
Serial.begin(9600);
pinMode(piezo, OUTPUT);
pinMode(led, OUTPUT);
}

void loop(){

cm = 0.0343 * readUltrasonicDistance(7, 6) / 2;
Serial.print(cm);
Serial.println(" cm");

if(cm < 20){


analogWrite(led, 255);
tone(4, 1000, 100);
delay(75);
}else if(cm < 40){
analogWrite(led, 200);
tone(4, 1000, 100);
delay(125);
}else if(cm < 60){
analogWrite(led, 150);
tone(4, 1000, 100);
delay(250);
}else if(cm < 80){
analogWrite(led, 100);
tone(4, 1000, 100);
delay(500);
}else if(cm < 100){
analogWrite(led, 50);
tone(4, 1000, 100);
delay(1000);
}else {
digitalWrite(led,0);
}

//delay(100);
}

long readUltrasonicDistance(int triggerPin, int echoPin){


pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
//Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
Servo Motor Slides:

#include <Servo.h>
Servo myservo;

void setup(){
myservo.attach(8);
pinMode(3, OUTPUT);
Serial.begin(9600);
myservo.write(0);
Serial.println("Enter a number between 0 & 180: ");
}

void loop(){
if(Serial.available()>0){
int num = Serial.parseInt();
Serial.println(num);

if(num >= 0 && num <= 180){


myservo.write(num);
}
else{
tone(3, 1000, 200);

}
}
}
Servo, Buzzer, & LED:

#include <Servo.h>
Servo servo;

void setup(){
Serial.begin(9600);
servo.attach(8);
pinMode(7,OUTPUT);
servo.write(0);
}

void loop(){
if(Serial.available()>0){
String command = Serial.readString();
if(command == "Led ON"){
digitalWrite(7, HIGH);
}
else if(command == "Led OFF"){
digitalWrite(7, LOW);
}
else if(command == "piezo"){
for(int i=0; i<3; i++){
tone(3, 100);
delay(100);
noTone(3);
delay(100);
}
}
else if(command == "rotate"){
servo.write(180);
delay(1000);
servo.write(0);
delay(1000);
}
else{
Serial.println("Invalid Command! Try Again!");
}
}
}

You might also like