//LCD with I2C
#include <Wire.h>
//Include the library code:
#include <LiquidCrystal.h>
//Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int temp_address = 72; //1001000 written as decimal number
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
//Set up the LCDs number of columns and rows:
lcd.begin(16, 2);
//Create a Wire object
Wire.begin();
}
void loop() {
//Start talking to the device at the specified address
Wire.beginTransmission(temp_address);
//Send a bit asking for register zero, the data register
Wire.write(0);
//Complete Transmission
Wire.endTransmission();
//Read the temperature from the device
//Request 1 Byte from the specified address
Wire.requestFrom(temp_address, 1);
//Wait for response
while(Wire.available() == 0);
//Get the temp and read it into a variable
int c = Wire.read();
c = map(c, 22, 32, 22, 32);
c = constrain(c, 22, 32);
int buttonval = analogRead(A0);// let buttons read as variable
int f = round(c*9.0/5.0 +32.0);//convert to farenheit
// read the input on analog pin 0:
Serial.println(buttonval);
delay(1);
Serial.println(c);
delay(500); // delay in between reads
Serial.println(f);
delay(500); // delay in between reads
if ( buttonval == 481)
{
switch(c){
case 23:{
lcd.setCursor(0,1);
lcd.print("23C");
break;
}
case 24:{
lcd.setCursor(0,1);
lcd.print("24C");
break;
}
case 25:{
lcd.setCursor(0,1);
lcd.print("25C ");
break;
}
case 26:{
lcd.setCursor(0,1);
lcd.print("26C");
break;
}
}
}
else if ( buttonval == 0){
switch(f){
case 73:{
lcd.setCursor(0,1);
lcd.print("73F ");
break;
}
case 75:{
lcd.setCursor(0,1);
lcd.print("75F ");
break;
}
case 77:{
lcd.setCursor(0,1);
lcd.print("77F ");
break;
}
case 79:{
lcd.setCursor(0,1);
lcd.print("79F ");
break;
}
}
}
return;
}
Monday, October 15, 2018
day 14 assignment
/* This program demonstrates the relationship between /
/* variables and addresses. */
#include <stdio.h>
int main(void)
{
/* Declare and initialize variables. */
int a=1, b=2;
/*Print the contents and addresses of a and b. */
printf("a = %d; address of a = %u \n",a,&a);
printf("b = %d; address of b = %u \n",b,&b);
/* Exit program.*/
return 0;
}
/* variables and addresses. */
#include <stdio.h>
int main(void)
{
/* Declare and initialize variables. */
int a=1, b=2;
/*Print the contents and addresses of a and b. */
printf("a = %d; address of a = %u \n",a,&a);
printf("b = %d; address of b = %u \n",b,&b);
/* Exit program.*/
return 0;
}
Thursday, October 11, 2018
day 13 assignment
/*?????????????????????????????????????????????????????????????*/
/* This program determines the locations of peaks in an */
/* grid of elevation data. */
#include <stdio.h>
#define N 25
#define FILENAME "grid1.txt"
int main(void)
{
/* Declare variables. */
int nrows, ncols, i, j;
int numpeaks=0;
int numvalleys=0;
double elevation[N][N];
FILE *grid;
/* Read information from a data file. */
grid = fopen(FILENAME,"r");
if (grid == NULL)
printf("Error opening input file\n");
else
{
fscanf(grid,"%d %d",&nrows,&ncols);
for (i=0; i<=nrows-1; i++)
for (j=0; j<=ncols-1; j++)
fscanf(grid,"%lf",&elevation[i][j]);
/* Determine and print peak locations. */
printf("Top left point defined as row 0, column 0 \n");
for (i=1; i<=nrows-2; i++)
for (j=1; j<=ncols-2; j++)
if ((elevation[i-1][j]<elevation[i][j]) &&
(elevation[i+1][j]<elevation[i][j]) &&
(elevation[i][j-1]<elevation[i][j]) &&
(elevation[i][j+1]<elevation[i][j])){
numpeaks = numpeaks +1;
numvalleys =
printf("Peak at row: %d column: %d \n",i,j);
}
printf("peak count is %i \n", numpeaks);
fclose(grid); /* Close file. */
}
return 0; /* Exit program. */
}
/* This program determines the locations of peaks in an */
/* grid of elevation data. */
#include <stdio.h>
#define N 25
#define FILENAME "grid1.txt"
int main(void)
{
/* Declare variables. */
int nrows, ncols, i, j;
int numpeaks=0;
int numvalleys=0;
double elevation[N][N];
FILE *grid;
/* Read information from a data file. */
grid = fopen(FILENAME,"r");
if (grid == NULL)
printf("Error opening input file\n");
else
{
fscanf(grid,"%d %d",&nrows,&ncols);
for (i=0; i<=nrows-1; i++)
for (j=0; j<=ncols-1; j++)
fscanf(grid,"%lf",&elevation[i][j]);
/* Determine and print peak locations. */
printf("Top left point defined as row 0, column 0 \n");
for (i=1; i<=nrows-2; i++)
for (j=1; j<=ncols-2; j++)
if ((elevation[i-1][j]<elevation[i][j]) &&
(elevation[i+1][j]<elevation[i][j]) &&
(elevation[i][j-1]<elevation[i][j]) &&
(elevation[i][j+1]<elevation[i][j])){
numpeaks = numpeaks +1;
numvalleys =
printf("Peak at row: %d column: %d \n",i,j);
}
printf("peak count is %i \n", numpeaks);
fclose(grid); /* Close file. */
}
return 0; /* Exit program. */
}
day 13 lab
//Reads Temp from I2C temperature sensor and prints it on the serialport
//Include Wire I2C library
#include <Wire.h>
#include "pitches.h"
const int speaker=9;
int temp_address = 72; //1001000 written as decimal number
int notes[] = { NOTE_B0, NOTE_C1, NOTE_CS1, NOTE_D1, NOTE_DS1};
int value[] = { 22, 23, 24, 25, 26} ;
int d;
void setup()
{
//Start serial communication at 9600 baud
Serial.begin(9600);
//Create a Wire object
Wire.begin();
}
void loop()
{
//Send a request
//Start talking to the device at the specified address
Wire.beginTransmission(temp_address);
//Send a bit asking for register zero, the data register
Wire.write(0);
//Complete Transmission
Wire.endTransmission();
//Read the temperature from the device
//Request 1 Byte from the specified address
Wire.requestFrom(temp_address, 1);
//Wait for response
while(Wire.available() == 0);
//Get the temp and read it into a variable
int c = Wire.read();
c = map(c, 22, 32, 22, 32);
c = constrain(c, 22, 32);
//Do some math to convert the Celsius to Fahrenheit
int f = round(c*9.0/5.0 +32.0);
//Send the temperature in degrees C and F to the serial monitor
Serial.println(c);
//Serial.print("C ");
//Serial.print(f);
//Serial.println("F");
delay(500);
switch ( c )
{
case 26:
{
tone(speaker, NOTE_DS1);
break;
}
case 25:
{
tone(speaker, NOTE_D1);
break;
}
case 24:
{
tone(speaker, NOTE_CS1);
break;
}
case 23:
{
tone(speaker, NOTE_C1);
break;
}
case 22:
{
tone(speaker, NOTE_B0);
break;
}
}
return;
}
//Include Wire I2C library
#include <Wire.h>
#include "pitches.h"
const int speaker=9;
int temp_address = 72; //1001000 written as decimal number
int notes[] = { NOTE_B0, NOTE_C1, NOTE_CS1, NOTE_D1, NOTE_DS1};
int value[] = { 22, 23, 24, 25, 26} ;
int d;
void setup()
{
//Start serial communication at 9600 baud
Serial.begin(9600);
//Create a Wire object
Wire.begin();
}
void loop()
{
//Send a request
//Start talking to the device at the specified address
Wire.beginTransmission(temp_address);
//Send a bit asking for register zero, the data register
Wire.write(0);
//Complete Transmission
Wire.endTransmission();
//Read the temperature from the device
//Request 1 Byte from the specified address
Wire.requestFrom(temp_address, 1);
//Wait for response
while(Wire.available() == 0);
//Get the temp and read it into a variable
int c = Wire.read();
c = map(c, 22, 32, 22, 32);
c = constrain(c, 22, 32);
//Do some math to convert the Celsius to Fahrenheit
int f = round(c*9.0/5.0 +32.0);
//Send the temperature in degrees C and F to the serial monitor
Serial.println(c);
//Serial.print("C ");
//Serial.print(f);
//Serial.println("F");
delay(500);
switch ( c )
{
case 26:
{
tone(speaker, NOTE_DS1);
break;
}
case 25:
{
tone(speaker, NOTE_D1);
break;
}
case 24:
{
tone(speaker, NOTE_CS1);
break;
}
case 23:
{
tone(speaker, NOTE_C1);
break;
}
case 22:
{
tone(speaker, NOTE_B0);
break;
}
}
return;
}
Thursday, October 4, 2018
day 12 lab
#include <Servo.h>
const int SERVO =9;
const int IR =0;
Servo myServo;
int pos = 0; // variable to store the servo position
void setup()
{
Serial.begin(9600);
myServo.attach(SERVO); // attach the servo
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myServo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
if (analogRead(IR) > 200)
{
delay(500);
}
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myServo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
if (analogRead(IR) > 200)
{
delay(500);
}
}
}
const int SERVO =9;
const int IR =0;
Servo myServo;
int pos = 0; // variable to store the servo position
void setup()
{
Serial.begin(9600);
myServo.attach(SERVO); // attach the servo
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myServo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
if (analogRead(IR) > 200)
{
delay(500);
}
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myServo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
if (analogRead(IR) > 200)
{
delay(500);
}
}
}
day 11 lab
//Single Character Control of an LED
#include <Servo.h>
const int LED=4;
const int servo=9;
int data; //Holds incoming character
int pos = 0;
Servo myServo;
void setup()
{
Serial.begin(9600); //Serial Port at 9600 baud
myServo.attach(servo); // attach servo
pinMode(LED, OUTPUT); // set led to output
}
void loop()
{
Serial.print(data);
delay(100);
//Only act when data is available in the buffer
while (Serial.available() > 0)
{
data = Serial.parseInt(); //Read byte of data
//Turn LED on
if (data <= 180)
{
myServo.write(data);
Serial.println(data);
}
//Turn on LED if over 180
else if (data > 180)
{
digitalWrite(LED, HIGH);
Serial.println(data);
}
}
}
#include <Servo.h>
const int LED=4;
const int servo=9;
int data; //Holds incoming character
int pos = 0;
Servo myServo;
void setup()
{
Serial.begin(9600); //Serial Port at 9600 baud
myServo.attach(servo); // attach servo
pinMode(LED, OUTPUT); // set led to output
}
void loop()
{
Serial.print(data);
delay(100);
//Only act when data is available in the buffer
while (Serial.available() > 0)
{
data = Serial.parseInt(); //Read byte of data
//Turn LED on
if (data <= 180)
{
myServo.write(data);
Serial.println(data);
}
//Turn on LED if over 180
else if (data > 180)
{
digitalWrite(LED, HIGH);
Serial.println(data);
}
}
}
Tuesday, October 2, 2018
day 10 assignment
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
/* Declare variables and function prototype. */
//unsigned int seed;
double interval_start, interval_end, k;
double rand_float(double interval_start,double interval_end);
/* Get seed value and interval limits. */
//printf("Enter a positive integer seed value: \n");
//scanf("%u",&seed);
srand(101);
printf("Enter integer limits a and b (a<b): \n");
scanf("%lf %lf",&interval_start,& interval_end);
/* Generate and print ten random numbers. */
printf("Random Numbers: \n");
for (k=1; k<=10; k++)
printf("%lf \n",rand_float(interval_start, interval_end));
/* Exit program. */
return 0;
}
/* This function generates a random integer */
/* between specified limits a and b (a<b). */
double rand_float(double interval_start,double interval_end)
{
return (rand()%32767/32767.0)*( interval_end - interval_start + 1) + interval_start;
}
Subscribe to:
Comments (Atom)