Wednesday, May 4, 2011

Lab 4

Part A: Data-logging to non-volatile memory


EEPROM allows the user to save non-volatile data on the Arduino so that the information is not lost when the Arduino is powered down.  The user can save their non-volatile data in the 1024 bytes that the Arduino has available.    The main two functions used in data-logging are:

value = EEPROM.read(address);

EEPROM.write(address, value);



Basically, for the purpose of this lab, a value was stored for each byte less than or equal to 512.  Each value corresponded to the byte where the specific value was stored.  For instance the value "45" was stored in byte "45" in the non-volatile memory of the Arduino.  After the numbers were stored using EEPROM, the Arduino was powered down by unplugging the Arduino from the USB.  After plugging in the Arduino back into to the computer, the correct values were read from the EEPROM, proving that that the Arduino correctly stored the values in its non-volatile memory.  The code can be seen below:

#include <EEPROM.h>
int value;
int i=0;
int a;
void setup()
{
   Serial.begin(9600);
  
    EEPROM.write(a, a);
}

void loop()
{
  if (a <= 512);
  {
   a=i++;
   value = EEPROM.read(a);
  Serial.println(value);
  Serial.print("\n");
  delay(20);
  }

}

Part B: Mechatronics- build a rotational positioning device


The servo is controlled by sending a pulse to the device at least every 20 ms, so the servo is not moved out of position.  Basically the duration of the pulse determines the position that the servo moves to.  Although numbers vary slightly depending on the Arduino, a pulse width of 1 ms corresponds to a position of 0 degrees, a pulse width of 1.5 ms corresponds to 90 degrees, and a pulse width of 2 ms corresponds to a position of 180 degrees.  Similarly, the servo can be moved to any position within this range by selecting the appropriate pulse width between 1 to 2 ms.  The code for the first, most basic part of this lab can be seen below:


Code 1:

int buttonPin = 10;


void setup() {Serial.begin(9600);
pinMode(buttonPin, OUTPUT);

}

void loop()
{
 
      


     digitalWrite(buttonPin, HIGH);
     delayMicroseconds(2);
     digitalWrite(buttonPin, LOW);
     delayMicroseconds(1);
    
      
}


_________________________________________________________



Next, the servo was reversed back and forth.   This was done by uploading the following code to the Arduino:

Code 2:

int buttonPin = 10;
int i;

void setup() {Serial.begin(9600);
pinMode(buttonPin, OUTPUT);

}

void loop() 
{
  
       
for(i=0;i<2000;i=i+2)
{
     digitalWrite(buttonPin, HIGH);  
     delayMicroseconds(i);
     digitalWrite(buttonPin, LOW);
     delayMicroseconds(19000-i);
     
       
}



Next, the servo was controlled by entering an actual angle value rather than just a "time" pulse width in order to control the position.  Additionally, the speed of the servo motor was controlled.  Both of these were done by using the following code:

Code 3:


int buttonPin = 10;
int count=5;
int moveServo;
void setup() {Serial.begin(9600);
pinMode(buttonPin, OUTPUT);

}



void loop()  {
 if (Serial.available() > 0) 
{
  moveServo = Serial.read();
  if (moveServo == 49) {
  for (count = 5 ; count <= 50 ; count += 5)   // Count by 5 to gradually increase steps
  {
                 
        analogWrite(buttonPin, 180);           
                 
        delay(30);                             
  }
  for (count = 50 ; count >= 5 ; count -= 5)   // Count down by 5 to gradually decrease steps
  {   
               //higher count= slower speed
        analogWrite(buttonPin, 60);           
         
        delay(30); 
  }
  }
  if (moveServo == 50) {
  for (count = 5 ; count <= 150 ; count += 5)   // Count by 5 to gradually increase steps
  {
                 
        analogWrite(buttonPin, 180);           
                 
        delay(50);                             
  }
  for (count = 150 ; count >= 5 ; count -= 5)   // Count down by 5 to gradually decrease steps
  {   
               //higher count= slower speed
        analogWrite(buttonPin, 60);           
         
        delay(50); 
  }
  }
  if (moveServo == 51) {
  for (count = 5 ; count <= 250 ; count += 5)   // Count by 5 to gradually increase steps
  {
                 
        analogWrite(buttonPin, 180);           
                 
        delay(50);                             
  }
  for (count = 250 ; count >= 5 ; count -= 5)   // Count down by 5 to gradually decrease steps
  {   
               //higher count= slower speed
        analogWrite(buttonPin, 60);           
         
        delay(50); 
  }
  }
}
 }

Part C: Using a color LCD display

The LCD display can be used to display not only writing but also a variety of shapes, such as lines, dots, circles, and squares.  The user can also choose how to color each of these words or figures and where to position them exactly on the screen based on an x and y coordinate system.  For the first part of this section of the lab, the example code was downloaded from blackboard to see how the device worked.  Then, we created our own code to run on the Arduino to display various shapes, colors, and writing based on the example by simply using the LCD reference sheet to insert various shapes, writing, and colors.

However, our main goal was to make an interactive program that would test our reflex once a shape popped up on the screen.  For this specific experiment, we tested the user's reflex to a red rectangle popping up on the screen.  The test would be initiated by pressing one of the buttons.  Once the red rectangle popped up, the user would have to press that same button as fast they could repeatedly until the rectangle disappeared and the word, "STOP" appeared.  The serial monitor would count the number of button presses and hence determine both the user's reaction time as well as their speed in pressing the button.

Code 1:

// Included files
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "WProgram.h"
#include "HardwareSerial.h"
// External Component Libs
#include "LCD_driver.h"
int time;
int buttonPin = 5;    
int val; 
 int buttonState;
 int buttonPresses = 0;
void setup() {Serial.begin(9600);
ioinit(); //Initialize I/O
LCDInit(); //Initialize the LCD
LCDContrast(44);
              pinMode(buttonPin, INPUT);
              buttonState = digitalRead(buttonPin);

}

void loop() 
{
   
 
   int s2;
   s2 = !digitalRead(4);
   if (s2) {
    LCDClear(BLACK);
    delay(2000);
    LCDSetRect(1, 1, 100, 100, 1, RED);
    
  }
  val = digitalRead(buttonPin);
  time = millis();
  if (val != buttonState) {
    if(val == LOW) {
    if(time <= 10000) {
      buttonPresses++;
      Serial.println(buttonPresses);
      
     }
    }
  }
  buttonState = val; 
 if( time == 10000) {
   LCDClear(WHITE);
    LCDPutStr("STOP", 30, 30, BLACK, WHITE);
 }
 

      
  }

No comments:

Post a Comment