Browse Source

Upload files to ''

master
cs131014 5 years ago
parent
commit
ab2a64ab03
  1. 55
      lilypad.ino
  2. 134
      motor_move.ino

55
lilypad.ino

@ -0,0 +1,55 @@
// these constants describe the pins. They won't change:
const int xpin = A1; // x-axis of the accelerometer
const int ypin = A2; // y-axis
const int zpin = A3; // z-axis (only on 3-axis models)
//
int sampleDelay = 500; //number of milliseconds between readings
void setup()
{
// initialize the serial communications:
Serial.begin(9600);
//
//Make sure the analog-to-digital converter takes its reference voltage from
// the AREF pin
pinMode(xpin, INPUT);
pinMode(ypin, INPUT);
pinMode(zpin, INPUT);
}
void loop()
{
int x = analogRead(xpin);
//
//add a small delay between pin readings. I read that you should
//do this but haven't tested the importance
delay(1);
//
int y = analogRead(ypin);
//
//add a small delay between pin readings. I read that you should
//do this but haven't tested the importance
delay(1);
//
int z = analogRead(zpin);
//
//zero_G is the reading we expect from the sensor when it detects
//no acceleration. Subtract this value from the sensor reading to
//get a shifted sensor reading.
float zero_G =512;
//
//scale is the number of units we expect the sensor reading to
//change when the acceleration along an axis changes by 1G.
//Divide the shifted sensor reading by scale to get acceleration in Gs.
float scale =102.3;
//
Serial.print(((float)x - zero_G)/scale);
Serial.print("\t");
//
Serial.print(((float)y - zero_G)/scale);
Serial.print("\t");
//
Serial.print(((float)z - zero_G)/scale);
Serial.print("\n");
//
// delay before next reading:
delay(sampleDelay);
}

134
motor_move.ino

@ -0,0 +1,134 @@
// Setup Motor A (front and rear) pins
int enableA = 1;
int pinA1 = 3;
int pinA2 = 2;
// Setup Motor B (front and rear) pins
int enableB = 6;
int pinB1 = 5;
int pinB2 = 4;
void setup() {
// The setup code goes here and runs once only
// Configure the pin modes for each drive motor
pinMode (enableA, OUTPUT);
pinMode (pinA1, OUTPUT);
pinMode (pinA2, OUTPUT);
pinMode (enableB, OUTPUT);
pinMode (pinB1, OUTPUT);
pinMode (pinB2, OUTPUT);
}
void loop() {
// Main code goes here and will run repeatedly:
car(); // function keeps moving car forward while distance of objects in front are > 15cm away
avoid(); // function makes car go back, turn slightly right to move forward in new direction
}
// Create motor functions
void motorAforward() {
digitalWrite (pinA1, HIGH);
digitalWrite (pinA2, LOW);
}
void motorBforward() {
digitalWrite (pinB1, LOW);
digitalWrite (pinB2, HIGH);
}
void motorAbackward() {
digitalWrite (pinA1, LOW);
digitalWrite (pinA2, HIGH);
}
void motorBbackward() {
digitalWrite (pinB1, HIGH);
digitalWrite (pinB2, LOW);
}
void motorAstop() {
digitalWrite (pinA1, HIGH);
digitalWrite (pinA2, HIGH);
}
void motorBstop() {
digitalWrite (pinB1, HIGH);
digitalWrite (pinB2, HIGH);
}
void motorAcoast() {
digitalWrite (pinA1, LOW);
digitalWrite (pinA2, LOW);
}
void motorBcoast() {
digitalWrite (pinB1, LOW);
digitalWrite (pinB2, LOW);
}
void motorAon() {
digitalWrite (enableA, HIGH);
}
void motorBon() {
digitalWrite (enableB, HIGH);
}
void motorAoff() {
digitalWrite (enableA, LOW);
}
void motorBoff() {
digitalWrite (enableB, LOW);
}
// Setup movement functions
void forward (int duration) {
motorAforward();
motorBforward();
delay (duration);
}
void backward (int duration) {
motorAbackward();
motorBbackward();
delay (duration);
}
void right (int duration) {
motorAbackward();
motorBforward();
delay (duration);
}
void left (int duration) {
motorAforward();
motorBbackward();
delay (duration);
}
void coast (int duration) {
motorAcoast();
motorBcoast();
delay (duration);
}
void breakRobot (int duration) {
motorAstop();
motorBstop();
delay (duration);
}
void disableMotors() {
motorAoff();
motorBoff();
}
void enableMotors() {
motorAon();
motorBon();
}
// Setup the main car function
void car() {
int distance_0;
distance_0 = distance();
// Keep moving forward in a straight line while distance of objects in front > 15cm away
while(distance_0 > 15)
{
motorAon();
motorBon();
forward(1);
distance_0 = distance();
}
breakRobot(0);
}
Loading…
Cancel
Save