Lab 13/ HW 13
Lab 13 (LCD);
//LCD with Progress Bar
//Include the library code:
#include <LiquidCrystal.h>
#include <Wire.h>
//Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int temp_address = 73;
//Create the progress bar characters
byte p20[8] = {
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
};
byte p40[8] = {
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
};
byte p60[8] = {
B11100,
B11100,
B11100,
B11100,
B11100,
B11100,
B11100,
B11100,
};
byte p80[8] = {
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
};
byte p100[8] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
};
void setup()
{
//Set up the LCDs number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("ENGR6 Display");
//Make progress characters
lcd.createChar(0, p20);
lcd.createChar(1, p40);
lcd.createChar(2, p60);
lcd.createChar(3, p80);
lcd.createChar(4, p100);
//initialze serial communication at 9600 bits/sec
Serial.begin(9600);
Wire.begin();
}
void loop()
{
//read the input on pin A0
int sensorValue = analogRead(A0);
//print out the value you read
Serial.println(sensorValue);
//Move cursor to second line
lcd.setCursor(0, 1);
//Clear the line each time it reaches the end
//with 16 " " (spaces)
//lcd.print(" ");
//Iterate through each character on the second line
/* for (int i = 0; i < 16; i++)
{
//Iterate through each progress value for each character
for (int j = 0; j < 5; j++)
{
lcd.setCursor(i, 1); //Move the cursor to this location
lcd.write(j); //Update progress bar
delay(100); //Wait
}
}*/
//display specific button press based on values
if(sensorValue == 740)
{
lcd.print("select ");
}
if(sensorValue == 502)
{
lcd.print("left ");
}
if(sensorValue == 142)
{
lcd.print("up ");
}
if(sensorValue == 326)
{
lcd.print("down ");
}
if(sensorValue == 0)
{
lcd.print("right ");
}
}
HW 13 (Pointers):
Pointer Program:
#include <stdio.h>
int i1, i2;
int *p1, *p2;
int main(void)
{
i1 = 5;
p1 = &i1;
i2 = *p1/2 + 10;
p2 = p1;
printf("The value of i1: %i\n\n", i1);
printf("The value of i2: %i\n\n", i2);
printf("The value of *p1: %i\n\n", *p1);
printf("The value of *p2: %i\n\n", *p2);
return 0;
}
//LCD with Progress Bar
//Include the library code:
#include <LiquidCrystal.h>
#include <Wire.h>
//Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int temp_address = 73;
//Create the progress bar characters
byte p20[8] = {
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
};
byte p40[8] = {
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
};
byte p60[8] = {
B11100,
B11100,
B11100,
B11100,
B11100,
B11100,
B11100,
B11100,
};
byte p80[8] = {
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
};
byte p100[8] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
};
void setup()
{
//Set up the LCDs number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("ENGR6 Display");
//Make progress characters
lcd.createChar(0, p20);
lcd.createChar(1, p40);
lcd.createChar(2, p60);
lcd.createChar(3, p80);
lcd.createChar(4, p100);
//initialze serial communication at 9600 bits/sec
Serial.begin(9600);
Wire.begin();
}
void loop()
{
//read the input on pin A0
int sensorValue = analogRead(A0);
//print out the value you read
Serial.println(sensorValue);
//Move cursor to second line
lcd.setCursor(0, 1);
//Clear the line each time it reaches the end
//with 16 " " (spaces)
//lcd.print(" ");
//Iterate through each character on the second line
/* for (int i = 0; i < 16; i++)
{
//Iterate through each progress value for each character
for (int j = 0; j < 5; j++)
{
lcd.setCursor(i, 1); //Move the cursor to this location
lcd.write(j); //Update progress bar
delay(100); //Wait
}
}*/
//display specific button press based on values
if(sensorValue == 740)
{
lcd.print("select ");
}
if(sensorValue == 502)
{
lcd.print("left ");
}
if(sensorValue == 142)
{
lcd.print("up ");
}
if(sensorValue == 326)
{
lcd.print("down ");
}
if(sensorValue == 0)
{
lcd.print("right ");
}
}
HW 13 (Pointers):
Pointer Program:
#include <stdio.h>
int i1, i2;
int *p1, *p2;
int main(void)
{
i1 = 5;
p1 = &i1;
i2 = *p1/2 + 10;
p2 = p1;
printf("The value of i1: %i\n\n", i1);
printf("The value of i2: %i\n\n", i2);
printf("The value of *p1: %i\n\n", *p1);
printf("The value of *p2: %i\n\n", *p2);
return 0;
}
Comments
Post a Comment