Saturday, February 13, 2016

Day 15 to 18 - Fire Fight Challenge


  • Our objective was to autonomously program a Vex Squarebot robot to navigate throughout a maze and blow out a candle. There were four rooms and the candle was randomly placed in one of the rooms. In order to have the candle detect the candle we used an IR detector to sense the heat from the candle and we used encoders on the wheels to make sure that the robot could have been commanded to go through the maze.
Here is the code for our robot.#pragma config(Sensor, in1,    rightEncoder,   sensorRotation)
#pragma config(Sensor, in3,    leftEncoder,    sensorRotation)
#pragma config(Sensor, in4,    fan,            sensorDigitalOut)
#pragma config(Sensor, in6,    frontIRSensor,  sensorAnalog)
#pragma config(Sensor, in14,   frontSonarSensor, sensorSONAR, int1)
#pragma config(Motor,  port2,           rightMotor,    tmotorServoContinuousRotation, openLoop, reversed)
#pragma config(Motor,  port3,           leftMotor,     tmotorServoContinuousRotation, openLoop)

void clearEncoders();
void forward(int x);
void tleft(int x);
void tright(int x);
void backwards(int x);
void stop(int x);
void search();
void flame();

task main()
{
wait1Msec(2000);
bMotorReflected[port2] = true;
while(true)
{
//approach room 1//
clearEncoders();
stop(200);
forward(200);
stop(800);
tright(85);
stop(800);
forward(190);
stop(800);
tright(70);
stop(800);

//enter room 1 //
forward(130);
stop(800);
flame();
stop(800);
tleft(115);
stop(100);

//exit room 1 and approach room 2//
backwards(290);
stop(800);
forward(15);
stop(800);
tright(90);
stop(800);
backwards(110);
stop(800);
forward(205);
stop(800);
tright(80);
stop(800);
forward(290);
stop(800);
tright(65);
stop(800);

//enter room 2//
forward(120);
stop(800);
flame();
stop(800);
tleft(115);
stop(800);

//exit room 2 approach room 3//
backwards(150);
stop(800);
tleft(55);
stop(800);
backwards(360);
stop(800);
forward(25);
stop(800);
tright(85);
stop(800);
backwards(180);
stop(800);

//enter room 3//
forward(150);
stop(800);
tright(80);
stop(800);
forward(300);
stop(800);
flame();
stop(800);
tleft(115);
stop(800);

//exit room 3 and approach room 4//
backwards(450);
stop(800);
forward(50);
stop(800);
tleft(55);
stop(800);
forward(100);
stop(800);
tright(85);
stop(800);
forward(200);
stop(800);
tleft(75);
stop(800);

//enter room 4//
forward(150);
stop(800);
flame();
tleft(145);
stop(100);



}


}


void clearEncoders()
{
SensorValue[leftEncoder] = 0;
SensorValue[rightEncoder] = 0; //function for encoders to reset their count; therefore, to ensure that encoders are accurate at keeping consistent movements.
}

void forward(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] < x) //function ensures that both encoders are working 
{
motor[leftMotor] = 65;
motor[rightMotor] = 60;
}
}
void tleft(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] < x)
{
motor[leftMotor] = -50;
motor[rightMotor] = 50;
}
}

void tright(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] < x)
{
motor[leftMotor] = 50;
motor[rightMotor] = -40;
}
}

void backwards(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] < x)
{
motor[leftMotor] = -60;
motor[rightMotor] = -60;
}
}

void stop(int x)
{
clearEncoders();
{
motor[leftMotor] = 0;
motor[rightMotor] = 0;
wait1Msec(x);
}
}


void flame() ******
{
int count=0;
while(SensorValue[frontIRSensor]==0 && count<30)//was this while function makes the IR sensor search for a flame
{
tright(5); // turns right 30 times at (5) power or in other words it is a tright(150); command if there is no IR value greater than one
stop(5);
count++;
}
if(SensorValue[frontIRSensor]>1)//loop until sonar finds an object <20 inches.  // when the IR sensor detects a flame it should register a value greater than one.
{
while(SensorValue[frontIRSensor]<7) // The while function will not end until the IR sensor registers a value less than 7
{
forward(5); // keep going forward near the flame
}
stop(200);
SensorValue[fan]=true;
wait10Msec(1000);
SensorValue[fan]=false; // after the while statement above is fulfilled and the squarebot is close enough to the flame it will turn on the fan
wait10Msec(500000000);

}
}
  • Here is the video of this code in action with our sqaurebot

Wednesday, February 3, 2016

Day 13 - Ultra Sonic sensors on VEX squarebot

*The Lab today consisted of designing a squarebot that could navigate autonomously using functions and avoid walls forever or approach a can. The setup for this lab utilized encoders to consistently have the same movements; in addition, there were sonar sensors attached to the bot so it could "see" the walls and can to avoid or approach the items.
  • Autonomous robot that avoids the walls
    • Here is the code
#pragma config(Sensor, in2,    rightEncoder,   sensorRotation)
#pragma config(Sensor, in3,    leftEncoder,    sensorRotation)
#pragma config(Sensor, in9,    sonarSensor,    sensorSONAR, int1)
#pragma config(Motor,  port2,           rightMotor,    tmotorServoContinuousRotation, openLoop, reversed)
#pragma config(Motor,  port3,           leftMotor,     tmotorServoContinuousRotation, openLoop)

void clearEncoders();
void forward(int x);
void tleft(int x);
void tright(int x);
void backwards(int x);
void stop(int x);



task main()

{ wait1Msec(2000);
bMotorReflected[port2]=1;
while(1)
{

if (SensorValue[sonarSensor]>3 ||SensorValue[sonarSensor]==-1)//loop until sonar finds an object less than 3 inches.
{
forward(20);//go forward

}
else
{
backwards(20);
stop(1);
tleft(70);
}
}
}

void clearEncoders() //must clear encoders every time so that way their measurements are accurate
{
SensorValue[leftEncoder] = 0;
SensorValue[rightEncoder] = 0;
}

void forward(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] <x) // read both the left && right encoders
{
motor[leftMotor] = 60;
motor[rightMotor] = 50;
}
}
void tleft(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] <x)
{
motor[leftMotor] = -40;
motor[rightMotor] = 40;
}
}

void tright(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] <x)
{
motor[leftMotor] = 60;
motor[rightMotor] = -40;
}
}

void backwards(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] <x)
{
motor[leftMotor] = -40;
motor[rightMotor] = -40;
}
}

void stop(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] <x)
{
motor[leftMotor] = 0;
motor[rightMotor] = 0;
}
}

    • Picture of avoiding walls

Day 11 - Vex Programing of squarebot without encoders


  • Today we assembled and programmed the squarebot to drive through a maze. We used commands to tell the robot where to go during the maze. This code is not autonomous because it is not dependent on any sensors.
    • Here is the code for our sqaure bot


/*************************************************************************
VEX - Point Turns

Description: This program instructs your robot turn right for .75 seconds
and then turn left for .75 seconds. There is a two second pause at the
beginning of the program.

Configuration: This program is written to work with the Squarebot model.
Right Motor - port2
Left Motor - port3

Additional Notes:
- The "bMotorReflected[port2] = 1;" is needed with the Squarebot model,
but may not be needed for all robot configurations.
- Point Turns, or turns in place, are achieved by having the motors spin
in opposite directions.
*************************************************************************/
void stop(int x);

task main()
{
wait1Msec(3000); //Robot waits for 2000 milliseconds before executing program
bMotorReflected[port2] = 1; //Reflects the direction of the motor on port2

motor[port2] = 70; //Motor forward 1
motor[port3] = 70; //Motor on port3 is run at full (127) power forward
wait1Msec(3000);
stop(500);

//Turn LEFT
motor[port2] = 78; //Motor on port2 is run at full (-127) power reverse 68
motor[port3] = -83; //Motor on port3 is run at full (127) power forward
wait1Msec(400);//Robot runs previous code for 750 milliseconds before moving on
stop(500);

motor[port2] = 95; //FORWARD 2
motor[port3] = 95; //Motor on port3 is run at full (127) power forward
wait1Msec(1000);
stop(500);

motor[port2] = 130; //Turn 2nd LEFT
motor[port3] = -100; //Motor on port3 is run at full (127) power forward
wait1Msec(300);
stop(500);

motor[port2] = 85; //Forward 3
motor[port3] = 85; //Motor on port3 is run at full (127) power forward
wait1Msec(1700);
stop(500);

motor[port2] = -70; //Turn RIGHT
motor[port3] = 70; //Motor on port3 is run at full (127) power forward 68-70
wait1Msec(500);
stop(500);

motor[port2] = 60; //Foward 4
motor[port3] = 60; //Motor on port3 is run at full (127) power forward
wait1Msec(1000);
stop(500);

motor[port2] = -88; //Turn RIGHT
motor[port3] = 88; //Motor on port3 is run at full (127) power forward
wait1Msec(400);
stop(500);

motor[port2] = 80; //Final Forwa
motor[port3] = 80; //Motor on port3 is run at full (127) power forward
wait1Msec(2000);

} //Program ends, and the robot stops

void stop(int x)
{
motor[port2] = 0;
motor[port3] = 0;
wait1Msec(x);
}
    • In addition here is a video that shows our square bot in action


Day 10 - Hack a toy


  • The objective of the Hack-a-Toy project was to buy an old toy and dismantle it then re configure it and program it to do similar functions with the arduino.
  1. Here is the toy before it was was dismantled. It is the bunny on the left.
  2. The first step to hacking the toy was to take it apart. Here is a descriptive video that shows the toy's electronic components after it was taken apart.  An interesting part about the toy's anatomy is that it is that the system used transistors, motors, resistors, speakers, and other components.
  • The plan to reconfigure the toy involved making the toy so that the temperature of a human body could trigger the motor of the bunny. 
  1. Here is a picture of how, mechanically, this was achieved.  

Day 9 - Intro to Infrared Detectors and Ultrasonic Sensors


  • For this lab we used the arduino, along with IR Detector and Ultrasonic Sensors to view heat and distance values using the Arduino. 
    • Here is the code for the IR sensor portion of the lab.

#define sensorPin A0 // select the input pin (pin2)
int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {
  Serial.begin(9600);//9600
}
void loop() {
  sensorValue = analogRead(sensorPin); // read the value fromt he sensor:
  Serial.println(sensorValue);
  delay(300);
 // while (Serial.available()){
   // int inChar = Serial.read();
   // Serial.print(inChar);
   // delay(300);
  //}
}
    • Picture of IR Sensor in action
    • Code for Sonar Sensor.
const int trigPin = 2;
const int echoPin = 4;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

    • Video for Sonar Sensor

Day 6 - Intro to Sensors and Microcontroller inputs and outputs


  • Microcontrollers, input and outputs
    •  Demonstrate Push button LED using if/else statement
      • Here is how it was built in the video and what the end result was.


      • In addition here is the code that helped achieve this


#define buttonPin 8 //# of pushbutton pin
#define ledPin 6    //# of LED pin
int buttonState = 0; //variable for reading the pushbutton status
void setup() {
  pinMode(ledPin, OUTPUT);  //initialize the LED pin as output:
  pinMode(buttonPin, INPUT);  //initialize push button as input:
}
void loop() {
buttonState = digitalRead(buttonPin); //read the state of the pushbutton value:
// check if push buton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH); // turn LED on:
}
else {
digitalWrite(ledPin, LOW); // turn LED off
}
}

Day 5 - Intro to c code and hbridge


  • Driving Motors and Other Output Devices
  1. Demonstrate microcontroller controlling signal lamp
    1. Here is the picture of the contraption In addition here is the code that allowed the microcontroller to control the signal lamp.
void setup() {
  pinMode(9, OUTPUT); //Initialize Digital Pin 9 as an output
}

void loop() {
  digitalWrite(9, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(5000);              // wait for a second
  digitalWrite(9, LOW);    // turn the LED off by making the voltage LOW
  delay(3000);              // wait for a second
}
2. Demonstrate Darlington controlling signal lamp
3. Demonstrate microcontroller controlling motor

void setup() {
  pinMode(9, OUTPUT); //Initialize Digital Pin 9 as an output
}

void loop() {
  digitalWrite(9, HIGH);   // turn the motor on (HIGH is the voltage level)
  delay(5000);              // wait for a second
  digitalWrite(9, LOW);    // turn the motor off by making the voltage LOW
  delay(3000);              // wait for a second
}
4. Demonstrate PWM signal driving motor
#define motorPin 9 //Motor connected to dig. pin 9
void setup() {
  // nothing happens in set up

}

void loop() {
analogWrite (motorPin, 90);//set motor to 20%
delay (1000);
analogWrite (motorPin, 50);//set motor to 90%
delay (1000);

}
  • Bi-directional Motor Control
  1. This first video demonstrates DPDT relay controlled the motor in both directions.
  2. In addition, this next picture and video shows a motor being controlled in opposing directions because of the arduino programming.Here is the code for this achievement 
    void setup() {
      pinMode (13, OUTPUT) ; //Initialize Digital Pin 13 as an output
        pinMode (12, OUTPUT) ; //Initialize Digital Pin 12 as an output
    }

    void loop () {
      digitalWrite (13,HIGH) ; //Set the motor On ClockWise
      delay (100);           //Wait for 1000 ms (1 Second)
      digitalWrite(13,LOW);  //Set the LED off
      delay (1000);   //Wait for 1 second
     
      digitalWrite (12,HIGH) ; //Set the motor On CounterClockWise
      delay (100);           //Wait for 1000 ms (1 Second)
      digitalWrite(12,LOW);  //Set the LED off
      delay (1000);   //Wait for 1 second
    }

Sunday, January 31, 2016

Day 4 - Intro to Transistors and Arduino

  • This lab explored how transistors work and required us to assemble several systems with transistors and programmed by the Arduino
  1. A transistor is very similar to a relay. There are three pins in a transistor and the pins serve as a collector (where positive electricity flows), base(where electricity may travel to), and emitter (where ground electricity flows). This photo is a visual representation of how electricity flows in both types of transistors PNP and NPN.
  2.  We utilized the capabilities through this assembly. 
  3. Next, we removed the push button and used a human finger as a conductor for the electricity to pass through. It may difficult to tell from the video but when the finger is wet the light shone brighter.
  4. For this next part we used a potentiometer and a variable screw resistor to change the current flowing through the transistors. In this picture A1 is on the right side and A2 is on the left side.. Similarly, in this picture A1 is on the right side and A2 is on the left side. 
  5. We started at a low current and slowly increased the amount of current that could pass into the transistor. The relationship that was discovered was that the more low the milli amps that were permitted to travel through A1 the higher the gain was measured. Keep in mind that gain = (A2 current) /(A1 current).
  • Introduction to Microcontrollers
  1. Here is the LED flashing band. For the next part we designed and powered 4 led lights and cycled them continuously. 
    1. video

    1. code
void setup() {
  pinMode (13, OUTPUT) ; //Initialize Digital Pin 13 as an output
  pinMode (12, OUTPUT) ; //Initialize Digital Pin 12 as an output
  pinMode (7, OUTPUT) ; //Initialize Digital Pin 7 as an output
  pinMode (3, OUTPUT) ; //Initialize Digital Pin 3 as an output
}

void loop () {
  digitalWrite (13,HIGH) ; //Set the LED On
  delay (100);           //Wait for 1000 ms (1 Second)
  digitalWrite(13,LOW);  //Set the LED off
  delay (1000);   //Wait for 1 second
  
  digitalWrite (12,HIGH) ; //Set the LED On
  delay (100);           //Wait for 1000 ms (1 Second)
  digitalWrite(12,LOW);  //Set the LED off
  delay (1000);   //Wait for 1 second

    digitalWrite (7,HIGH) ; //Set the LED On
  delay (100);           //Wait for 1000 ms (1 Second)
  digitalWrite(7,LOW);  //Set the LED off
  delay (1000);   //Wait for 1 second

    digitalWrite (3,HIGH) ; //Set the LED On
  delay (100);           //Wait for 1000 ms (1 Second)
  digitalWrite(3,LOW);  //Set the LED off
  delay (1000);   //Wait for 1 second

    digitalWrite (7,HIGH) ; //Set the LED On
  delay (100);           //Wait for 1000 ms (1 Second)
  digitalWrite(7,LOW);  //Set the LED off
  delay (1000);   //Wait for 1 second

    digitalWrite (12,HIGH) ; //Set the LED On
  delay (100);           //Wait for 1000 ms (1 Second)
  digitalWrite(12,LOW);  //Set the LED off
  delay (1000);   //Wait for 1 second
}

Day 3 - Switches and Relays

  • This lab explored how switches and relay can allow or restrict the passage of electricity in a system.

  1. A switch connects, severs, or diverts the flow of electricity. This pictured, borrowed from the lab handout, describes how a switch works. Furthermore, here is a schematic of the assembly conducted in the lab. Note that there are two switches in this schematic.  This is the real life assembly of this schematic and here is a video. .
  2. In the next part of the lab we assembled a similar system; however, instead of a switch we used a relay. Here is a basic schematic of a relay.  In this schematic there are 4 active areas of interest. The very bottom area, where the coil is located influences the switch in the coil (2nd to top area). When the coil is activated a magnetic force makes the switch move like a lever. To further understand the relay we utilized a schematic that involved the relay.  An interesting concept of the relay in this schematic is that when there is no power from the area that says dc the switch remains toward the bottom; therefore, the led more towards the right will activate. On the other hand, when there is power run through the relay the left led will be activated. Here is how we assembled this schematic.


Saturday, January 30, 2016

Day 2 - Introductions to BreadBoard Ohm's law and Potentiometers


  • For this part of the lab a led light was assembled by connecting all of the components to a BreadBoard
  1. An led such as the one shown in this picture was assembled to the breadboard. Furthermore, this resistor was used in the assembly.  Next, there is a picture of a BreadBoard. Where the blue minus sign and red plus sign are located the components connect the BreadBoard connect horizontal in this picture; on the other hand, in this picture the holes that are next to the numbers are connected vertically in this picture.
    Now, using a separate BreadBoard, the led system was assembled. Power>Resistor>longer side of light bulb>ground 
  • In this part of this day's lab we learned about continuity, resistance, and voltage measurements with the multimeter.
    • Ohm's law is V=I*R or Voltage=Current*Resistance
  1. Here is a picture of the Potentiometer.  OL means that there is no continuity or open loop. O stands for open and L means Loop. Open loop indicates that the positive and ground are not connected; therefore, this indicates that there is no connection where electricity can flow. This is very helpful when soldering a circuit board because it may lead to the discovery of bad soldering.
  2. For the next part we attached the multimeter to a battery and measured the voltage of a battery. Notice that in the first picture the settings for the multimeter were on the continuity settings, which looks like a sound wave, and now they are on the voltage settings.
  3.  Furthermore, the wall's voltage was measured.  Notice again that the settings for the multimeter are set to ac instead of dc.
  4. Next, the resistance of several resistors were measured using the multimeter.   In order to read the resistance the multimeter setting were changed, as shown in this picture, and the ground pin (black) was connected to one end of the resistor and the positive pin (red) was connected to another side of the resistor.
  5. Also in this lab, the resistance of Light Dependent resistors were measured. Notice how in this video when light is exposed to the LDR the resistance decreases on the multimeter and when light is restricted from the LDR the resistance value is more high on the multimeter. 
  6. In addition the resistance was measured on the multimeter during the use of screw-variable resistor.  In the video the current changes when the resistor is activated.

Day 1 - Soldering and Power Source

  • Using a random circuit board, and random resistors and spare parts, we were given the opportunity to practice soldering.
  1.  The first step was to gather random items/parts and a circuit board with plenty of room to solder the parts onto the board. 
  2. Next, we had to heat up the soldering device to 600 degrees Fahrenheit and then place the circuit board, item that we wanted attached to the circuit board, soldering pen tip, and solder all at the same spot until the solder melted and bonded the circuit board and the item. The green part of the board where the copper was visible was the side where the solder was melted down to connect the components. In addition, using the pliers shown in this picture, the ends left on the green side were trimmed to ensure that there would be adequate space.
  • In the other part of this lab we were given a power source and had to solder wiring to it for later use.
  1. Using a phone charger, we cut the end that would normally plug into the phone with pliers.Next using a multimeter we tested which wire supplied the positive charge and which one was ground or negative.
  2. After securely soldering the cables with red as positive and black as ground we used heat shrink tubing to secure the wiring in place and prevent it from tearing apart. The heat shrink tubing was placed around the areas where wiring was attached and then it melted the tubing to the wiring. The heat gun for this part of the lab was extremely hot and the wire along with the tubing was only placed over the heat for a few seconds.
  3. Here is the end result.