Controls Reviewer 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Prop. of: VALDEZ, JOHN MER S.

Controls Note

Int = integer (declaration of variable / to limit the input (no decimal just whole number))

Ex. “int redLED = 11;” – means the redLED is a variable that represents no. 11

“int duration;” variable for the duration

long = Same concept with integer but has no limitation for the integer, e.g. 696,969,696

Ex. “long duration;” – variable for the duration of the sound wave travel (used in Act 5)

float = same concept with integer but has 2 decimal in it.

Ex. “float distance;” variable for measured distance (expected output Y.xx, where Y is whole number and

xx is decimal)

#define = declaration of variable.

Ex. “#define echoPin 10” – echoPin is a variable for 10 Note: doesn’t need to have semicolon

pinMode = used to declare the pin location from 0-13 from Arduino and establishing if its for Input or
Output

Ex. “pinMode(echoPin, Input);” means the echopin which is located in port 10 is expected to be the
Input data from the ultrasonic sensor. (refer to the example above). The proper format for this is
//”pinMode(port no., Input/Output);

Serial.begin(bps) = usually in 9600 bps, this is written in this format, “Serial.begin(9600);”

Serial.print & Serial.println = the difference of the two is that the Serial.print prints only the number or
string, while Serial.println prints it with a newline character

Ex.
digitalWrite = this determines what port will be turned on or turned off, turn on = HIGH, turn off = LOW

Ex. “digitalWrite(echoPin, LOW);” the format for this statement is “digitalWrite(port no., HIGH/LOW);”

delay(time) = the unit of the delay is in ms or milliseconds, //1000milliseconds = 1second, usually used
before and after the “digitalWrite” command

Serial.available() = usually used for the human input in the Serial Monitor

Serial.parseInt() = usually used after you used the Serial.available, this will print the valid integer
inputed by human in the Serial Monitor
Below are some of my Code;

//From Activity 6 (not yet finished and possibly not working) //activity that combines ultrasonic sensor
and servo motor

#include <Servo.h>

const int TRIG_PIN = 8;


const int ECHO_PIN = 9; Servo servo;
const int SERVO_PIN = 11; float duration_us, distance_cm;
int Angle1 =10;
int Angle2 =20; void setup() {
int Angle3 =30; Serial.begin (9600);
int Angle4 =40; pinMode(TRIG_PIN, OUTPUT);
int Angle5 =50; pinMode(ECHO_PIN, INPUT);
int Angle6 =60; servo.attach(SERVO_PIN);
int Angle7 =70; servo.write(0);
int Angle8 =80; }
int Angle9 =90;
int Angle10 =100; void loop() {
int Angle11 =110; digitalWrite(TRIG_PIN, HIGH);
int Angle12 =120; delayMicroseconds(10);
int Angle13 =130; digitalWrite(TRIG_PIN, LOW);
int Angle14 =140; duration_us = pulseIn(ECHO_PIN, HIGH);
int Angle15 =150; distance_cm = 0.017 * duration_us;
int Angle16 =160;
int Angle17 =170; if(distance_cm <= Angle1)
int Angle18 =180; servo.write(10);
else if(Angle1 < distance_cm <= Angle2)
servo.write(20);
else if(Angle2 < distance_cm <= Angle3)
servo.write(30);
else if(Angle3 < distance_cm <= Angle4) else if(Angle13 <distance_cm <= Angle14)
servo.write(40); servo.write(140);
else if(Angle4 < distance_cm <= Angle5) else if(Angle14 < distance_cm <= Angle15)
servo.write(50); servo.write(150);
else if(Angle5 < distance_cm <= Angle6) else if(Angle15 < distance_cm <= Angle16)
servo.write(60); servo.write(160);
else if(Angle6 < distance_cm <= Angle7) else if(Angle16 < distance_cm <= Angle17)
servo.write(70); servo.write(170);
else if(Angle7 < distance_cm <= Angle8) else if(Angle17 < distance_cm <= Angle18)
servo.write(80); servo.write(180);
else if(Angle8 < distance_cm <= Angle9) else
servo.write(90); servo.write(0);
else if(Angle9 < distance_cm <= Angle10)
servo.write(100);
else if(Angle10 < distance_cm <= Angle11) Serial.print("distance: ");
servo.write(110); Serial.print(distance_cm);
else if(Angle11 < distance_cm <= Angle12) Serial.println(" cm");
servo.write(120); delay(500);
else if(Angle12 < distance_cm <= Angle13) }
servo.write(130);

//Code from Sir Doroliat (servo motor only guided by User Input thru Serial Monitor)

#include <Servo.h> }

Int servoPin = 10;

Int servoPos; void loop() {

Servo myFirstServo; Serial.print (“What angle do you want?: “);

while (Serial.available () ==0 {}

void setup() { servoPos=Serial.parseInt();

Serial.begin(6900); myFirstServo.write(servoPos);
myFirstServo.attach(servoPin);
}

//My Code from Activity 5

int echoPin = 10;//int can be replace by #define


int trigPin = 11; //int can be replace by #define digitalWrite(trigPin, LOW);
delayMicroseconds(2);
long duration; digitalWrite(trigPin, HIGH);
float distance; delayMicroseconds(2);
digitalWrite(trigPin, LOW);
void setup() {
pinMode(trigPin, INPUT); duration = pulseIn(echoPin, HIGH);
pinMode(echoPin, OUTPUT); distance = duration * 0.0343 / 2;
Serial.begin(9600);
Serial.println("Ultrasonic Sensor HC-SR04 Serial.print("The measured distance is: ");
Test");
Serial.print(distance);
Serial.println("with Arduino UNO R3");
Serial.println(" in cm");
}
delay(500);
void loop() {
}

// My Code from Activity 4

int redLED = 11; Serial.begin(9600);


int greenLED = 10; pinMode(redLED,OUTPUT);
int timeON = 300; pinMode(greenLED,OUTPUT);
int timeOFF = 1000; }
int redBlinks = 20;
int greenBlinks = 20; void loop() {
// put your main code here, to run repeatedly:
void setup() {
// put your setup code here, to run once: Serial.println("what ");
Serial.println("is ");
Serial.println("your "); greenBlinks = Serial.parseInt();
Serial.println("Birth Month");
while (Serial.available()==0){}; Serial.println("Green LED is Blinking");
redBlinks = Serial.parseInt(); int n=1;
while (n<=greenBlinks){
Serial.println("Red LED is Blinking"); Serial.print("Blink no.: ");
int j=1; Serial.println(n);
while (j<=redBlinks){ digitalWrite(greenLED,HIGH);
Serial.print("Blink no.: "); delay(timeON);
Serial.println(j); digitalWrite(greenLED,LOW);
digitalWrite(redLED,HIGH); delay(timeOFF);
delay(timeON); n=n+1;
digitalWrite(redLED,LOW); }
delay(timeOFF);
j=j+1;
} delay(10000);
Serial.println(" "); Serial.println(" ");
Serial.println("what is your birth day? "); }
while (Serial.available()==0){};

You might also like