Docsity

MICROcontroller Reviewer, Exercises of Microcontrollers

Toggle sidebar
bg1
// LCD with a driver
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
// initialize the LCD
lcd.init();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.print("Hello, world!");
}
void loop()
{
// Do nothing here...
}
scl - a5 sda - a4
// Set the LCD
// countdown
#include <LiquidCrystal.h>
int x = 100;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
lcd.begin(16, 2);
lcd.print("hello, world!");
}
void loop() {
if (x!=0){
lcd.setCursor(0,1);
x=100-(millis()/50);
lcd.print(x);
}
else{
lcd.clear();
lcd.print("BOOOOM");
delay(1000);
}}
Pin Configuration
Pin 1 to Arduino GND
Pin 2 to Arduino 5V
Pin 3 to wiper ( this is the middle pin of the 10k
potentiometer )
Pin 4 to Arduino pin 12
Pin 5 to Arduino GND
Pin 6 to Arduino pin 11
Pin 11 to Arduino pin 5
Pin 12 to Arduino pin 4
Pin 13 to Arduino pin 3
Pin 14 to Arduino pin 2
//2 LCD
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LiquidCrystal lcd2(12, 10, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
lcd.print("hello, world!");
lcd2.begin(16, 2);
lcd2.print("hey, world!");
lcd2.setCursor(0, 1);
lcd2.print("it works!");
}
void loop() {
lcd.setCursor(0, 1);
lcd.print(millis()/100);
}
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download MICROcontroller Reviewer and more Exercises Microcontrollers in PDF only on Docsity!

// LCD with a driver #include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); void setup() { // initialize the LCD lcd.init(); // Turn on the blacklight and print a message. lcd.backlight(); lcd.print("Hello, world!"); } void loop() { // Do nothing here... } scl - a5 sda - a // Set the LCD // countdown #include <LiquidCrystal.h> int x = 100; const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); void setup() { lcd.begin(16, 2); lcd.print("hello, world!"); } void loop() { if (x!=0){ lcd.setCursor(0,1); x=100-(millis()/50); lcd.print(x); } else{ lcd.clear(); lcd.print("BOOOOM"); delay(1000); }} Pin Configuration Pin 1 to Arduino GND Pin 2 to Arduino 5V Pin 3 to wiper ( this is the middle pin of the 10k potentiometer ) Pin 4 to Arduino pin 12 Pin 5 to Arduino GND Pin 6 to Arduino pin 11 Pin 11 to Arduino pin 5 Pin 12 to Arduino pin 4 Pin 13 to Arduino pin 3 Pin 14 to Arduino pin 2 //2 LCD #include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); LiquidCrystal lcd2(12, 10, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); lcd.print("hello, world!"); lcd2.begin(16, 2); lcd2.print("hey, world!"); lcd2.setCursor(0, 1); lcd2.print("it works!"); } void loop() { lcd.setCursor(0, 1); lcd.print(millis()/100); }

// Input sa Serial, output sa LCD //Di nakikita sa Serial #include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); String msg=""; void setup() { lcd.init(); Serial.begin(9600); } void loop() { if (Serial.available()) { char z=Serial.read(); if (z==13){ lcd.clear(); lcd.print(msg); msg=""; } else{ msg+=z; } } } // Input sa Serial, output sa LCD //Nakikita sa Serial #include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); String msg=""; void setup() { lcd.init(); Serial.begin(9600); } void loop() { while(Serial.available()){ char c = Serial.read(); Serial.print(c); if (c==13){ lcd.clear(); lcd.setCursor(0,0); lcd.print(msg); msg=""; } else{ msg+=c; } } } // Display 1 Pot. Value in LCD #include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); int potval = 0; void setup() { lcd.init(); pinMode(A1,INPUT); } void loop() { potval = analogRead(A1); lcd.print (potval); lcd.setCursor (6,0); lcd.print ("Ohms"); delay (100); lcd.clear(); }

Serial.begin(9600); pinMode(A0,INPUT); } void loop() { if(Serial.available()){ msg=""; while(Serial.available()){ lcd.clear(); lcd2.clear(); msg+=(char)Serial.read(); } lcd.print("potVal= " +(String) msg); lcd2.print("potVal= " +(String) msg); } } //Serial Communication [1 way & 2 way] Input sa isang serial Display sa isang serial (ONE WAY) #include<SoftwareSerial.h> SoftwareSerial mySerial(2,3); void setup() { mySerial.begin(9600); Serial.begin(9600); } String msg=""; void loop() { if(Serial.available()){ char c= Serial.read(); Serial.print(c); if (c ==13){ mySerial.print(msg); msg=" "; } msg+=c; } } //Serial Communication [1 way & 2 way] Input sa isang serial Display sa isang serial (TWO WAY) // Limitation of Proteus , not simulating //(transmitter) #include<SoftwareSerial.h> SoftwareSerial mySerial(2,3); void setup() { // put your setup code here, to run once: mySerial.begin(9600); Serial.begin(9600); Serial.println("TRANSMITTER"); } String msg=""; void loop() { if(Serial.available()){ char c= Serial.read(); Serial.print(c); if (c ==13){ mySerial.print(msg); msg=" "; } msg+=c; } } //mc1 mc2 [1 & 2] potentiometer value to display in LCD using one Serial Connection going to another microcontroller to LCD [2 microcontroller //MC void setup() { Serial.begin(9600); // put your setup code here, to run once: pinMode(A0,INPUT); pinMode(A1,INPUT); } void loop() { int x = analogRead(A0); int y = analogRead(A1); Serial.println((String)x+","+(String)y); delay(500); }

//MC

#include <LiquidCrystal.h> String msg; const int rs = 12, en = 7, d4 = 5, d5 = 4, d6 = 3, d7 = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); void setup() { lcd.begin(16, 2); Serial.begin(9600); } void loop() { if(Serial.available()){ msg=""; while(Serial.available()){ lcd.clear(); msg+=(char)Serial.read(); } int ind1 = msg.indexOf(","); String pot1Val = msg.substring(0,ind1); lcd.setCursor(0,0); lcd.print("pot1Val= "+(String)pot1Val); String pot2Val = msg.substring(ind1+1); lcd.setCursor(0,1); lcd.print("pot2Val= "+(String)pot2Val); } } //Microcontroller - serial Monitor[input] - microcontroller - LCD Output 2 MICROCONTROLLER 2 CODES //Mc #include<SoftwareSerial.h> SoftwareSerial mySerial(2,3); void setup() { // put your setup code here, to run once: mySerial.begin(9600); Serial.begin(9600); Serial.println("TRANSMITTER"); } String msg=""; void loop() { if(Serial.available()){ char c= Serial.read(); Serial.print(c); if (c ==13){ mySerial.print(msg); msg=" "; } msg+=c; } } //MC2(Receiver) #include <LiquidCrystal.h> String msg=""; const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); void setup() { lcd.begin(16, 2); Serial.begin(9600); } void loop() { if(Serial.available()){ msg=""; while(Serial.available()){ lcd.clear(); msg+=(char)Serial.read(); } lcd.print(msg); } // LCD Configuration

if (msg== "3"){ g=102; analogWrite(9,g); } if(msg== "2"){ g=76; analogWrite(9,g); } if (msg== "1"){ g=51; analogWrite(9,g); } if (msg== "0"){ g=25; analogWrite(9,g); } msg=""; }if (ot==10){ msg=""; }else { msg+=ot; } } } // H Bridge Circuit // L298N Pin Configuration // Servo Motor // #1 Lab activity in servomotor #include <Servo.h> Servo myservo; // create servo object to control a servo int potpin = 0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)

myservo.write(val); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there } // #2 Lab activity in servomotor #include <Servo.h> Servo mysservo; int value=90; int pioLast; int pio; int kevs; int kevsLast; void setup () { Serial.begin(9600); mysservo.attach(10); mysservo.write(value); pinMode (4,OUTPUT); // ++ increment button pinMode (5,OUTPUT); // -- decrement button } void loop (){ pio = digitalRead (5); kevs = digitalRead (4); if ((value<=150 && value>=0)){ mysservo.write(value); if (pio!=pioLast && value<150){ if (pio==1){ pioLast = 1; value = value + 10; } else pioLast = 0; } if (kevs!=kevsLast && value>0){ if (kevs==1){ kevsLast = 1; value = value - 10; } else kevsLast = 0; } delay (10); Serial.println(value); } } // #3 Lab activity in servomotor (from someone) #include <Servo.h> Servo myservo; String msg; void setup() { Serial.begin(9600); myservo.attach(9); // put your setup code here, to run once: } void loop() { // put your main code here, to run repeatedly: if(Serial.available()){ char c = Serial.read(); if(c==13){ int x = msg.toInt(); if(x>=0&&x<=180){ Serial.print(x); myservo.write(x); delay(10); } msg=""; } if(c==10){ }else{ msg+=c; } } } // #3 Lab activity in servomotor (from another someone) #include <Servo.h> Servo myservo; String msg; int x=0; void setup() { myservo.attach(9); Serial.begin(9600); } void loop() { if (Serial.available()){ char c = Serial.read(); if (c==13){ int val = msg.toInt(); Serial.println(val); if (val<=180 && val>=0){ myservo.write(val); } x++; } if (c==10){ } else msg+=c; }

// Serial Communication Wired RC Remote Control Car (POT) // TRANSMITTER void setup() { Serial.begin(9600); } void loop() { int x = map(analogRead(A0), 0, 1023, 0, 9); int y = map(analogRead(A1), 0, 1023, 0, 9); Serial.println(String(x) + " " + String(y)); // RECEIVER int m=0; String value=""; String code1=""; String code2=""; void setup() { once: for (int x=3;x<=8;x++){ pinMode(x, OUTPUT); Serial.begin(9600); } } void loop() { if (Serial.available()){ char x = Serial.read(); if (x==13){ code1 = value.substring(0, 1); code2 = value.substring(2); Serial.println(value); m++; ForRev(); LefRig(); Stop(); } if (x==10){ } else{ value+=x; } if (m==1){ value=""; m=0; } } } void ForRev(){ if (code1=="9" || code1=="8" || code1=="7" || code1=="6"){ digitalWrite(3, 0); digitalWrite(4, 1); digitalWrite(7, 1); digitalWrite(8, 0); } if (code1=="0" || code1=="1" || code1=="2" || code1=="3"){ digitalWrite(3, 1); digitalWrite(4, 0); digitalWrite(7, 0); digitalWrite(8, 1); } } void LefRig(){ if (code2=="9"||code2=="8"||code2=="7"||code2=="6"){ digitalWrite(3, 1); digitalWrite(4, 0); digitalWrite(7, 1); digitalWrite(8, 0); } if (code2=="0" || code2=="1" || code2=="2" || code2=="3"){ digitalWrite(3, 0); digitalWrite(4, 1); digitalWrite(7, 0); digitalWrite(8, 1); } } void Stop(){ if ((code1=="4" || code1=="5") && (code2=="4" || code2=="5")){ digitalWrite(3, 0); digitalWrite(4, 0); digitalWrite(7, 0); digitalWrite(8, 0); } }

// QUIZ make a program that can control the motor using H bridge and 3 complementary switch CCW=counterclockwise CW=clockwise BUZZON=BuzzerOn BUZZOFF=buzzeroff Stop //COMTROLLER(transmitter) //diko alam kung tama tong gawa ko hahaha // carag void setup() { pinMode(2,INPUT_PULLUP); pinMode(3,INPUT_PULLUP); pinMode(4,INPUT_PULLUP); Serial.begin(9600); } void loop() { int x1= digitalRead(2); int y1= digitalRead(3); int z1= digitalRead(4); if(x1==LOW){ Serial.println("CCW"); }else if(y1==LOW){ Serial.println("CW"); }else if(z1==LOW){ Serial.println("BUZZON"); }else{ Serial.print("STOP "); Serial.println("BUZZOFF"); } } //RECEIVER // diko ko alam kung tama tong gawa ko haha String msg; void setup(){ Serial.begin(9600); pinMode(9,OUTPUT); pinMode(10,OUTPUT); pinMode(11,OUTPUT); } void loop(){ if(Serial.available()){ char c = Serial.read(); if(c==13){ motor(msg); msg=""; } if(c==10){ }else { msg+=c; } } } void motor(String x){ if(x.indexOf("STOP BUZZOFF")>=0){ digitalWrite(9,0); digitalWrite(10,0); digitalWrite(11,0); } if(x.indexOf("BUZZON")>=0){ digitalWrite(9,1); } if(x.indexOf("CCW")>=0){ digitalWrite(10,0); digitalWrite(11,1); } if(x.indexOf("CW")==1){ digitalWrite(11,0); digitalWrite(10,1); } } // Arduino DC Motor Control Tutorial – L298N | PWM | H-Bridge INTERNET /* Arduino DC Motor Control - PWM | H-Bridge | L298N Example 02 - Arduino Robot Car Control by Dejan Nedelkovski, www.HowToMechatronics.com */ #define enA 9 #define in1 4 #define in2 5 #define enB 10 #define in3 6 #define in4 7 int motorSpeedA = 0;