Skip to main content

Day 21: Arrays of Structures | Timers and Interrupts

Lab

const int ButtonInt=0;
const int red=11;
const int yellow=10;
const int green=9;
volatile int selectLED=red;
int lastLED=green;
void setup() {
  pinMode(red,OUTPUT);
  pinMode(yellow,OUTPUT);
  pinMode(green,OUTPUT);
  attachInterrupt(ButtonInt,swap,RISING);
  Serial.begin(9600);
}

void swap()
{
  analogWrite(selectLED,0);
  if(lastLED==red)
  {
    selectLED=yellow;
  }
  else if(lastLED==yellow)
  {
    selectLED=green;
  }
  else if(lastLED==green)
  {
    selectLED=red; 
  }
  lastLED=selectLED;
  Serial.println(selectLED);
}
void loop() {
  for(int i=0; i<256; i++)
  {
    analogWrite(selectLED,i);
    delay(10);
  }
  for(int i=255; i>=0; i--)
  {
    analogWrite(selectLED,i);
    delay(10);
  }
}

Lecture

Arrays of Structures

Notes

  • Arrays can contain only single type of information, e.g. array of integers or array of character strings
  • If array is needed to store info that contains different types of values, use array of structures
Example 1.1
h[0].name = "Camille";
h[0].year = 1969;
h[0].category = 5;

Example 1.1 shows how to access an individual data member of a structure in an array. The array name, subscript, and data member name must be specified. 

To access an entire structure within the array, the name of the array and a subscript must be specified. 

Example 1.2 
print_hurricane(h[0]);

Example 1.2 would output the following
Hurricane: Camille
Year: 1969
Category: 5

Example 2.1

#include <stdio.h> 
#define FILENAME "storms2.txt" 
/*  Define structure to represent a hurricane.  */ 
struct hurricane 
{ 
char name[10]; 
int year, category; 
}; 
int main(void) 
{ 
/*  Declare variables and function prototype.  */ 
int max_category=0, k=0, npts; 
struct hurricane h[100]; 
FILE *storms; 
void print_hurricane(struct hurricane h); 
/*  Read and print information from the file.  */ 
storms = fopen(FILENAME,"r"); 
if (storms == NULL) 
printf("Error opening data file. \n"); 
else 
{ 
printf("Hurricanes with Maximum Category \n"); 
while (fscanf(storms, "%s %d %d",h[k].name,&h[k].year, 
&h[k].category) == 3) 
{ 
if (h[k].category > max_category) 
max_category = h[k].category; 
k++; 
} 
npts = k; 
for (k=0; k<=npts-1; k++) 
if (h[k].category == max_category) 
print_hurricane(h[k]); 
fclose(storms); 
} 
/*  Exit program   */ 
return 0; 
} 
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ 
/*  This function prints the hurricane information.            */ 
void print_hurricane(struct hurricane h) 
{ 
printf("Hurricane: %s \n",h.name); 
printf("Year: %d, Category: %d \n",h.year,h.category); 
return; 
}

Problem Solving Example: Tsunamis


Comments

Popular posts from this blog

Day 20: Structures, Programming with Pointers

Lecture Structures Structure defines set of data, but individual parts do not have to be the same type. Example 1.1 struct hurricane {  char name[10];  int year,category; }; Within a structure, variables and even arrays can be defined. Structures are also known as aggregate data types since multiple data values can be collected into a single data type. Individual values within a structure are called data members, and each member is given a name. In Example 1.1, the names of the data members are name, year, and category. To refer to a data member, the structure variable name followed by a period and a data member name is used.  Definition and Initialization Define structure. Keyword struct used to define name of structure (aka structure tag) and data members that are included in structure After structure defined, structure variables can be defined using declaration statements. Semicolon required after structure definition. Statements can appear before m...

Day 4: RGB Nightlight | Algorithm Development, Conditional Expressions, Selection Statements

Hardware: RGB Nightlight const int red=11; const int green=10; const int blue=9; const int button=4; boolean lastbutton=LOW; boolean currentbutton=LOW; int ledMode=0; void setup() {   // put your setup code here, to run once:   pinMode(button,INPUT);   pinMode(red,OUTPUT);   pinMode(green,OUTPUT);   pinMode(blue,OUTPUT); } boolean debounce(boolean last) {   boolean current=digitalRead(button);   if(last!=current)   {     delay(5);     current=digitalRead(button);   }   return current; } void setMode(int mode) {   //RED   if(mode==1)   {     digitalWrite(red,HIGH);     digitalWrite(green,LOW);     digitalWrite(blue,LOW);   }   if(mode==2)   {     //orange     digitalWrite(red,HIGH);     analogWrite(green,40);     digitalWrite(blue,LOW);...