CLASSWORK/ HOMEWORK:
DNA String Lab:
/*??????????????????????????????????????????????????????????????-*/
/* This program initializes a long character string and a short */
/* character string. It then prints the locations of the short */
/* string in the long string. It also prints the number of */
/* occurrences of the short string in the long string. */
#include <stdio.h>
#include <string.h>
int main(void)
{
/* Declare and initialize variables. */
int count=0;
char long_str[]="AAACTGACATTGGACCTACTTTGACT",
short_str[]="ACT";
char *ptr1=long_str, *ptr2=short_str;
printf("Long String: %s \n", long_str);
printf("Short String: %s \n", short_str);
//for (int i=ptr1; i<=(ptr1+strlen(long_str)-1); i++)
//printf("Long String: %c",*(ptr1+i));
printf("ptr1\t\tlong_str \n");
printf("%i\t\t%i \n", ptr1, long_str);
/* Count the number of occurrences of short_str in long_str. */
/* While the function strstr does not return NULL, increment */
/* count and move ptr1 to next character of the long string. */
while ((ptr1=strstr(ptr1,ptr2)) != NULL)
{
printf("%i\t\t%i \n", ptr1, long_str);
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
/* Exit program. */
return 0;
}
/*????????????????????????????????????????????????????????????-*/
Part 1:
/*??????????????????????????????????????????????????????????????-*/
/* This program initializes a long character string and a short */
/* character string. It then prints the locations of the short */
/* string in the long string. It also prints the number of */
/* occurrences of the short string in the long string. */
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 1000
int main(void)
{
/* Declare and initialize variables. */
int count=0;
char long_str[MAX_SIZE];
char short_str[MAX_SIZE];
printf("Enter in long DNA string: \n");
scanf("%s",long_str);
printf("Enter in short DNA string: \n");
scanf("%s",short_str);
char *ptr1=long_str, *ptr2=short_str;
printf("\nLong DNA String: %s \n", long_str);
printf("Short DNA String: %s \n", short_str);
//for (int i=ptr1; i<=(ptr1+strlen(long_str)-1); i++)
//printf("Long String: %c",*(ptr1+i));
printf("\nptr1\t\tlong_str \n");
printf("%i\t\t%i \n", ptr1, long_str);
/* Count the number of occurrences of short_str in long_str. */
/* While the function strstr does not return NULL, increment */
/* count and move ptr1 to next character of the long string. */
while ((ptr1=strstr(ptr1,ptr2)) != NULL)
{
printf("%i\t\t%i \n", ptr1, long_str);
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
/* Exit program. */
return 0;
}
/*????????????????????????????????????????????????????????????-*/
Part 2:
/*??????????????????????????????????????????????????????????????-*/
/* This program initializes a long character string and a short */
/* character string. It then prints the locations of the short */
/* string in the long string. It also prints the number of */
/* occurrences of the short string in the long string. */
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 1000
#define FILENAME "dnastring.txt"
int main(void)
{
/* Declare and initialize variables. */
int count=0;
char long_str[MAX_SIZE];
char short_str[MAX_SIZE];
FILE *dna;
dna = fopen(FILENAME, "r");
if (dna == NULL)
printf("Error opening input file. \n");
else
{
fscanf(dna,"%s",long_str);
printf("Enter in short DNA string: \n");
scanf("%s",short_str);
char *ptr1=long_str, *ptr2=short_str;
printf("\nLong DNA String: %s \n", long_str);
printf("Short DNA String: %s \n", short_str);
//for (int i=ptr1; i<=(ptr1+strlen(long_str)-1); i++)
//printf("Long String: %c",*(ptr1+i));
printf("\nptr1\t\tlong_str \n");
printf("%i\t\t%i \n", ptr1, long_str);
/* Count the number of occurrences of short_str in long_str. */
/* While the function strstr does not return NULL, increment */
/* count and move ptr1 to next character of the long string. */
while ((ptr1=strstr(ptr1,ptr2)) != NULL)
{
printf("%i\t\t%i \n", ptr1, long_str);
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
fclose(dna);
}
/* Exit program. */
return 0;
}
/*????????????????????????????????????????????????????????????-*/
Part 3:
/*??????????????????????????????????????????????????????????????-*/
/* This program initializes a long character string and a short */
/* character string. It then prints the locations of the short */
/* string in the long string. It also prints the number of */
/* occurrences of the short string in the long string. */
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 1000
#define FILENAME "dnastring.txt"
int main(void)
{
/* Declare and initialize variables. */
int count=0;
char long_str[MAX_SIZE];
char short_str[MAX_SIZE];
FILE *dna;
dna = fopen(FILENAME, "r");
if (dna == NULL)
printf("Error opening input file. \n");
else
{
fscanf(dna,"%s",long_str);
printf("Enter in short DNA string: \n");
scanf("%s",short_str);
char *ptr1=long_str, *ptr2=short_str;
int short_length = strlen(short_str);
int long_length = strlen(long_str);
if (short_length > long_length)
printf("Error: Short string must be smaller than long string.\n");
else
{
printf("\nLong DNA String: %s \n", long_str);
printf("Short DNA String: %s \n", short_str);
//for (int i=ptr1; i<=(ptr1+strlen(long_str)-1); i++)
//printf("Long String: %c",*(ptr1+i));
//printf("\nptr1\t\tlong_str \n");
//printf("%i\t\t%i \n", ptr1, long_str);
/* Count the number of occurrences of short_str in long_str. */
/* While the function strstr does not return NULL, increment */
/* count and move ptr1 to next character of the long string. */
while ((ptr1=strstr(ptr1,ptr2)) != NULL)
{
//printf("%i\t\t%i \n", ptr1, long_str);
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
fclose(dna);
}
}
/* Exit program. */
return 0;
}
/*????????????????????????????????????????????????????????????-*/
Greetings! My name is Matthew Ibarra and this is my electronic notebook for ENGR 6 - Programming for Engineers. I am currently taking this class in Fall 2017 at Mt. SAC. In this blog, you can expect to find posts about my classroom activities, laboratory experiments, code documentation, project updates, and other relevant data.
Saturday, December 16, 2017
Day 20 (contd) - Dynamic Memory Allocation
CLASSWORK:
/* */
/* This program determines the maximum contiguous */
/* memory allocation that can be reserved during a */
/* specific program execution. */
#include <stdio.h>
#include <stdlib.h>
#define UNIT 1000000
int main(void)
{
/* Declare and initialize variables. */
double k=1;
int *ptr;
/* Find maximum amount of contiguous memory */
/* available in units of millions of integers. */
ptr = (int *)malloc(UNIT*sizeof(int));
while (ptr != NULL)
{
printf("%f integers \n",(k)*UNIT);
free(ptr);
k++;
ptr = (int *)malloc(k*UNIT*sizeof(int));
}
/* Print maximum amount of memory available. */
printf("Maximum contiguous memory available: \n");
/* Exit program. */
return 0;
}
HOMEWORK:
/* */
/* This program reads a seismic data file and then */
/* determines the times of possible seismic events. */
/* Dynamic memory allocation is used. */
#include <stdio.h>
#define FILENAME "seismic1.txt"
#define THRESHOLD 1.5
int main(void)
{
/* Declare variables and function prototypes. */
int k, npts, short_window, long_window;
double *sensor, time_incr, short_power,
long_power, ratio;
FILE *file_ptr;
double power_w(double *ptr,int n);
/* Read data header and allocate memory. */
file_ptr = fopen(FILENAME,"r");
if (file_ptr == NULL)
printf("Error opening input file. \n");
else
{
fscanf(file_ptr,"%d %lf",&npts,&time_incr);
sensor = (double *)malloc(npts*sizeof(double));
if (sensor == NULL)
printf("Not enough memory available. \n");
else
(no changes in the remainder of the program)
/* */
/* This program determines the maximum contiguous */
/* memory allocation that can be reserved during a */
/* specific program execution. */
#include <stdio.h>
#include <stdlib.h>
#define UNIT 1000000
int main(void)
{
/* Declare and initialize variables. */
double k=1;
int *ptr;
/* Find maximum amount of contiguous memory */
/* available in units of millions of integers. */
ptr = (int *)malloc(UNIT*sizeof(int));
while (ptr != NULL)
{
printf("%f integers \n",(k)*UNIT);
free(ptr);
k++;
ptr = (int *)malloc(k*UNIT*sizeof(int));
}
/* Print maximum amount of memory available. */
printf("Maximum contiguous memory available: \n");
/* Exit program. */
return 0;
}
HOMEWORK:
/* */
/* This program reads a seismic data file and then */
/* determines the times of possible seismic events. */
/* Dynamic memory allocation is used. */
#include <stdio.h>
Lab 18 - Using Timer Interrupts
CLASSWORK:
Blinking LED with Timer Interrupts:
//Using Timer Interrupts with the Arduino
#include <TimerOne.h>
const int LED=13;
void setup()
{
pinMode(LED, OUTPUT);
Timer1.initialize(1000000); //Set a timer of length 1000000
//microseconds (1 second)
Timer1.attachInterrupt(blinky); //Runs "blinky" on each
//time interrupt
}
void loop()
{
// put your main code here, to run repeatedly:
}
//Timer interrupt function
void blinky()
{
digitalWrite(LED, !digitalRead(LED)); //Toggle LED State
}
HOMEWORK:
Basic Speaker with Hardware and Timer Interrupts:
//Use Hardware and Timer Interrupts for Fun with Sound
//Include the TimerOne library
#include <TimerOne.h>
//Button pins
const int BUTTON_INT =0;
const int SPEAKER =12;
//Music keys
#define NOTE_C 65
#define NOTE_D 73
#define NOTE_E 82
#define NOTE_F 87
#define NOTE_G 98
#define NOTE_A 110
#define NOTE_B 123
//Volatile variables can change inside interrupts
volatile int key = NOTE_C;
volatile int octave_multiplier = 1;
void setup()
{
//Set up Serial
Serial.begin(9600);
pinMode(SPEAKER, OUTPUT);
//The pin is inverted, so we want to look at the rising edge
attachInterrupt(BUTTON_INT, changeKey, RISING);
//Set up timer interrupt
Timer1.initialize(500000); //Set a timer of length .5 seconds
Timer1.attachInterrupt(changePitch); //Runs "blinky" on each
//time interrupt
}
void changeKey()
{
octave_multiplier = 1;
if (key == NOTE_C)
key = NOTE_D;
else if (key == NOTE_D)
key = NOTE_E;
else if (key == NOTE_E)
key = NOTE_F;
else if (key == NOTE_F)
key = NOTE_G;
else if (key == NOTE_G)
key = NOTE_A;
else if (key == NOTE_A)
key = NOTE_B;
else if (key == NOTE_B)
key = NOTE_C;
}
//Timer interrupt function
void changePitch()
{
octave_multiplier = octave_multiplier * 2;
if (octave_multiplier > 16) octave_multiplier = 1;
tone(SPEAKER, key*octave_multiplier);
}
void loop()
{
Serial.print("Key: ");
Serial.print(key);
Serial.print(" Multiplier ");
Serial.print(octave_multiplier);
Serial.print(" Frequency ");
Serial.println(key*octave_multiplier);
delay(100);
}
Blinking LED with Timer Interrupts:
//Using Timer Interrupts with the Arduino
#include <TimerOne.h>
const int LED=13;
void setup()
{
pinMode(LED, OUTPUT);
Timer1.initialize(1000000); //Set a timer of length 1000000
//microseconds (1 second)
Timer1.attachInterrupt(blinky); //Runs "blinky" on each
//time interrupt
}
void loop()
{
// put your main code here, to run repeatedly:
}
//Timer interrupt function
void blinky()
{
digitalWrite(LED, !digitalRead(LED)); //Toggle LED State
}
Basic Speaker with Hardware and Timer Interrupts:
//Use Hardware and Timer Interrupts for Fun with Sound
//Include the TimerOne library
#include <TimerOne.h>
//Button pins
const int BUTTON_INT =0;
const int SPEAKER =12;
//Music keys
#define NOTE_C 65
#define NOTE_D 73
#define NOTE_E 82
#define NOTE_F 87
#define NOTE_G 98
#define NOTE_A 110
#define NOTE_B 123
//Volatile variables can change inside interrupts
volatile int key = NOTE_C;
volatile int octave_multiplier = 1;
void setup()
{
//Set up Serial
Serial.begin(9600);
pinMode(SPEAKER, OUTPUT);
//The pin is inverted, so we want to look at the rising edge
attachInterrupt(BUTTON_INT, changeKey, RISING);
//Set up timer interrupt
Timer1.initialize(500000); //Set a timer of length .5 seconds
Timer1.attachInterrupt(changePitch); //Runs "blinky" on each
//time interrupt
}
void changeKey()
{
octave_multiplier = 1;
if (key == NOTE_C)
key = NOTE_D;
else if (key == NOTE_D)
key = NOTE_E;
else if (key == NOTE_E)
key = NOTE_F;
else if (key == NOTE_F)
key = NOTE_G;
else if (key == NOTE_G)
key = NOTE_A;
else if (key == NOTE_A)
key = NOTE_B;
else if (key == NOTE_B)
key = NOTE_C;
}
//Timer interrupt function
void changePitch()
{
octave_multiplier = octave_multiplier * 2;
if (octave_multiplier > 16) octave_multiplier = 1;
tone(SPEAKER, key*octave_multiplier);
}
void loop()
{
Serial.print("Key: ");
Serial.print(key);
Serial.print(" Multiplier ");
Serial.print(octave_multiplier);
Serial.print(" Frequency ");
Serial.println(key*octave_multiplier);
delay(100);
}
Friday, December 15, 2017
Day 20 - Dynamic Data Structures
CLASSWORK:
Getting the linked list nodal functions to work:
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/* This program tests the functions to insert and delete */
/* items in a linked list. */
#include <stdio.h>
#include <stdlib.h>
/* Define structure to represent a node in a linked list. */
struct node
{
int data;
struct node *link;
};
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/* This function returns a value of one if the linked list */
/* is empty. */
int empty(struct node *head)
{
/* Declare variables. */
int k = 0;
/* Determine if the list is empty. */
if (head == NULL)
k = 1;
/* Return integer. */
return k;
}
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/* This function prints a linked list. */
void print_list(struct node *head)
{
/* Declare variables. */
struct node *next;
/* Print linked list. */
if (empty(head))
printf("Empty list \n");
else
{
printf("List Values: \n");
next = head;
while (next->link != NULL)
{
printf("%d \n", next->data);
next = next->link;
}
printf("%d \n", next->data);
}
/* Void return. */
return;
}
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/* This function inserts a new node in a linked list. */
void insert(struct node **ptr_to_head, struct node *nw)
{
/* Declare variables and function prototypes. */
struct node **next;
/* Check for insert to empty list. */
if (empty(*ptr_to_head))
*ptr_to_head = nw;
else
/* Traverse list to find location for insert. */
{
next = ptr_to_head;
while (((*next)->data < nw->data) &&
((*next)->link != NULL))
next = &(*next)->link;
/* Check for duplicate data. */
if ((*next)->data == nw->data)
printf("Node already in list. \n");
else
/* Check for insert after last node. */
if ((*next)->data < nw->data)
(*next)->link = nw;
else
{
nw->link = *next;
*next = nw;
}
}
/* Void return. */
return;
}
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/* This function deletes a node from a linked list. */
void remove_e(struct node **ptr_to_head, int old)
{
/* Declare variables and function prototypes. */
struct node *next, *last, *hold, *head;
/* Check for delete to empty list. */
head = *ptr_to_head;
if (empty(head))
printf("Empty list. \n");
else
/* Check for deletion of first node. */
{
if (head->data == old)
{
/* Delete first node. */
hold = head;
*ptr_to_head = head->link;
free(hold);
}
else
/* Traverse list to find old node. */
{
next = head->link;
last = head;
while ((next->data < old) && (next->link != NULL))
{
last = next;
next = next->link;
}
/* Delete node if found. */
if (next->data == old)
{
hold = last;
last->link = next->link;
free(hold);
}
else
printf("Value %d not in list. \n", old);
}
}
/* Void return. */
return;
}
int main(void)
{
/* Declare variables and function prototypes. */
int k = 0, old, value;
struct node *head, *next, *previous, *nw, **ptr_to_head = &head;
void insert(struct node **ptr_to_head, struct node *nw);
void remove_e(struct node **ptr_to_head, int n);
int empty(struct node *head);
void print_list(struct node *head);
/* Generate and print a linked list with five nodes. */
head = (struct node *)malloc(sizeof(struct node));
next = head;
for (k = 1; k <= 5; k++)
{
next->data = k * 5;
next->link = (struct node *)malloc(sizeof(struct node));
previous = next;
next = next->link;
}
previous->link = NULL;
print_list(head);
/* Allow user to insert or delete nodes in the list. */
while (k != 2)
{
printf("Enter 0 to delete node, 1 to add node, 2 to quit. \n");
scanf("%d", &k);
if (k == 0)
{
printf("Enter data value to delete: \n");
scanf("%d",&old);
remove_e(ptr_to_head, old);
print_list(head);
}
else
if (k == 1)
{
printf("Enter data value to add: \n");
scanf("%d",&value);
nw = (struct node *)malloc(sizeof(struct node));
nw->data = value;
nw->link = NULL;
insert(ptr_to_head, nw);
print_list(head);
}
}
/* Exit program. */
return 0;
}
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
HOMEWORK:
Quick Sort Program:
#include <stdio.h>
int partition(int a[], int p, int r)
{
int i, j, pivot, temp;
pivot = a[p]; //pivot value is a[initial] = 25
i = p; // i = 0 (initially)
j = r; // j = 7 (initially)
while(1) //while previous statements are true
{ //Start at a[initial]
while(a[i] < pivot && a[i] != pivot) //if the array element is less than & not equal to the pivot...
i++; //...increment i by one, moving to next array element
while(a[j] > pivot && a[j] != pivot) //Starting at a[final], if the array element is greater than &
j--; //not equal to the pivot, then decrement j by one
if(i < j) //if i is less than j...
{ //...and neither of the previous conditions are met...
temp = a[i]; //Set the variable temp equal to the ith array element
a[i] = a[j]; //Set the ith array element equal to the jth array element
a[j] = temp; //Set the jth array element equal to the variable temp
} //In other words... we swap the values a[i] and a[j]
else //if (i !< j) && other conditions are not met...
{
return j; //print out j
}
}
}
/*--------------------------------------------------*/
/* a[] is the array, p is starting index, that is 0,
and r is the last index of array. */
void quicksort(int a[], int p, int r)
{
//printf("Starting Index: %i Ending Index: %i \n", p, r);
if(p < r)
{
int q = partition(a, p, r);
quicksort(a, p, q);
quicksort(a, q+1, r);
}
}
void display(int a[], int size)
{
for (int k=0; k<size; k++)
{
printf("%i ", a[k]);
}
printf("\n");
}
int main(void)
{
int a[] = {25,52,37,63,14,17,8,6};
int n = sizeof(a)/sizeof(a[0]);
printf("Input Array: \t");
display(a, n);
quicksort(a, 0, n-1);
printf("Output Array: \t");
display(a, n);
return 0;
}
Getting the linked list nodal functions to work:
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/* This program tests the functions to insert and delete */
/* items in a linked list. */
#include <stdio.h>
#include <stdlib.h>
/* Define structure to represent a node in a linked list. */
struct node
{
int data;
struct node *link;
};
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/* This function returns a value of one if the linked list */
/* is empty. */
int empty(struct node *head)
{
/* Declare variables. */
int k = 0;
/* Determine if the list is empty. */
if (head == NULL)
k = 1;
/* Return integer. */
return k;
}
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/* This function prints a linked list. */
void print_list(struct node *head)
{
/* Declare variables. */
struct node *next;
/* Print linked list. */
if (empty(head))
printf("Empty list \n");
else
{
printf("List Values: \n");
next = head;
while (next->link != NULL)
{
printf("%d \n", next->data);
next = next->link;
}
printf("%d \n", next->data);
}
/* Void return. */
return;
}
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/* This function inserts a new node in a linked list. */
void insert(struct node **ptr_to_head, struct node *nw)
{
/* Declare variables and function prototypes. */
struct node **next;
/* Check for insert to empty list. */
if (empty(*ptr_to_head))
*ptr_to_head = nw;
else
/* Traverse list to find location for insert. */
{
next = ptr_to_head;
while (((*next)->data < nw->data) &&
((*next)->link != NULL))
next = &(*next)->link;
/* Check for duplicate data. */
if ((*next)->data == nw->data)
printf("Node already in list. \n");
else
/* Check for insert after last node. */
if ((*next)->data < nw->data)
(*next)->link = nw;
else
{
nw->link = *next;
*next = nw;
}
}
/* Void return. */
return;
}
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/* This function deletes a node from a linked list. */
void remove_e(struct node **ptr_to_head, int old)
{
/* Declare variables and function prototypes. */
struct node *next, *last, *hold, *head;
/* Check for delete to empty list. */
head = *ptr_to_head;
if (empty(head))
printf("Empty list. \n");
else
/* Check for deletion of first node. */
{
if (head->data == old)
{
/* Delete first node. */
hold = head;
*ptr_to_head = head->link;
free(hold);
}
else
/* Traverse list to find old node. */
{
next = head->link;
last = head;
while ((next->data < old) && (next->link != NULL))
{
last = next;
next = next->link;
}
/* Delete node if found. */
if (next->data == old)
{
hold = last;
last->link = next->link;
free(hold);
}
else
printf("Value %d not in list. \n", old);
}
}
/* Void return. */
return;
}
int main(void)
{
/* Declare variables and function prototypes. */
int k = 0, old, value;
struct node *head, *next, *previous, *nw, **ptr_to_head = &head;
void insert(struct node **ptr_to_head, struct node *nw);
void remove_e(struct node **ptr_to_head, int n);
int empty(struct node *head);
void print_list(struct node *head);
/* Generate and print a linked list with five nodes. */
head = (struct node *)malloc(sizeof(struct node));
next = head;
for (k = 1; k <= 5; k++)
{
next->data = k * 5;
next->link = (struct node *)malloc(sizeof(struct node));
previous = next;
next = next->link;
}
previous->link = NULL;
print_list(head);
/* Allow user to insert or delete nodes in the list. */
while (k != 2)
{
printf("Enter 0 to delete node, 1 to add node, 2 to quit. \n");
scanf("%d", &k);
if (k == 0)
{
printf("Enter data value to delete: \n");
scanf("%d",&old);
remove_e(ptr_to_head, old);
print_list(head);
}
else
if (k == 1)
{
printf("Enter data value to add: \n");
scanf("%d",&value);
nw = (struct node *)malloc(sizeof(struct node));
nw->data = value;
nw->link = NULL;
insert(ptr_to_head, nw);
print_list(head);
}
}
/* Exit program. */
return 0;
}
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
HOMEWORK:
Quick Sort Program:
#include <stdio.h>
int partition(int a[], int p, int r)
{
int i, j, pivot, temp;
pivot = a[p]; //pivot value is a[initial] = 25
i = p; // i = 0 (initially)
j = r; // j = 7 (initially)
while(1) //while previous statements are true
{ //Start at a[initial]
while(a[i] < pivot && a[i] != pivot) //if the array element is less than & not equal to the pivot...
i++; //...increment i by one, moving to next array element
while(a[j] > pivot && a[j] != pivot) //Starting at a[final], if the array element is greater than &
j--; //not equal to the pivot, then decrement j by one
if(i < j) //if i is less than j...
{ //...and neither of the previous conditions are met...
temp = a[i]; //Set the variable temp equal to the ith array element
a[i] = a[j]; //Set the ith array element equal to the jth array element
a[j] = temp; //Set the jth array element equal to the variable temp
} //In other words... we swap the values a[i] and a[j]
else //if (i !< j) && other conditions are not met...
{
return j; //print out j
}
}
}
/*--------------------------------------------------*/
/* a[] is the array, p is starting index, that is 0,
and r is the last index of array. */
void quicksort(int a[], int p, int r)
{
//printf("Starting Index: %i Ending Index: %i \n", p, r);
if(p < r)
{
int q = partition(a, p, r);
quicksort(a, p, q);
quicksort(a, q+1, r);
}
}
void display(int a[], int size)
{
for (int k=0; k<size; k++)
{
printf("%i ", a[k]);
}
printf("\n");
}
int main(void)
{
int a[] = {25,52,37,63,14,17,8,6};
int n = sizeof(a)/sizeof(a[0]);
printf("Input Array: \t");
display(a, n);
quicksort(a, 0, n-1);
printf("Output Array: \t");
display(a, n);
return 0;
}
Lab 19 - Data Logging with SD Cards
CLASSWORK:
Basic SD Card Reader:
// Write to SD card
#include <SD.h>
//Set by default for the SD library
//MOSI = pin 11
//MISO = pin 12
//SCLK = pin 13
//We alwasy need to set the CS Pin
const int CS_PIN = 10;
void setup() {
Serial.begin(9600);
Serial.println("Initializing Card");
//CS pin is an output
pinMode(CS_PIN, OUTPUT);
if (!SD.begin(CS_PIN))
{
Serial.println("Card failure");
return;
}
Serial.println("Card Ready");
}
void loop() {
long timeStamp = millis();
String dataString = "Hello There!";
//Open a file and write to it.
File dataFile = SD.open("Sensor1.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(timeStamp);
dataFile.print(",");
dataFile.println(dataString);
dataFile.close(); //Data isnt actually written until we close the connection!
//Print same thing to the screen for debugging
Serial.print(timeStamp);
Serial.print(",");
Serial.println(dataString);
}
else
{
Serial.print("Couldn't open log file");
}
delay(5000);
}
SD Card Reader with Command File:
//SD read and write
#include <SD.h>
//Set by default for the SD card library
const int CS_PIN = 10;
const int POW_PIN = 8;
//Default rate of 5 seconds
int refresh_rate = 5000;
void setup() {
Serial.begin(9600);
Serial.println("Initializing Card");
//CS pin is an output
pinMode(CS_PIN, OUTPUT);
digitalWrite(POW_PIN, HIGH);
if (!SD.begin(CS_PIN))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
//Read the configuration info (speed.txt)
File commandFile = SD.open("speed.txt");
if (commandFile)
{
Serial.println("Reading Command File");
while(commandFile.available())
{
refresh_rate = commandFile.parseInt();
}
Serial.print("Refresh Rate = ");
Serial.print(refresh_rate);
Serial.println("ms");
commandFile.close(); //Close the file when finished
File dataFile = SD.open("sensor1.csv", FILE_WRITE);
dataFile.println("-----New Data Set-----");
dataFile.println("Time,TempC,TempF");
dataFile.close();
Serial.println("-----New Data Set-----");
Serial.println("Time\tTempC\tTempF");
}
else
{
Serial.println("Could not read command file.");
return;
}
}
void loop() {
long timeStamp = millis();
String dataString = "Hello There!";
//Open a file and write to it.
File dataFile = SD.open("sensor1.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(timeStamp);
dataFile.print(",");
dataFile.println(dataString);
dataFile.close(); //Data isnt actually written until we close the connection!
//Print same thing to the screen for debugging
Serial.print(timeStamp);
Serial.print(",");
Serial.println(dataString);
}
else
{
Serial.println("Couldn't open log file");
}
delay(refresh_rate);
}
HOMEWORK:
SD Card Reader with Temperature Sensor:
//SD read and write
#include <SD.h>
//Set by default for the SD card library
const int CS_PIN = 10;
const int POW_PIN = 8;
//Default rate of 5 seconds
int refresh_rate = 5000;
/*-----Temperature Sensor Constants------*/
const int temp_pin = A0;
int temp_reading = 0;
float voltage = 0; // setup some variables
float celsius = 0;
float farenheit ;
void setup() {
Serial.begin(9600);
Serial.println("Initializing Card");
//CS pin is an output
pinMode(CS_PIN, OUTPUT);
digitalWrite(POW_PIN, HIGH);
if (!SD.begin(CS_PIN))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
//Read the configuration info (speed.txt)
File commandFile = SD.open("speed.txt");
if (commandFile)
{
Serial.println("Reading Command File");
while(commandFile.available())
{
refresh_rate = commandFile.parseInt();
}
Serial.print("Refresh Rate = ");
Serial.print(refresh_rate);
Serial.println("ms");
commandFile.close(); //Close the file when finished
File dataFile = SD.open("sensor1.csv", FILE_WRITE);
dataFile.println("-----New Data Set-----");
dataFile.println("Time,TempC,TempF");
dataFile.close();
Serial.println("-----New Data Set-----");
Serial.println("Time\tTempC\tTempF");
}
else
{
Serial.println("Could not read command file.");
return;
}
}
void loop() {
long timeStamp = millis();
while (timeStamp < 60040)
{
long timeStamp = millis();
temperature_function();
String dataString = "Hello There!";
//Open a file and write to it.
File dataFile = SD.open("sensor1.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(timeStamp*.001);
dataFile.print(",");
dataFile.print(celsius);
dataFile.print(",");
dataFile.println(farenheit);
// dataFile.println(dataString);
dataFile.close(); //Data isnt actually written until we close the connection!
//Print same thing to the screen for debugging
Serial.print(timeStamp*.001);
Serial.print("\t");
Serial.print(celsius);
Serial.print("\t");
Serial.println(farenheit);
//Serial.println(dataString);
}
else
{
Serial.println("Couldn't open log file");
}
delay(refresh_rate);
}
}
void temperature_function()
{
temp_reading = analogRead(temp_pin);
voltage = ((float)temp_reading*5000)/1024; // convert sensor value to millivolts
voltage = voltage-500; // remove voltage offset
celsius = voltage/10; // convert millivolts to Celsius
farenheit = ((celsius*1.8)+32);
// Serial.print(temp_reading);
// Serial.print(" Temperature: "); // these four lines send the temp to the port
// Serial.print(celsius);
// Serial.print(" degrees C ");
// Serial.print(farenheit);
//Serial.println(" degrees F");
// delay(1000);
}
Temperature Sensor with SD Card Reader and Interrupts:
//Use Hardware and Timer Interrupts for Fun with Sound
//Include the TimerOne library
#include <TimerOne.h>
#include <SD.h>
//Button pins
const int BUTTON_INT = 0;
const int BUTTON2_INT =1;
//Set by default for the SD card library
const int CS_PIN = 10;
const int POW_PIN = 8;
//Volatile variables can change inside interrupts
volatile int refresh_rate = 0; // in ms
volatile long timeStamp;
/*-----Temperature Sensor Constants------*/
const int temp_pin = A0;
int temp_reading = 0;
float voltage = 0; // setup some variables
float celsius = 0;
float farenheit ;
void setup()
{
//Set up Serial
Serial.begin(9600);
Serial.println("Initializing Card");
//CS pin is an output
pinMode(CS_PIN, OUTPUT);
pinMode(POW_PIN, OUTPUT);
digitalWrite(POW_PIN, HIGH);
if (!SD.begin(CS_PIN))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
//Read the configuration info (speed.txt)
File commandFile = SD.open("speed.txt");
if (commandFile)
{
Serial.println("Reading Command File");
while(commandFile.available())
{
refresh_rate = commandFile.parseInt();
}
Serial.print("Refresh Rate = ");
Serial.print(refresh_rate);
Serial.println("ms");
commandFile.close(); //Close the file when finished
File dataFile = SD.open("sensor1.csv", FILE_WRITE);
dataFile.println("-----New Data Set-----");
dataFile.println("Time,TempC,TempF");
dataFile.close();
Serial.println("-----New Data Set-----");
Serial.println("Time\tTempC\tTempF");
}
else
{
Serial.println("Could not read command file.");
return;
}
//The pin is inverted, so we want to look at the rising edge
attachInterrupt(BUTTON_INT, increaseDelay, RISING);
//attachInterrupt(BUTTON2_INT, decreaseDelay, RISING);
//Set up timer interrupt
Timer1.initialize(refresh_rate*1000); //Set a timer of length 1 seconds
Timer1.attachInterrupt(printData); //Runs "temperature function" on each
//time interrupt
}
void increaseDelay()
{
if (refresh_rate >= 18)
refresh_rate++;
else
refresh_rate = 18;
Serial.print("New Refresh Rate:");
Serial.println(refresh_rate);
}
void decreaseDelay()
{
if (refresh_rate > 18)
refresh_rate--;
else
refresh_rate = 18;
Serial.print("Decrease:");
Serial.println(refresh_rate);
}
void loop() {
temperature_function();
//Open a file and write to it.
File dataFile = SD.open("sensor1.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(timeStamp);
dataFile.print(",");
dataFile.print(celsius);
dataFile.print(",");
dataFile.println(farenheit);
dataFile.close(); //Data isnt actually written until we close the connection!
//Print same thing to the screen for debugging
Serial.print(timeStamp);
Serial.print("\t");
Serial.print(celsius);
Serial.print("\t");
Serial.println(farenheit);
//Serial.println(dataString);
}
else
{
Serial.println("Couldn't open log file");
}
}
void printData()
{
timeStamp = millis();
}
void temperature_function()
{
temp_reading = analogRead(temp_pin);
voltage = ((float)temp_reading*5000)/1024; // convert sensor value to millivolts
voltage = voltage-500; // remove voltage offset
celsius = voltage/10; // convert millivolts to Celsius
farenheit = ((celsius*1.8)+32);
}
Basic SD Card Reader:
// Write to SD card
#include <SD.h>
//Set by default for the SD library
//MOSI = pin 11
//MISO = pin 12
//SCLK = pin 13
//We alwasy need to set the CS Pin
const int CS_PIN = 10;
void setup() {
Serial.begin(9600);
Serial.println("Initializing Card");
//CS pin is an output
pinMode(CS_PIN, OUTPUT);
if (!SD.begin(CS_PIN))
{
Serial.println("Card failure");
return;
}
Serial.println("Card Ready");
}
void loop() {
long timeStamp = millis();
String dataString = "Hello There!";
//Open a file and write to it.
File dataFile = SD.open("Sensor1.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(timeStamp);
dataFile.print(",");
dataFile.println(dataString);
dataFile.close(); //Data isnt actually written until we close the connection!
//Print same thing to the screen for debugging
Serial.print(timeStamp);
Serial.print(",");
Serial.println(dataString);
}
else
{
Serial.print("Couldn't open log file");
}
delay(5000);
}
SD Card Reader with Command File:
//SD read and write
#include <SD.h>
//Set by default for the SD card library
const int CS_PIN = 10;
const int POW_PIN = 8;
//Default rate of 5 seconds
int refresh_rate = 5000;
void setup() {
Serial.begin(9600);
Serial.println("Initializing Card");
//CS pin is an output
pinMode(CS_PIN, OUTPUT);
digitalWrite(POW_PIN, HIGH);
if (!SD.begin(CS_PIN))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
//Read the configuration info (speed.txt)
File commandFile = SD.open("speed.txt");
if (commandFile)
{
Serial.println("Reading Command File");
while(commandFile.available())
{
refresh_rate = commandFile.parseInt();
}
Serial.print("Refresh Rate = ");
Serial.print(refresh_rate);
Serial.println("ms");
commandFile.close(); //Close the file when finished
File dataFile = SD.open("sensor1.csv", FILE_WRITE);
dataFile.println("-----New Data Set-----");
dataFile.println("Time,TempC,TempF");
dataFile.close();
Serial.println("-----New Data Set-----");
Serial.println("Time\tTempC\tTempF");
}
else
{
Serial.println("Could not read command file.");
return;
}
}
void loop() {
long timeStamp = millis();
String dataString = "Hello There!";
//Open a file and write to it.
File dataFile = SD.open("sensor1.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(timeStamp);
dataFile.print(",");
dataFile.println(dataString);
dataFile.close(); //Data isnt actually written until we close the connection!
//Print same thing to the screen for debugging
Serial.print(timeStamp);
Serial.print(",");
Serial.println(dataString);
}
else
{
Serial.println("Couldn't open log file");
}
delay(refresh_rate);
}
HOMEWORK:
SD Card Reader with Temperature Sensor:
//SD read and write
#include <SD.h>
//Set by default for the SD card library
const int CS_PIN = 10;
const int POW_PIN = 8;
//Default rate of 5 seconds
int refresh_rate = 5000;
/*-----Temperature Sensor Constants------*/
const int temp_pin = A0;
int temp_reading = 0;
float voltage = 0; // setup some variables
float celsius = 0;
float farenheit ;
void setup() {
Serial.begin(9600);
Serial.println("Initializing Card");
//CS pin is an output
pinMode(CS_PIN, OUTPUT);
digitalWrite(POW_PIN, HIGH);
if (!SD.begin(CS_PIN))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
//Read the configuration info (speed.txt)
File commandFile = SD.open("speed.txt");
if (commandFile)
{
Serial.println("Reading Command File");
while(commandFile.available())
{
refresh_rate = commandFile.parseInt();
}
Serial.print("Refresh Rate = ");
Serial.print(refresh_rate);
Serial.println("ms");
commandFile.close(); //Close the file when finished
File dataFile = SD.open("sensor1.csv", FILE_WRITE);
dataFile.println("-----New Data Set-----");
dataFile.println("Time,TempC,TempF");
dataFile.close();
Serial.println("-----New Data Set-----");
Serial.println("Time\tTempC\tTempF");
}
else
{
Serial.println("Could not read command file.");
return;
}
}
void loop() {
long timeStamp = millis();
while (timeStamp < 60040)
{
long timeStamp = millis();
temperature_function();
String dataString = "Hello There!";
//Open a file and write to it.
File dataFile = SD.open("sensor1.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(timeStamp*.001);
dataFile.print(",");
dataFile.print(celsius);
dataFile.print(",");
dataFile.println(farenheit);
// dataFile.println(dataString);
dataFile.close(); //Data isnt actually written until we close the connection!
//Print same thing to the screen for debugging
Serial.print(timeStamp*.001);
Serial.print("\t");
Serial.print(celsius);
Serial.print("\t");
Serial.println(farenheit);
//Serial.println(dataString);
}
else
{
Serial.println("Couldn't open log file");
}
delay(refresh_rate);
}
}
void temperature_function()
{
temp_reading = analogRead(temp_pin);
voltage = ((float)temp_reading*5000)/1024; // convert sensor value to millivolts
voltage = voltage-500; // remove voltage offset
celsius = voltage/10; // convert millivolts to Celsius
farenheit = ((celsius*1.8)+32);
// Serial.print(temp_reading);
// Serial.print(" Temperature: "); // these four lines send the temp to the port
// Serial.print(celsius);
// Serial.print(" degrees C ");
// Serial.print(farenheit);
//Serial.println(" degrees F");
// delay(1000);
}
Temperature Sensor with SD Card Reader and Interrupts:
//Use Hardware and Timer Interrupts for Fun with Sound
//Include the TimerOne library
#include <TimerOne.h>
#include <SD.h>
//Button pins
const int BUTTON_INT = 0;
const int BUTTON2_INT =1;
//Set by default for the SD card library
const int CS_PIN = 10;
const int POW_PIN = 8;
//Volatile variables can change inside interrupts
volatile int refresh_rate = 0; // in ms
volatile long timeStamp;
/*-----Temperature Sensor Constants------*/
const int temp_pin = A0;
int temp_reading = 0;
float voltage = 0; // setup some variables
float celsius = 0;
float farenheit ;
void setup()
{
//Set up Serial
Serial.begin(9600);
Serial.println("Initializing Card");
//CS pin is an output
pinMode(CS_PIN, OUTPUT);
pinMode(POW_PIN, OUTPUT);
digitalWrite(POW_PIN, HIGH);
if (!SD.begin(CS_PIN))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
//Read the configuration info (speed.txt)
File commandFile = SD.open("speed.txt");
if (commandFile)
{
Serial.println("Reading Command File");
while(commandFile.available())
{
refresh_rate = commandFile.parseInt();
}
Serial.print("Refresh Rate = ");
Serial.print(refresh_rate);
Serial.println("ms");
commandFile.close(); //Close the file when finished
File dataFile = SD.open("sensor1.csv", FILE_WRITE);
dataFile.println("-----New Data Set-----");
dataFile.println("Time,TempC,TempF");
dataFile.close();
Serial.println("-----New Data Set-----");
Serial.println("Time\tTempC\tTempF");
}
else
{
Serial.println("Could not read command file.");
return;
}
//The pin is inverted, so we want to look at the rising edge
attachInterrupt(BUTTON_INT, increaseDelay, RISING);
//attachInterrupt(BUTTON2_INT, decreaseDelay, RISING);
//Set up timer interrupt
Timer1.initialize(refresh_rate*1000); //Set a timer of length 1 seconds
Timer1.attachInterrupt(printData); //Runs "temperature function" on each
//time interrupt
}
void increaseDelay()
{
if (refresh_rate >= 18)
refresh_rate++;
else
refresh_rate = 18;
Serial.print("New Refresh Rate:");
Serial.println(refresh_rate);
}
void decreaseDelay()
{
if (refresh_rate > 18)
refresh_rate--;
else
refresh_rate = 18;
Serial.print("Decrease:");
Serial.println(refresh_rate);
}
void loop() {
temperature_function();
//Open a file and write to it.
File dataFile = SD.open("sensor1.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(timeStamp);
dataFile.print(",");
dataFile.print(celsius);
dataFile.print(",");
dataFile.println(farenheit);
dataFile.close(); //Data isnt actually written until we close the connection!
//Print same thing to the screen for debugging
Serial.print(timeStamp);
Serial.print("\t");
Serial.print(celsius);
Serial.print("\t");
Serial.println(farenheit);
//Serial.println(dataString);
}
else
{
Serial.println("Couldn't open log file");
}
}
void printData()
{
timeStamp = millis();
}
void temperature_function()
{
temp_reading = analogRead(temp_pin);
voltage = ((float)temp_reading*5000)/1024; // convert sensor value to millivolts
voltage = voltage-500; // remove voltage offset
celsius = voltage/10; // convert millivolts to Celsius
farenheit = ((celsius*1.8)+32);
}
Subscribe to:
Posts (Atom)