08| Brain Bots (Final Project)

Photoresistor

Liam Brady

Photoresistors are resistance-based sensors that react to how much light they are being hit by. The more light present, the smaller the resistance across the sensor. The Arduino is able to measure the changing resistance to determine how much light is in the room.

void setup() {
  Serial.begin(9600);
}

void loop() {
  int val = analogRead(A0);

  Serial.println(val);
}

Servo

Liam Brady

Servos are specialized motors that support precise control. Rather than spinning off to oblivion like a motor, servos let the designer specify exactly where they want the object to rotate to. Most common servos allow for 180ยบ of rotation.

#include <Servo.h>
Servo myservo;

void setup() {
  myservo.attach(9);
}

void loop() {
  int val = analogRead(A0);
  val = map(val, 0, 1023, 0, 179);

  myservo.write(val);

  delay(15);
}

Potentiometer

Liam Brady

Potentiometers are variable resistors. Potentiometers have a knob that varies the output voltage (by varying the resistance as they are turned). The analog pins on the Arduino can measure this change.

void setup() {
  Serial.begin(9600);
}

void loop() {
  int val = analogRead(A0);

  Serial.println(val);

  delay(100);
}

Ultrasonic Sensor (Large)

Liam Brady

Ultrasonic sensors are distance sensors that use sound waves to detect how far away an object is. They send out high frequency bursts of sound and listen for its echo. They then determine how far away the object is based on how long it takes for the sound to return to the sensor. This variety requires an Arduino library to operate.

 

NewPing Library

#include <NewPing.h>

NewPing mysensor(5, 6, 200);

void setup() {
  Serial.begin(9600);
}

void loop() {
  int pingTime = mysensor.ping();

  int distance = mysensor.ping_in();

  int distance_cm = mysensor.ping_cm();

  Serial.println(distance);
}