Thursday, March 31, 2011

Lab 4: Data Storage and RC servos

During this lab, we wanted to be able to use the EEPROM.read and EEPROM.write functions to store data in the Arduino so that it could be used later on in different studies. However, the one danger in doing this is that if the code is written in an endless loop, then it is very possible to fry the Arduino

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

void loop() {
int inputChar;
if (Serial.available() > 0){
  inputChar = Serial.read();
   
    if (inputChar == 'w')
    {
        EEPROM.write(address, value);
        address=address+1;
        value=value+2;
    }
}
}

Additionally, we tried to build an angle positioning device by using an RC servo motor plugged into the Arduino. The angle of the servo motor is controlled by the length of pulses sent to the servo from the Arduino. Each of the pulses must be sent in 20ms intervals.

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

void loop() {
 
int inputChar;
int inputNumber;
int inputNumber2;
inputNumber = 0;

if (Serial.available() > 0) {
      delayMicroseconds(5000);
      while (Serial.available() > 0) {
        inputChar = Serial.read();
        inputNumber2 = inputNumber * 10 + (inputChar-48) ;
      
       
        if (inputNumber2 == 1)
          Serial.println ("180");
        digitalWrite(4,HIGH);
 
              delayMicroseconds(2000);
            digitalWrite(4,LOW);
 
              delay(18);

if (inputNumber2 == 9)
          Serial.println ("90");
        digitalWrite(4,HIGH);
 
              delayMicroseconds(1500);
            digitalWrite(4,LOW);
 
              delayMicroseconds(19000);

if (inputNumber2 == 0)
          Serial.println ("0");
        digitalWrite(4,HIGH);
 
              delayMicroseconds(1000);
            digitalWrite(4,LOW);
 
              delayMicroseconds(19000);
      }
    }
}

No comments:

Post a Comment