Activity 1 - Arduino -keypad-LCD
*
* Created by [Link]
*
* This example code is in the public domain
*
* Tutorial page: [Link]
*/
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
const int ROW_NUM = 4; // four rows
const int COLUMN_NUM = 4; // four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
int cursorColumn = 0;
void setup(){
[Link](); // initialize the lcd
[Link]();
}
void loop(){
char key = [Link]();
if (key) {
[Link](cursorColumn, 0); // move cursor to (cursorColumn, 0)
[Link](key); // print key at (cursorColumn, 0)
cursorColumn++; // move cursor to next position
if(cursorColumn == 16) { // if reaching limit, clear LCD
[Link]();
cursorColumn = 0;
}
}
}
Arduino-Ultrasonics-Buzzer
*
* Created by [Link]
*
* This example code is in the public domain
*
* Tutorial page: [Link]
*/
// constants won't change
const int TRIG_PIN = 6; // Arduino pin connected to Ultrasonic Sensor's TRIG pin
const int ECHO_PIN = 7; // Arduino pin connected to Ultrasonic Sensor's ECHO pin
const int BUZZER_PIN = 3; // Arduino pin connected to Piezo Buzzer's pin
const int DISTANCE_THRESHOLD = 50; // centimeters
// variables will change:
float duration_us, distance_cm;
void setup() {
[Link] (9600); // initialize serial port
pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode
pinMode(ECHO_PIN, INPUT); // set arduino pin to input mode
pinMode(BUZZER_PIN, OUTPUT); // set arduino pin to output mode
}
void loop() {
// generate 10-microsecond pulse to TRIG pin
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// measure duration of pulse from ECHO pin
duration_us = pulseIn(ECHO_PIN, HIGH);
// calculate the distance
distance_cm = 0.017 * duration_us;
if(distance_cm < DISTANCE_THRESHOLD)
digitalWrite(BUZZER_PIN, HIGH); // turn on Piezo Buzzer
else
digitalWrite(BUZZER_PIN, LOW); // turn off Piezo Buzzer
// print the value to Serial Monitor
[Link]("distance: ");
[Link](distance_cm);
[Link](" cm");
delay(500);
}
Arduino-servo motor-joystick
/*
* Created by [Link]
* This example code is in the public domain
* Tutorial page: [Link]
*/
#include <Servo.h>
#define VRX_PIN A0 // Arduino pin connected to VRX pin
#define VRY_PIN A1 // Arduino pin connected to VRY pin
#define SERVO_X_PIN 2 // Arduino pin connected to Servo motor 1
#define SERVO_Y_PIN 3 // Arduino pin connected to Servo motor 2
Servo xServo; // create servo object to control a servo 1
Servo yServo; // create servo object to control a servo 2
void setup() {
[Link](9600) ;
[Link](SERVO_X_PIN);
[Link](SERVO_Y_PIN);
void loop() {
// read analog X and Y analog values
int xValue = analogRead(VRX_PIN);
int yValue = analogRead(VRY_PIN);
int xAngle = map(xValue, 0, 1023, 0, 180); // scale it to the servo's angle (0 to 180)
int yAngle = map(yValue, 0, 1023, 0, 180); // scale it to the servo's angle (0 to 180)
[Link](xAngle); // rotate servo motor 1
[Link](yAngle); // rotate servo motor 2
// print data to Serial Monitor on Arduino IDE
[Link]("Joystick: ");
[Link](xValue);
[Link](", ");
[Link](yValue);
[Link](" => Servo Motor: ");
[Link](xAngle);
[Link]("°, ");
[Link](yAngle);
[Link]("°");
}
Arduino Stepper Motor
/*
* Created by [Link]
* This example code is in the public domain
* Tutorial page: [Link]
*/
// Include the AccelStepper Library
#include <AccelStepper.h>
// define step constant
#define FULLSTEP 4
#define STEP_PER_REVOLUTION 2048 // this value is from datasheet
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
AccelStepper stepper(FULLSTEP, 11, 9, 10, 8);
void setup() {
[Link](9600);
[Link](1000.0); // set the maximum speed
[Link](50.0); // set acceleration
[Link](200); // set initial speed
[Link](0); // set position
[Link](STEP_PER_REVOLUTION); // set target position: 64 steps <=> one revolution
void loop() {
// change direction once the motor reaches target position
if ([Link]() == 0)
[Link](-[Link]());
[Link](); // MUST be called in loop() function
[Link](F("Current Position: "));
[Link]([Link]());