Day 19 HW
Comment: The program took me awhile to figure out how to correctly modify the header file so that the program would work. I never really understood how to get modification 3 and up to work. Also I never got the interrupt to work correctly on my arduino. Initially, I didn't write attach interrupt and spent a few hours wondering why I couldn't get the interrupt to work. The furthest I got was to get the led to change colors when I pressed the button, but never got the cds value to change the led.
#include <stdio.h>
#define FILENAME "waves2.txt"
/* Define structure to represent a tsunami. */
struct tsunami
{
int mo, da, yr, fatalities;
double max_height;
char location[20];
};
int main(void)
{
/* Declare variables. */
int k=0,i=0, npts,max_fata=0;
double max=0, sum=0, ave;
struct tsunami t[100];
FILE *waves;
/* Read and print information from the file. */
waves = fopen(FILENAME,"r");
if (waves == NULL)
printf("Error opening data file. \n");
else
{
while (fscanf(waves,"%d %d %d %d %lf %s",&t[k].mo,&t[k].da,
&t[k].yr,&t[k].fatalities,&t[k].max_height,t[k].location) == 6)
{
sum = sum + t[k].max_height;
if (t[k].max_height > max)
max = t[k].max_height;
k++;
}
if(t[k].max_height>max)
i++;
for(i=0;i<=9;i++)
{
if(t[i].fatalities>max_fata)
{
t[i].fatalities=max_fata;
i=t[i].yr;
}
}
npts = k;
ave = (sum/9);
printf("Summary Information for Tsunamis \n");
printf("Maximum Wave Height (in feet): %.2f \n",max*3.28);
printf("Average Wave Height (in feet): %.2f \n",ave*3.28);
printf("Tsunamis with greater than average heights: \n");
printf(" Number of tsunami the same year as the maximum height:%i\n",i);
printf("Year with maximum fatalities:%i",t[i].year);
for (k=0; k<=npts-1; k++)
if (t[k].max_height > ave)
printf("%s \n",t[k].location);
fclose(waves);
}
return 0;
}
const int BUTTON_INT =0;
const int RED =11;
const int GREEN =10;
const int BLUE =9;
volatile int selectedLED =RED;
int light=A0;
int val;
void setup() {
// put your setup code here, to run once:
pinMode(RED,OUTPUT);
pinMode(GREEN,OUTPUT);
pinMode(BLUE,OUTPUT);
attachInterrupt(BUTTON_INT,swap,RISING);
Serial.begin(9600);
}
void swap()
{
analogWrite(selectedLED,0);
if(selectedLED ==GREEN)
selectedLED = RED;
else if(selectedLED == RED)
selectedLED = BLUE;
else if (selectedLED == BLUE)
selectedLED = GREEN;
}
void loop() {
val=analogRead(light);
Serial.println(val);
if(val<800)
{
selectedLED=RED;
analogWrite(RED,LOW);
}
else
{
// put your main code here, to run repeatedly:
for(int i=0;i<256;i++)
{
analogWrite(selectedLED,i);
delay(10);
}
for(int i=255;i>=0;i--)
{
analogWrite(selectedLED,i);
delay(10);
}
}
}
#include <stdio.h>
#define FILENAME "waves2.txt"
/* Define structure to represent a tsunami. */
struct tsunami
{
int mo, da, yr, fatalities;
double max_height;
char location[20];
};
int main(void)
{
/* Declare variables. */
int k=0,i=0, npts,max_fata=0;
double max=0, sum=0, ave;
struct tsunami t[100];
FILE *waves;
/* Read and print information from the file. */
waves = fopen(FILENAME,"r");
if (waves == NULL)
printf("Error opening data file. \n");
else
{
while (fscanf(waves,"%d %d %d %d %lf %s",&t[k].mo,&t[k].da,
&t[k].yr,&t[k].fatalities,&t[k].max_height,t[k].location) == 6)
{
sum = sum + t[k].max_height;
if (t[k].max_height > max)
max = t[k].max_height;
k++;
}
if(t[k].max_height>max)
i++;
for(i=0;i<=9;i++)
{
if(t[i].fatalities>max_fata)
{
t[i].fatalities=max_fata;
i=t[i].yr;
}
}
npts = k;
ave = (sum/9);
printf("Summary Information for Tsunamis \n");
printf("Maximum Wave Height (in feet): %.2f \n",max*3.28);
printf("Average Wave Height (in feet): %.2f \n",ave*3.28);
printf("Tsunamis with greater than average heights: \n");
printf(" Number of tsunami the same year as the maximum height:%i\n",i);
printf("Year with maximum fatalities:%i",t[i].year);
for (k=0; k<=npts-1; k++)
if (t[k].max_height > ave)
printf("%s \n",t[k].location);
fclose(waves);
}
return 0;
}
const int BUTTON_INT =0;
const int RED =11;
const int GREEN =10;
const int BLUE =9;
volatile int selectedLED =RED;
int light=A0;
int val;
void setup() {
// put your setup code here, to run once:
pinMode(RED,OUTPUT);
pinMode(GREEN,OUTPUT);
pinMode(BLUE,OUTPUT);
attachInterrupt(BUTTON_INT,swap,RISING);
Serial.begin(9600);
}
void swap()
{
analogWrite(selectedLED,0);
if(selectedLED ==GREEN)
selectedLED = RED;
else if(selectedLED == RED)
selectedLED = BLUE;
else if (selectedLED == BLUE)
selectedLED = GREEN;
}
void loop() {
val=analogRead(light);
Serial.println(val);
if(val<800)
{
selectedLED=RED;
analogWrite(RED,LOW);
}
else
{
// put your main code here, to run repeatedly:
for(int i=0;i<256;i++)
{
analogWrite(selectedLED,i);
delay(10);
}
for(int i=255;i>=0;i--)
{
analogWrite(selectedLED,i);
delay(10);
}
}
}
Comments
Post a Comment