Wednesday, March 27, 2019

Ismael-PM-40 min-What we did for robot club

Next step

  • Add the fourth sensor
  • Add wires to the sensor
  • Add code for the sensor



What Ismael did today
  • I fix the code on the sensor
  • I run the code and it work
  • The serial monitor work    

What  Nicholas did today
  • He fix the wires on the sensor and arduino
  • He help me with the code
  • He rearrange the wires for  ground (GND) 
  • He rearrange the wires for power(VCC)  

/*
  Ultrasonic Sensor HC-SR04 and Arduino Tutorial

  by Dejan Nedelkovski,
  www.HowToMechatronics.com

*/
// Left Motor
const int leftDirControl1 = 2;
const int leftDirControl2 = 3;
const int leftSpeedControlPin = 4; // Needs to be a PWM pin to be able to control motor speed

// Right Motor
const int rightDirControl1 = 5;
const int rightDirControl2 = 6;
const int rightSpeedControlPin = 7; // Needs to be a PWM pin to be able to control motor speed

byte carSpeed = 0;  // change this (0-100) to control the speed of the motors
byte leftSpeed = 0;  // change this (0-100) to control the speed of the motors
byte rightSpeed = 0;  // change this (0-100) to control the speed of the motors
char carDirection = 'S'; // S=STOP, F=Forward, B=Reverse
char leftDirection = 'S'; // S=STOP, F=Forward, B=Reverse
char rightDirection = 'S';  // S=STOP, F=Forward, B=Reverse

// defines pins numbers
const int trigPin1 = 9;
const int echoPin1 = 10;
const int trigPin2 = 12;
const int echoPin2 = 8;
const int trigPin3 = 13;
const int echoPin3 = 11;

// defines variables
long duration1;
int distance1;
long duration2;
int distance2;
long duration3;
int distance3;
void setup() {
  pinMode(trigPin1, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin1, INPUT); // Sets the echoPin as an Input
  pinMode(trigPin2, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin2, INPUT); // Sets the echoPin as an Input
 pinMode(trigPin3, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin3, INPUT); // Sets the echoPin as an Input

  //Define L298N Dual H-Bridge Motor Controller Pins
  pinMode(leftDirControl1, OUTPUT);
  pinMode(leftDirControl2, OUTPUT);
  pinMode(leftSpeedControlPin, OUTPUT);
  pinMode(rightDirControl1, OUTPUT);
  pinMode(rightDirControl2, OUTPUT);
  pinMode(rightSpeedControlPin, OUTPUT);

  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600); // Starts the serial communication
  allDriveMotorsStop();
}

void loop() {
  // Clears the trigPin 1
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);

  // Sets the trigPin 1 on HIGH state for 10 micro seconds
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration1 = pulseIn(echoPin1, HIGH); 
  
 // Clears the trigPin 2
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);

  // Sets the trigPin 2 on HIGH state for 10 micro seconds
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration2 = pulseIn(echoPin2, HIGH);
  
 // Clears the trigPin 3
  digitalWrite(trigPin3, LOW);
  delayMicroseconds(2);

  // Sets the trigPin 3 on HIGH state for 10 micro seconds
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin3, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration3 = pulseIn(echoPin3, HIGH);

  // Calculating the distance
  distance1 = duration1 * 0.034 / 2;
  distance2 = duration2 * 0.034 / 2;
  distance3 = duration3 * 0.034 / 2;
  Serial.print("Distance1: ");
  Serial.println(distance1);
  Serial.print(" Distance2:  ");
  Serial.println(distance2);
Serial.print(" Distance3:  ");
  Serial.println(distance3);
delay(1000); 
//  if (distance1 > 10) {
//    // write code that does something when your distance threshhold is found
//    //alarm();
//    allDriveMotorsStop();
//    delay(2000);
//    allDriveMotorsReverse(100);
//    delay(500);
//    DriveMotorsLeftBack(100);
//    delay(500);
//    Serial.println("less than 10");
//  }
//  else {
//    // write code that does something when your distance threshhold is NOT found
//    digitalWrite(LED_BUILTIN, HIGH);
//    Serial.print(distance1);
//    allDriveMotorsForward(150);
//    Serial.println(" is more than 10");
//  }
//
//  // Prints the distance on the Serial Monitor
//  Serial.print("Distance: ");
//  Serial.println(distance1);
}


void allDriveMotorsStop()
{
  analogWrite(leftSpeedControlPin, 0);//Sets speed variable via PWM
  analogWrite(rightSpeedControlPin, 0);//Sets speed variable via PWM
  digitalWrite(leftDirControl1, LOW);
  digitalWrite(leftDirControl2, LOW);
  digitalWrite(rightDirControl1, LOW);
  digitalWrite(rightDirControl2, LOW);
  Serial.println("All Motors STOPPED");
}

int allDriveMotorsForward(int carSpeed)
{
  analogWrite(leftSpeedControlPin, carSpeed);//Sets speed variable via PWM
  analogWrite(rightSpeedControlPin, carSpeed);//Sets speed variable via PWM
  digitalWrite(leftDirControl1, LOW);
  digitalWrite(leftDirControl2, HIGH);
  digitalWrite(rightDirControl1, LOW);
  digitalWrite(rightDirControl2, HIGH);
  Serial.print("car is Forward at a speed of ");
  Serial.println(carSpeed);
}
int allDriveMotorsReverse(int carSpeed)
{
  analogWrite(leftSpeedControlPin, carSpeed);//Sets speed variable via PWM
  analogWrite(rightSpeedControlPin, carSpeed);//Sets speed variable via PWM
  digitalWrite(leftDirControl1, HIGH);
  digitalWrite(leftDirControl2, LOW);
  digitalWrite(rightDirControl1, HIGH);
  digitalWrite(rightDirControl2, LOW);
  Serial.print("car is Reverse at a speed of ");
  Serial.println(carSpeed);
}
int DriveMotorsLeftBack(int carSpeed)
{
  analogWrite(leftSpeedControlPin, carSpeed);//Sets speed variable via PWM
  analogWrite(rightSpeedControlPin, carSpeed);//Sets speed variable via PWM
  digitalWrite(leftDirControl1, HIGH);
  digitalWrite(leftDirControl2, LOW);
  digitalWrite(rightDirControl1, LOW);
  digitalWrite(rightDirControl2, HIGH);
  Serial.print("car is Reverse at a speed of ");
  Serial.println(carSpeed);
}
int DriveMotorsRightBack(int carSpeed)
{
  analogWrite(leftSpeedControlPin, carSpeed);//Sets speed variable via PWM
  analogWrite(rightSpeedControlPin, carSpeed);//Sets speed variable via PWM
  digitalWrite(leftDirControl1, LOW);
  digitalWrite(leftDirControl2, HIGH);
  digitalWrite(rightDirControl1, HIGH);
  digitalWrite(rightDirControl2, LOW);
  Serial.print("car is Reverse at a speed of ");
  Serial.println(carSpeed);
}

No comments:

Post a Comment