Sunday, January 22, 2012

Lab 1 Exercises

Below is a collection of the major programs we wrote in our first lab session (January 19, 2012). Most of them were basic exercises; we will keep editing the final two parts of this lab as we learn more and think of better ways of structuring them.

(Pictures coming soon!)


"Try making a program where x changes at each iteration (x = 1, 2, 3, 4... etc.) Have it increment by 1, 2, 10, or some other number. Try a large number such as 100. What happens?"

int x = 0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
// Serial.print("my number is ");
Serial.print(x);
Serial.print("\n");
x += 100;
delay(200);
}

We commented out the first printout as suggested and ran our code several times, each time with a larger increment (in the code above, the increment is 100; we ran it with increments of 1, 5, 100, and 1000). With an increment of 1000 we were able to quickly see the effects of rollover-- since the max capacity of x is -32,768 to 32,767 as an int variable, after 32,000 the printed-out numbers became negative but continued to increase by positive increments of 1000.


"Write a program that blinks the light on and off. Note that the pin number can be a variable..."

int pin_number = 6;

void setup()
{
pinMode(pin_number,OUTPUT);
}

void loop()
{
digitalWrite(pin_number,HIGH);
delay(200);
digitalWrite(pin_number,LOW);
delay(200);
}

"Write a program so that the LED flashes at a faster and faster (or slower and slower) rate."

Slower:


int pin_number = 6;
int delay_time = 200;

void setup()
{
pinMode(pin_number,OUTPUT);
}

void loop()
{
digitalWrite(pin_number,HIGH);
delay(delay_time);
digitalWrite(pin_number,LOW);
delay(delay_time);
delay_time += 100;

if(delay_time >= 5000)
{
delay_time = 200;
}
}

This code resets the interval once it becomes very long (here, greater than 5 seconds).

Faster:


int pin_number = 6;
int delay_time = 5000;

void setup()
{
pinMode(pin_number,OUTPUT);
}

void loop()
{
digitalWrite(pin_number,HIGH);
delay(delay_time);
digitalWrite(pin_number,LOW);
delay(delay_time);
delay_time -= 100;
if(delay_time <= 200)
{
delay_time = 5000;
}
}

...this one resets the delay interval to a high number when it becomes very low.

"Make 2 LEDs flash in alternation."

int pin_number1 = 6;
int pin_number2 = 7;
int delay_time = 500;

void setup()
{
pinMode(pin_number1,OUTPUT);
pinMode(pin_number2,OUTPUT);
}

void loop()
{
digitalWrite(pin_number1,HIGH);
digitalWrite(pin_number2,LOW);
delay(delay_time);
digitalWrite(pin_number1,LOW);
digitalWrite(pin_number2,HIGH);
delay(delay_time);
}

"Monitor the operation of a pushbutton so that when it is pressed, something is written on the SerialMonitor."

int pin_number = 4;
int delay_time = 500;
int buttonState;
int buttonStatep = 0;

void setup()
{
Serial.begin(9600);
pinMode(pin_number,INPUT);
}

void loop()
{
buttonState = digitalRead(pin_number);

if(buttonState != buttonStatep)
{
Serial.print("Button State is ");
Serial.println(buttonState);
}

buttonStatep = buttonState;
}

This code is written to notify us when there is any change in the pushbutton's state, whether it's going from being unpushed to pushed or vice versa.

"Write a program so that when the button is pushed, an LED goes on."

int pin_number1 = 6;
int pin_number2 = 4;
int buttonState;
int buttonStatep = 0;

void setup()
{
Serial.begin(9600);
pinMode(pin_number1,OUTPUT);
pinMode(pin_number2,INPUT);
}

void loop()
{
buttonState = digitalRead(pin_number2);

if(buttonState != buttonStatep)
{
Serial.print("Button State is ");
Serial.println(buttonState);
}

buttonStatep = buttonState;
if(buttonState == 0)
{
digitalWrite(pin_number1,HIGH);
}

if(buttonState == 1)
{
digitalWrite(pin_number1,LOW);
}
}

Again, this notifies us whenever the state of the pushbutton changes. We just added a portion to change the state of the LED's pin based on whether the input from the pushbutton's pin is pushed (0) or unpushed (1).


"Write a program that:
1. writes "Hello BE470 student!" to the LCD.
2. Waits 2 seconds and erases the LCD.
3. Counts down from 9 to 0 at 1-second intervals.
4. Then writes, "Blast off!"
5. And stops."

#include <LiquidCrystal.h>

int count = 9;
LiquidCrystal lcd(12,11,13,10,9,8);

void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);

lcd.print("Hello");
lcd.setCursor(0,1);
lcd.print(" BE470 Student!");

delay(2000);
lcd.clear();

while(count>=0)
{
lcd.print(count);
delay(1000);
count--;
lcd.clear();
}
lcd.setCursor(0,1);
lcd.print("Blast off!");
}

void loop()
{}

We chose to incorporate the "stop" after the final printout by leaving the loop portion empty and working in void setup() instead. One way you could move all of the actions to void loop() would be to write while(1) {} at the end, but this seemed silly so we decided the cleaner approach would be to keep it the way it is above.


Our first medical device! :)

The first task of this portion of the lab was to measure time in milliseconds via two methods: first by using delay(), and then by using millis().

We tried various methods of using delay() to keep track of time, e.g. something like:

...
delay(interval);
Time += interval;
...

where the interval was previously defined as some value (in milliseconds) and the time variable would increase after each delay.

We learned several things from trying these approaches, one of the most important being the following:

Since the delay() function prevents almost all other activity from occurring during the delay period, using it to keep track of time could seriously limit the function of our device. The device would be unable to carry out most operations except between time intervals, and the more it carries out during the time between intervals (before the loop goes back and initiates another delay), the less accurate the timekeeping portion of the program would become over time.



"Make a reaction-time measuring device. Have the microcontroller turn an LED on, and then measure and display how long it takes for a subject to press a pushbutton. Display the reaction time on the LCD. (Extra credit: set up the device so that it conducts a series of 10 reaction-time tests and then presents the results in some graphical format, either using the serial terminal or the LCD.)"


#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,13,10,9,8);


int pin_number;
unsigned long time1;
unsigned long time2;
int pin_number3;
int buttonState;
int buttonState_p;
unsigned long delay_time[10];
int a;
int q;


void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2);  
  pin_number = 6;  
  pin_number3 = 4;
  a = 0;            
  q = 0;
  buttonState_p = 1;      
  pinMode(pin_number,OUTPUT);
  pinMode(pin_number3,INPUT);
  time1 = millis();    
}


void loop()
{
  if(q==0){lcd.print("Start:"); q = 1;}
  buttonState = digitalRead(pin_number3);
  digitalWrite(pin_number,HIGH);    
      Serial.print("Button State is ");
      Serial.print(buttonState);  
      Serial.print("\n");  
      //Serial.print(time1);  
      //Serial.print(time2);    
  if(buttonState == 0 && buttonState!= buttonState_p){
    digitalWrite(pin_number,LOW);            
    time2 = millis();                        
    delay_time[a] = time2 - time1;          
    a++;                                  
    if(a == 10){                      
      lcd.clear();                            
      lcd.print("Finished!");                
    }
    delay(2000+random(500,2000));          
    time1 = millis();                      
  }
  buttonState_p = buttonState;  
  if(a == 10)
 {                
    lcd.clear();                
    for(int d=0; d<=9; d++){      
      lcd.setCursor(0,0);            
      lcd.print(d+1);              
      lcd.setCursor(0,1);            
      lcd.print(delay_time[d]);    
      delay(3000);              
      lcd.clear();              
      if(d==9){a = 0; q = 0;}  
    }
  }
}


IT WORKS ahahaha :)

Winslow incorporated extra practical features like pseudo-randomized time intervals before the next time the LED comes on. That way, the person being tested can't use time-counting to deflate their reaction time measurements. 


"Make an activity/exercise monitoring device. Imagine that the pushbutton is a switch on the bottom of the foot. The idea is to monitor the number of steps taken in a certain time interval and alert the user if the number of steps is fewer than a desired number. "


#include <LiquidCrystal.h>

// Initialize global variables
LiquidCrystal lcd(12,11,13,10,9,8);
int pin_number;
int pin_number3;
int buttonState;
int buttonState_p;
unsigned long time1;
unsigned long time2;
unsigned long time_span;
int step_count;
int step_goal;
int reset;
int q;

void setup(){
  Serial.begin(9600);
  lcd.begin(16, 2);           // Initialilze LCD
  reset = 0;                  // Sets reset equal to false
  pin_number = 6;             // Set LED pin
  pin_number3 = 4;            // Set Button pin
  buttonState_p = 1;          // Set button state history to OFF
  step_count = 0;             // Reset step counter
  step_goal = 20;             // Set desired number of steps
  time_span = 10000;          // Set desired time interval in milliseconds
  q = 0;
  time1 = millis();           // Start trial timer
  time2 = time1 + time_span;  // Determine ending time
  pinMode(pin_number,OUTPUT); // Set LED as output
  pinMode(pin_number3,INPUT); // Set Button as input
}

void loop(){
  if(step_count==0){
    if(q==0){lcd.print("Start:"); q=1;}    // Displays Start
    digitalWrite(pin_number,LOW);          // Turn off LED
  }
      Serial.print("Button State is ");    // Prints variables to serial
      Serial.print(buttonState);           // ^
      Serial.print("/n");                  // ^
      Serial.print(millis());              // ^
      Serial.print(time2);                 // ^
  buttonState = digitalRead(pin_number3);  // Read State of button
  if(buttonState == 0 && buttonState_p != buttonState){ // Check for Button press
    step_count++;                                       // Increase trial number
    lcd.clear();                                        // Clear LCD
    lcd.print(millis()-time1);                          // Prints current time
    lcd.setCursor(0,1);                                 // Moves cursor to second row
    lcd.print(step_count);                              // Prints the current number of steps
  }
  if((millis()-time1)%100==0){             // Update display every tenth second
    lcd.clear();                           // Clear LCD
    lcd.print(millis()-time1);             // Prints current time
    lcd.setCursor(0,1);                    // Moves cursor to second row
    lcd.print(step_count);                 // Prints the current number of steps
  }
  buttonState_p = buttonState;             // Records button state history
  if(millis() >= time2){                   // Displays results from the trials unto LCD
    lcd.clear();                           // Clears display
    lcd.print("Finished!");                // Notifies user that trial has finished
    lcd.setCursor(0,1);                    // Moves cursor to second row
    lcd.print(step_count);                 // Prints how many steps acheived out of number of goal steps
    lcd.print(" / ");                      // ^
    lcd.print(step_goal);                  // ^
    if(step_count < step_goal){            // Lights LED if step taken is less than goal
      digitalWrite(pin_number,HIGH);       // Turns on the LED
    }
    while(reset == 0){                         // Displays results untill next trial is started by pressing the button
      buttonState = digitalRead(pin_number3);  // Read State of button
      if(buttonState == 0){                    // Check for Button press
        reset = 1;                             // Sets reset to true
        step_count = 0;                        // Resets the step counter
        q = 0;
        lcd.clear();
      }
    }
  }
}

IT WORKS! :)

First post!

Hello world :)