Lab 7/ HW 7
Lab 7 (PIR + Servo):
//Servo Potentiometer Control
#include <Servo.h>
const int SERVO=8; //Servo on Pin 9
const int PIR=3; //POT on Analog Pin 0
Servo myServo;
int pos = 0; //for storing the reading from the POT
void setup()
{
Serial.begin(2400);
pinMode (PIR, INPUT);
myServo.attach(SERVO);
}
void loop()
{
//pos = digitalRead(PIR); //Read PIR
//pos = map(pos, 0, 0, 0, 180); //scale it to servo range
//myServo.write(pos); //sets the servo
//delay(15); //waits for the servo
if(digitalRead(PIR) == HIGH)
{
Serial.println("motion seen");
for(pos=0; pos<180; pos+=179)
{
myServo.write(pos);
delay(2000);
}
}
if(digitalRead(PIR) == LOW)
{
Serial.println("no motion");
for(pos = 270; pos>=1; pos-=270)
{
myServo.write(pos);
delay(2000);
}
}
}
/* pos = map(pos, 0, 180, 0, 0 );
myServo.write(pos);
delay(100);
}*/
/* else
{
Serial.println("no motion");
pos = map(pos, 0, 180, 0, 180);
myServo.write(pos);
delay(100);
}*/
HW 7(Random Numbers):
//Servo Potentiometer Control
#include <Servo.h>
const int SERVO=8; //Servo on Pin 9
const int PIR=3; //POT on Analog Pin 0
Servo myServo;
int pos = 0; //for storing the reading from the POT
void setup()
{
Serial.begin(2400);
pinMode (PIR, INPUT);
myServo.attach(SERVO);
}
void loop()
{
//pos = digitalRead(PIR); //Read PIR
//pos = map(pos, 0, 0, 0, 180); //scale it to servo range
//myServo.write(pos); //sets the servo
//delay(15); //waits for the servo
if(digitalRead(PIR) == HIGH)
{
Serial.println("motion seen");
for(pos=0; pos<180; pos+=179)
{
myServo.write(pos);
delay(2000);
}
}
if(digitalRead(PIR) == LOW)
{
Serial.println("no motion");
for(pos = 270; pos>=1; pos-=270)
{
myServo.write(pos);
delay(2000);
}
}
}
/* pos = map(pos, 0, 180, 0, 0 );
myServo.write(pos);
delay(100);
}*/
/* else
{
Serial.println("no motion");
pos = map(pos, 0, 180, 0, 180);
myServo.write(pos);
delay(100);
}*/
HW 7(Random Numbers):
/* This program generates and prints ten random */
/* integers between user-specified limits. */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
/* Declare variables and function prototype. */
unsigned int seed;
int a, b, k;
int rand_int(int a,int b);
/* Get seed value and interval limits. */
printf("Enter a positive integer seed value: \n");
scanf("%u",&seed);
srand(seed);
printf("Enter integer limits a and b (a<b): \n");
scanf("%i %i",&a,&b);
/* Generate and print ten random numbers. */
printf("Random Numbers: \n");
for (k=1; k<=10; k++)
printf("%i ",rand_int(a,b));
printf("\n");
/* Exit program. */
return 0;
}
/* This function generates a random integer */
/* between specified limits a and b (a<b). */
int rand_int(int a,int b)
{
return rand()%(b-a+1) + a;
}
HW 7 (Dice Roll):
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned int seed;
int rolls, dice_1, dice_2, k, sum=0;
int rand_int(int a, int b);
// Get seed value
printf("Enter positive integer seed value: \n");
scanf("%u", &seed);
srand(seed);
// How many # of rolls
printf("Enter number of rolls of dice: \n");
scanf("%i",&rolls);
//simulation of rolls
for (k=1; k<=rolls; k++)
{
dice_1 = rand_int(1, 6);
dice_2 = rand_int(1,6);
if (dice_1 + dice_2 == 8);
sum++;
}
// compute and print % of rolls w/ sum 8
printf("dice 1 val %f \n", dice_1);
printf ("sum: %f \n", sum);
printf ("Percent with sum of 8: %.2f \n", sum*100/rolls);
return 0;
}
int rand_int(int a,int b)
{
return rand()%(b-a+1) + a;
}
HW 7 (Recursion Functions):
/* This program compares a recursive function and */
/* a nonrecursive function for computing factorials. */
#include <stdio.h>
int main(void)
{
/* Declare variables and function prototypes. */
int n;
double factorial(int k);
double factorial_r(int k);
/* Get user input. */
printf("Enter positive integer: \n");
scanf("%i",&n);
/* Compute and print factorials. */
printf("Nonrecursive: %f! = %f \n",n,factorial(n));
printf("Recursive: %f! = %f \n",n,factorial_r(n));
/* Exit program. */
return 0;
}
/* This function computes a factorial with a loop. */
double factorial(int k)
{
/* Declare variables. */
int j;
double term;
/* Compute factorial with multiplication. */
term = 1;
for (j=2; j<=k; j++)
term *=j;
/* Return factorial value. */
return term;
}
/* This function computes a factorial recursively. */
double factorial_r(int k)
{
/* Use recursive reference until k=0. */
if (k == 0)
return 1;
else
return k*factorial_r(k-1);
}
Comments
Post a Comment