Day11
lab 11
//Servo Potentiometer Control
#include <Servo.h>
const int SERVO1 = 9; //Servo on Pin 9
const int SERVO2 = 8;
const int GREEN = 10;
const int BLUE = 11;
const int POT = 0; //POT on Analog Pin 0
Servo myServo1;
Servo myServo2;
int val1 = 0; //for storing the reading from the POT
int val2 =0;
void setup()
{
Serial.begin(9600); //Serial Port at 9600 baud
//Set pins as outputs
myServo1.attach(SERVO1);
myServo2.attach(SERVO2);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}
void loop()
{
//Keep working as long as data is in the buffer
while (Serial.available() > 0)
{
//
val1 = Serial.parseInt(); //First valid integer
val2 = Serial.parseInt(); //Second valid integer
// bval = Serial.parseInt(); //Third valid integer
//
//
if (Serial.read() == '\n') //Done transmitting
{
if (val2 >= 0 && val2 <= 179)
{
digitalWrite(GREEN, LOW);
myServo1.write(val2);
}
else
digitalWrite(GREEN, HIGH);
if (val1 >= 0 && val1 <= 179)
{
digitalWrite(11, LOW);
myServo2.write(val1);
}
else
digitalWrite(11, HIGH);
}
}
}
Homework
/* This program generates a summary report from */
/* a data file that has a trailer record with */
/* negative values. */
#include <stdio.h>
#define FILENAME "sensor2.txt"
int main(void)
{
/* Declare and initialize variables. */
int num_data_pts=0;
double time, motion, sum=0, max, min;
FILE *sensor;
/* Open file and read the first data point. */
sensor = fopen(FILENAME,"r");
if (sensor == NULL)
printf("Error opening input file. \n");
else
{
fscanf(sensor,"%lf %lf",&time,&motion);
/* Initialize variables using first data point. */
max = min = motion;
/* Update summary data until trailer record read. */
do
{
sum += motion;
if (motion > max)
max = motion;
if (motion < min)
min = motion;
num_data_pts++;
fscanf(sensor,"%lf %lf",&time,&motion);
} while (time >= 0);
/* Print summary information. */
printf("Number of sensor readings: %d \n",num_data_pts);
printf("Maximum reading: %.2f \n",max);
printf("Minimum reading: %.2f \n",min);
printf("Average: %.2f \n",sum/num_data_pts);
fclose(sensor); /* Close file. */
}
return 0;/* Exit program. */
}

Comments
Post a Comment