Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 17, 2016
0 parents commit c94c712
Show file tree
Hide file tree
Showing 32 changed files with 846 additions and 0 deletions.
Binary file added 9781430249290.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Chapter 1/.DS_Store
Binary file not shown.
25 changes: 25 additions & 0 deletions Chapter 1/Blink/Blink.ino
@@ -0,0 +1,25 @@
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

30 changes: 30 additions & 0 deletions Chapter 1/Ultimate_Machine/Ultimate_Machine.ino
@@ -0,0 +1,30 @@
#include <Servo.h>

// DC hobby servo
Servo servo1;

// Switch connected to digital pin 2
int SwitchPin = 2;

void setup() {
// turn on servo
servo1.attach(9);

// sets the digital pin 2 as input
// and enables pullup resistor
pinMode(SwitchPin, INPUT_PULLUP);
}

void loop() {
// read the input pin
int val = digitalRead(SwitchPin);

// test if switch has been triggered
if (val == LOW) {
servo1.write(115);
delay(250);
servo1.write(0);
}
delay(100);
}

Binary file added Chapter 2/.DS_Store
Binary file not shown.
124 changes: 124 additions & 0 deletions Chapter 2/Android/Android.ino
@@ -0,0 +1,124 @@
// include the library for hobby servos
#include <Servo.h>

// DC hobby servo
Servo servo1;

// sets the constants for each of the sensor signal pins:
const int pingPin[] = {2, 3, 4};

// sets the increment counter for each sensor:
int counter = 0;

// sets the speed of the servo movement
int spd = 10;

// sets the left, right, and center positions of the servo
int left = 10;
int right = 170;
int center = (right - left) / 2;

// sets the variable to keep track of the servo angle
int angle = center;

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

// turn on servo and move to center
servo1.attach(9);
servo1.write(center);
}

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

// resets counter if we run out of sensors
if (counter == 3) counter = 0;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin[counter], OUTPUT);
digitalWrite(pingPin[counter], LOW);
delayMicroseconds(2);
digitalWrite(pingPin[counter], HIGH);
delayMicroseconds(5);
digitalWrite(pingPin[counter], LOW);

// The same pin is used to read the signal from the PING))): 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(pingPin[counter], INPUT);
duration = pulseIn(pingPin[counter], HIGH);

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

// moves the servo to the left if left sensor is triggered
if (inches < 6 && counter == 0) {
if (angle != left) {
for (int i=angle; i>left; i--) {
servo1.write(i);
delay(spd);
}
angle = left;
}

// moves to the center if center sensor is triggered
} else if (inches < 6 && counter == 1) {
// moves from left to center
if (angle < center) {
for (int i=angle; i<center; i++) {
servo1.write(i);
delay(spd);
}
// or moves from right to center
} else {
for (int i=angle; i>center; i--) {
servo1.write(i);
delay(spd);
}
}
angle = center;

// moves to the right if right sensor is triggered
} else if (inches < 6 && counter == 2) {
if (angle != right) {
for (int i=angle; i<right; i++) {
servo1.write(i);
delay(spd);
}
angle = right;
}

// otherwise hold steady at the current position
} else {
servo1.write(angle);
}

// send the value in inches to the Serial Monitor for each sensor
Serial.print("Sensor ");
Serial.print(counter);
Serial.print(": ");
Serial.print(inches);
Serial.println(" inches");

// increment counter for the next loop
counter++;

// short delay before starting over again
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;
}


59 changes: 59 additions & 0 deletions Chapter 2/One_Ultrasonic_With_Light/One_Ultrasonic_With_Light.ino
@@ -0,0 +1,59 @@
// sets the constants for the sensor and led signal pins:
const int pingPin = 2;
const int led = 10;

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

// sets the LED pin to an output mode
pinMode(led, OUTPUT);
}

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

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

// The same pin is used to read the signal from the PING))): 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(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

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

// turn on the led if object is within six inches
if (inches < 6) {
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}

// send the value in inches to the Serial Monitor
Serial.print(inches);
Serial.println(" inches");

// short delay before starting over again
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;
}

@@ -0,0 +1,71 @@
// sets the constants for each of the sensor and led signal pins:
const int pingPin[] = {2, 3, 4};
const int led[] = {10, 11, 12};

// sets the increment counter for each sensor:
int counter = 0;

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

// sets each LED pin to an output mode
for (int i=0; i<3; i++) pinMode(led[i], OUTPUT);
}

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

// resets counter if we run out of sensors
if (counter == 3) counter = 0;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin[counter], OUTPUT);
digitalWrite(pingPin[counter], LOW);
delayMicroseconds(2);
digitalWrite(pingPin[counter], HIGH);
delayMicroseconds(5);
digitalWrite(pingPin[counter], LOW);

// The same pin is used to read the signal from the PING))): 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(pingPin[counter], INPUT);
duration = pulseIn(pingPin[counter], HIGH);

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

// turn on the led if object is within six inches
if (inches < 6) {
digitalWrite(led[counter], HIGH);
} else {
digitalWrite(led[counter], LOW);
}

// send the value in inches to the Serial Monitor for each sensor
Serial.print("Sensor ");
Serial.print(counter);
Serial.print(": ");
Serial.print(inches);
Serial.println(" inches");

// increment counter for the next loop
counter++;

// short delay before starting over again
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;
}

Binary file added Chapter 3/.DS_Store
Binary file not shown.

0 comments on commit c94c712

Please sign in to comment.