Lab #4: Non-volatile memory, Servo Motors and MATLAB


Build a data-logger with a photoresister
Below is our code for the data logger with a photoresistor:
#include <EEPROM.h>
int pin = A0;
int index = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(pin,INPUT);
Serial.println("Input 'W' to write data into EEPROM. Input 'R' to read data.");
}
void loop() {
// put your main code here, to run repeatedly:
char inputChar = Serial.read();
if (inputChar == 'W'){
for(index; index <13; index ++){
int data = analogRead(pin);
EEPROM.write(index, data);
index ++;
delay(5000);
}
index = 0;
}else if (inputChar == 'R'){
int i = 0;
while (i <13){
int value = EEPROM.read(i);
Serial.println(value);
i++;
}
}
}
Servo Motor
Pulses
int servo = 6;
int inputChar;
void setup() {
Serial.begin(9600);
pinMode(servo, OUTPUT);
}
void loop() {
int cycle = 1500;
digitalWrite(servo, HIGH);
delayMicroseconds(cycle);
digitalWrite(servo, LOW);
delayMicroseconds(20000-cycle);
}
Enter Angular Position
servo set position int servo = 6;
int inputChar;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(servo, OUTPUT);
Serial.println("Type letters a-c to see how the Servo motor changes");
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0){
inputChar = Serial.read();
if(inputChar == 'a'){
Serial.println("0 degree angle");
for(int i = 0; i <100; i++){
digitalWrite(servo, HIGH);
delayMicroseconds(500);
digitalWrite(servo, LOW);
delay(20);
}
}
if(inputChar == 'b'){
Serial.println("90 degree angle");
for(int i = 0; i <100; i++){
digitalWrite(servo, HIGH);
delayMicroseconds(1500);
digitalWrite(servo, LOW);
delay(20);
}
}
if(inputChar == 'c'){
Serial.println("180 degree angle");
for(int i = 0; i <100; i++){
digitalWrite(servo, HIGH);
delayMicroseconds(2500);
digitalWrite(servo, LOW);
delay(20);
}
}
}
}
Rotating Between 2 Positions
int servo = 6;
int inputChar;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(servo, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(servo, HIGH);
delayMicroseconds(500);
digitalWrite(servo, LOW);
delay(500);
digitalWrite(servo, HIGH);
delayMicroseconds(1000);
digitalWrite(servo, LOW);
delay(500);
}
Control Servo Speed
int servo=6;
int inputChar;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(servo, OUTPUT);
Serial.println("Type a-c to see variations in speed!");
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available() > 0){
inputChar = Serial.read();
while (inputChar == 'a') {
Serial.println("Fast speed");
digitalWrite(servo, HIGH);
delayMicroseconds(500);
digitalWrite(servo, LOW);
delay(200);
digitalWrite(servo, HIGH);
delayMicroseconds(2000);
digitalWrite(servo, LOW);
delay(200);
if(Serial.available() > 0){
inputChar = Serial.read();
}
}
while (inputChar == 'b') {
Serial.println("Medium speed");
digitalWrite(servo, HIGH);
delayMicroseconds(500);
digitalWrite(servo, LOW);
delay(800);
digitalWrite(servo, HIGH);
delayMicroseconds(2000);
digitalWrite(servo, LOW);
delay(800);
if(Serial.available() > 0){
inputChar = Serial.read();
}
}
while (inputChar == 'c') {
Serial.println("Slow speed");
digitalWrite(servo, HIGH);
delayMicroseconds(800);
digitalWrite(servo, LOW);
delay(1500);
digitalWrite(servo, HIGH);
delayMicroseconds(2000);
digitalWrite(servo, LOW);
delay (15a00);
if(Serial.available() > 0) {
inputChar = Serial.read();
}
}
}
}
Control Servo with a Potentiometer
int potval = 0;
void setup() {
// put your setup code here, to run once:
pinMode(6, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
potval = analogRead(A2); // A2 is the pin that is connected to our potentiometer,
// so we made a variable to store and display the value
Serial.println(potval);
if (potval<= 300){ // after printing the potentiometer values to the serial monitor
// we decided a good value to be determining factor in our if loop was 300
digitalWrite(6, HIGH); // if the potentiometer value is less than 300, it will turn slower
delayMicroseconds(1000);
digitalWrite(6, LOW);
delayMicroseconds(7000);
delay(800);
digitalWrite(6,HIGH);
delayMicroseconds(2000);
digitalWrite(6,LOW);
delayMicroseconds(7000);
delay(1600);
}
else{ //if the value is higher than 300, the speed of the potentiometer is fater
digitalWrite(6, HIGH);
delayMicroseconds(2000);
digitalWrite(6, LOW);
delayMicroseconds(7000);
delay(800);
digitalWrite(6,HIGH);
delayMicroseconds(1000);
digitalWrite(6,LOW);
delayMicroseconds(7000);
delay(150);
}
}
Control Speed and Angle from Serial Monitor
float angle_value = 0; //angle entered in degrees
int inputChar;
float motor_pos = 0; //value of motor position
int speed_value = 0; //speed value
void setup() {
// put your setup code here, to run once:
pinMode(6,OUTPUT);
Serial.begin(9600);
Serial.println("Enter angle in degrees to rotate motor");
delay(1000);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()>0){
angle_value = 0;
angle_value = Serial.parseInt();
motor_pos = (500*angle_value/90)+1000;
Serial.println(angle_value);
Serial.println("Motor Position");
Serial.println(motor_pos);
Serial.println("Enter '1' or '10' for Speed Value"); //1 is slow, 10 is fast
while(Serial.available() ==0){}
speed_value = Serial.parseInt();
Serial.println("Speed Value");
Serial.println(speed_value);
Serial.println("Re-enter angle to change speed");
}
if (speed_value == 10 ){
digitalWrite(6, HIGH);
delayMicroseconds(motor_pos);
digitalWrite(6, LOW);
delayMicroseconds(20000-motor_pos);
delay(1000);
digitalWrite(6,HIGH);
delayMicroseconds(1000);
digitalWrite(6,LOW);
delayMicroseconds(19000);
delay(1000);
}
else{
digitalWrite(6, HIGH);
delayMicroseconds(motor_pos);
digitalWrite(6, LOW);
delayMicroseconds(20000-motor_pos);
delay(2000);
digitalWrite(6,HIGH);
delayMicroseconds(1000);
digitalWrite(6,LOW);
delayMicroseconds(19000);
delay(2000);
}
}


Comments
Post a Comment