Monday, November 21, 2016
Arduino Reflex Test Code
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int but = 7;
int buttonState = 0;
long ran = 0;
int beginTime = 0;
int delTime = 0;
int currTime = 0;
void setup() {
pinMode(but, INPUT);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop() {
beginTime = millis();
buttonState = digitalRead(but);
myservo.write(88);
if(buttonState == HIGH)
{
tone(3, 2000, 300);
delay(1000);
ran = random(2000, 5000);
delay(ran);
myservo.write(150);
delay(1000);
tone(3, 3000, 300);
}
else
{
myservo.write(88);
}
}
Sunday, June 19, 2016
Arduino Instrument Code and Fritzing
Code:
int trig = 10;
int echo = 11;
long duration;
long distance;
int force;
void setup() {
pinMode(echo, INPUT);
pinMode(trig, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig, LOW); //triggers on/off and then reads data
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = (duration / 2) * .0344; //344 m/s = speed of sound. We're converting into cm
int notes[7] = {261, 294, 329, 349, 392, 440, 494}; //Putting several notes in an array
// mid C D E F G A B
force = analogRead(A0); //defining force as FSR data
if (distance < 0 || distance > 50 || force < 100) { //if not presed and not in front
noTone(12); //dont play music
}
else if ((force > 100)) { //if pressed
int sound = map(distance, 0, 50, 0, 6); //map distance to the array of notes
tone(12, notes[sound]); //call a certain note depending on distance
}
}
int trig = 10;
int echo = 11;
long duration;
long distance;
int force;
void setup() {
pinMode(echo, INPUT);
pinMode(trig, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig, LOW); //triggers on/off and then reads data
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = (duration / 2) * .0344; //344 m/s = speed of sound. We're converting into cm
int notes[7] = {261, 294, 329, 349, 392, 440, 494}; //Putting several notes in an array
// mid C D E F G A B
force = analogRead(A0); //defining force as FSR data
if (distance < 0 || distance > 50 || force < 100) { //if not presed and not in front
noTone(12); //dont play music
}
else if ((force > 100)) { //if pressed
int sound = map(distance, 0, 50, 0, 6); //map distance to the array of notes
tone(12, notes[sound]); //call a certain note depending on distance
}
}
Fritzing:
Wednesday, June 15, 2016
Laser pointer servo
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
//int pos;
int posit;
Servo servo1; // create servo object to control a servo
Servo servo2; // create servo object to control a servo
// twelve servo objects can be created on most boards
void setup() {
servo1.attach(9); // attaches the servo on pin 9 to the servo object
servo2.attach(10);
Serial.begin(9600);
servo1.write(5);
servo2.write(5);
delay(20);
}
void loop() {
posit = servo1.read();
long horRand1 = random(30, 150);
Serial.println(horRand1);
Serial.println("1");
if (posit >= horRand1 ) {
Serial.println("2");
for (long pos = posit; pos >= horRand1; pos -= 1) {
servo1.write(pos);
//Serial.println("3");
delay(20);
Serial.println(pos);
}
Serial.println("4");
}
else {
for (long pos = posit; pos <= horRand1; pos += 1){
servo1.write(pos);
delay(20);
}
}
posit = servo2.read();
long vertRand2 = random(0, 100);
Serial.println(vertRand2);
if (posit >= vertRand2) {
for (long pos = posit; pos >= vertRand2; pos -= 1) {
servo2.write(pos);
delay(20);
}
}
else {
for (long pos = posit; pos <= vertRand2; pos += 1){
servo2.write(pos);
delay(20);
}
}
}
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
//int pos;
int posit;
Servo servo1; // create servo object to control a servo
Servo servo2; // create servo object to control a servo
// twelve servo objects can be created on most boards
void setup() {
servo1.attach(9); // attaches the servo on pin 9 to the servo object
servo2.attach(10);
Serial.begin(9600);
servo1.write(5);
servo2.write(5);
delay(20);
}
void loop() {
posit = servo1.read();
long horRand1 = random(30, 150);
Serial.println(horRand1);
Serial.println("1");
if (posit >= horRand1 ) {
Serial.println("2");
for (long pos = posit; pos >= horRand1; pos -= 1) {
servo1.write(pos);
//Serial.println("3");
delay(20);
Serial.println(pos);
}
Serial.println("4");
}
else {
for (long pos = posit; pos <= horRand1; pos += 1){
servo1.write(pos);
delay(20);
}
}
posit = servo2.read();
long vertRand2 = random(0, 100);
Serial.println(vertRand2);
if (posit >= vertRand2) {
for (long pos = posit; pos >= vertRand2; pos -= 1) {
servo2.write(pos);
delay(20);
}
}
else {
for (long pos = posit; pos <= vertRand2; pos += 1){
servo2.write(pos);
delay(20);
}
}
}
Monday, June 13, 2016
Piezo Buzzer: Code and Fritzing
On this Page:
Copy and paste code into Arduino IDE
Note: if you want to play different pitches on multiple pins, you need to call noTone() on the pin playing before using tone() on the next pin
Helpful Links:
-Arduino Code a Melody
-Sparkfun Piezo Experiment Guide
-Arduino Tone() Info
Example Code in Video:
void setup() {
pinMode(12, OUTPUT);
}
void loop() {
tone(12, 261); //Middle C
delay(1000);
tone(12, 277); //C#
delay(1000);
tone(12, 294); //D
delay(1000);
tone(12, 311); //D#
delay(1000);
tone(12, 330); //E
delay(1000);
tone(12, 349); //F
delay(1000);
tone(12, 370); //F#
delay(1000);
tone(12, 392); //G
delay(1000);
tone(12, 415); //G#
delay(1000);
tone(12, 440); //A
delay(1000);
// put your main code here, to run repeatedly:
}
Fritzing Electrical Diagram:
Harder but Efficient Code with For Loop:
void setup() {
pinMode(12, OUTPUT);
}
void loop() {
int notes[10]={261, 277, 294, 311, 330, 349, 370, 392, 415, 440};
// mid C C# D D# E F F# G G# A
for(int i = 0; i < 10; i++){
tone(12, notes[i]); //accesses spots on the array.
delay(1000);
}
}
- Helpful Links
- Example Code in Video
- Fritzing Electrical Diagram
- Harder but more Efficient code with For Loop
Copy and paste code into Arduino IDE
Note: if you want to play different pitches on multiple pins, you need to call noTone() on the pin playing before using tone() on the next pin
Helpful Links:
-Arduino Code a Melody
-Sparkfun Piezo Experiment Guide
-Arduino Tone() Info
Example Code in Video:
void setup() {
pinMode(12, OUTPUT);
}
void loop() {
tone(12, 261); //Middle C
delay(1000);
tone(12, 277); //C#
delay(1000);
tone(12, 294); //D
delay(1000);
tone(12, 311); //D#
delay(1000);
tone(12, 330); //E
delay(1000);
tone(12, 349); //F
delay(1000);
tone(12, 370); //F#
delay(1000);
tone(12, 392); //G
delay(1000);
tone(12, 415); //G#
delay(1000);
tone(12, 440); //A
delay(1000);
// put your main code here, to run repeatedly:
}
Fritzing Electrical Diagram:
Harder but Efficient Code with For Loop:
void setup() {
pinMode(12, OUTPUT);
}
void loop() {
int notes[10]={261, 277, 294, 311, 330, 349, 370, 392, 415, 440};
// mid C C# D D# E F F# G G# A
for(int i = 0; i < 10; i++){
tone(12, notes[i]); //accesses spots on the array.
delay(1000);
}
}
Saturday, June 4, 2016
Force Sensor: Code and Wiring

Code:
int pressurePin = A0;
int force;
int LEDpin = 12;
void setup() {
Serial.begin(9600);
pinMode(LEDpin, OUTPUT);
}
void loop() {
force = analogRead(pressurePin);
Serial.println(force);
if(force > 500)
{
digitalWrite(LEDpin, HIGH);
}
else
{
digitalWrite(LEDpin, LOW);
}
delay(100);
}
Wiring Diagram:
Friday, May 6, 2016
Ultrasonic Tutorial Code
int trig = 10;
int echo = 9;
int duration;
int distance;
void setup() {
pinMode(echo, INPUT);
pinMode(trig, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig, LOW);
delay(.002);
digitalWrite(trig, HIGH);
delay(.01);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH); //record the time it takes for the sound to return
distance = (duration / 2) / 29.1; //convert the time to distance
Serial.println(distance);
delay(100);
}
int echo = 9;
int duration;
int distance;
void setup() {
pinMode(echo, INPUT);
pinMode(trig, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig, LOW);
delay(.002);
digitalWrite(trig, HIGH);
delay(.01);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH); //record the time it takes for the sound to return
distance = (duration / 2) / 29.1; //convert the time to distance
Serial.println(distance);
delay(100);
}
Sunday, April 10, 2016
Carduino Plug and Play Code (copy and paste)
// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!
//Modified by Sam
#include <AFMotor.h> //Includes the Adafruit Motor Shield Library
AF_DCMotor motor1(1); // DC motor is hooked up to M1
AF_DCMotor motor2(2); // DC motor is hooked up to M2
//==================INSERT VALUES HERE \/ ====================================================================================================
int pin1 = 22; // CHANGE 22 TO THE NUMBER OF PIN YOU ARE USING!!!! Digital pins connected to the radio receiver ports =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
int pin2 = 30; //CHANGE 30 TO THE NUMBER OF PIN YOU ARE USING!!!! ^ The receiver is the small black box w/ a red antenna=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
//Make sure that pin1 is hooked up to the throttle port on the receiver (port 2 on 9x)
//and that pin2 is hooked up to the rudder port on the receiver (port 4 on 9x)
int neutralh = 1500;//PUT YOUR HIGH NEUTRAL VALUE HERE. REPLACE 1500 =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
int neutrall = 1420;//PUT YOUR LOW NEUTRAL VALUE HERE. REPLACE 1420 =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
int maxval = 1870; //PUT YOUR MAX VALUE HERE REPLACE 1870 =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~==~=~==~=~==~=~==~=~==~=~==~=~=
int minval = 1050; // PUT YOUR MIN VALUE HERE REPLACE 1050 =~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~
//==================INSERT VALUES HERE /\ ====================================================================================================
void setup() {
Serial.begin(9600); // set up Serial monitor at 9600 bps
motor1.run(RELEASE); //This releases the motors via the motorshield
motor2.run(RELEASE); //^ This is to make sure the motors dont start w/o control
pinMode(pin1, INPUT); //Setting up RC input pins
pinMode(pin2, INPUT); //Setting up RC input pins
void loop() {
int sV1; //These are all different speed values that will be mapped
int sV2;
int sV3;
int sV4;
int sV5;
int sV6;
int speedValue = pulseIn(pin1, HIGH); //Getting the pulse in value from pin 1 and defining pin1 as speed
int directionValue = pulseIn(pin2, HIGH); //Getting the pulse in value from pin 2 and defining pin1 as turning
//Serial.println(speedValue);
//Serial.println(directionValue);
//These can be printed for debugging
if (speedValue >= 1420 && speedValue <= 1500) { //If the sticks are Neutral, then stay still
motor1.run(RELEASE); //releases motors
motor2.run(RELEASE); // releases motors
delay(50); //checks again every 50 micro Seconds
}
if (speedValue > 1500 && directionValue >= 1420 && directionValue <= 1500) { //if the throttle is up and turning is neutral
sV1 = map(speedValue, 1500, 1870, 140, 255); //Maps the pulseIn speedvalue to numbers the motors can read
//Motor is set to 140-255 instead of 0-255 so that the motor doesnt stall if
//it is powered at too low of a level
if(sV1 > 255){ //If the speed value is over 255 then set it to 255. .
sV1 = 255; //This prevents overpowering the motor if there is a pulsein glitch
}
motor1.setSpeed(sV1); // Sets the speed of motor1 to the speedValue
motor2.setSpeed(sV1); // Sets the speed of motor2 to the speedValue
motor1.run(FORWARD); //Makes the motor go forward at the speed defined above
motor2.run(FORWARD); //Makes the motor go forward at the speed defined above
delay(50); //Repeat every 50 micro seconds
}
if (directionValue > neutralh && speedValue > neutralh) { //Speed value is forward, direction is right
sV2 = map(speedValue, neutralh, maxval, 140, 255); //RIGHT FORWARD
if(sV2 > 255){
sV2 = 255;
}
motor1.setSpeed(0);
motor2.setSpeed(sV2);
motor1.run(FORWARD);
motor2.run(FORWARD);
Serial.println("B");
delay(50);
}
if (directionValue < neutrall && speedValue > neutralh) { //Speed is forward, Direction is left
sV3 = map(speedValue, neutralh, maxval, 140, 255); //LEFT FORWARD
if(sV3 > 255){
sV3 = 255;
}
motor1.setSpeed(sV3);
motor2.setSpeed(0);
motor1.run(FORWARD);
motor2.run(FORWARD);
Serial.println("C");
delay(50);
}
if (speedValue < neutrall && directionValue >= neutrall && directionValue <= neutralh) { //Speed is reverse and direction value is centered
sV4 = map(speedValue, neutrall, minval, 140, 255); //REVERSE
if(sV4 > 255){
sV4 = 255;
}
motor1.setSpeed(sV4);
motor2.setSpeed(sV4);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
Serial.println("D");
delay(50);
}
if (directionValue > neutralh && speedValue < neutrall) {
sV5 = map(speedValue, neutralh, minval, 140, 255); //BACK RIGHT
if(sV5 > 255){
sV5 = 255;
}
motor1.setSpeed(sV5);
motor2.setSpeed(0);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
Serial.println("E");
delay(50);
}
if (directionValue < neutrall && speedValue < neutrall) {
sV6 = map(speedValue, neutralh, minval, 140, 255); //BACK LEFT
if(sV6 > 255){
sV6 = 255;
}
motor1.setSpeed(0);
motor2.setSpeed(sV6);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
Serial.println("F");
delay(50);
}
}
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!
//Modified by Sam
#include <AFMotor.h> //Includes the Adafruit Motor Shield Library
AF_DCMotor motor1(1); // DC motor is hooked up to M1
AF_DCMotor motor2(2); // DC motor is hooked up to M2
//==================INSERT VALUES HERE \/ ====================================================================================================
int pin1 = 22; // CHANGE 22 TO THE NUMBER OF PIN YOU ARE USING!!!! Digital pins connected to the radio receiver ports =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
int pin2 = 30; //CHANGE 30 TO THE NUMBER OF PIN YOU ARE USING!!!! ^ The receiver is the small black box w/ a red antenna=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
//Make sure that pin1 is hooked up to the throttle port on the receiver (port 2 on 9x)
//and that pin2 is hooked up to the rudder port on the receiver (port 4 on 9x)
int neutralh = 1500;//PUT YOUR HIGH NEUTRAL VALUE HERE. REPLACE 1500 =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
int neutrall = 1420;//PUT YOUR LOW NEUTRAL VALUE HERE. REPLACE 1420 =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
int maxval = 1870; //PUT YOUR MAX VALUE HERE REPLACE 1870 =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~==~=~==~=~==~=~==~=~==~=~==~=~=
int minval = 1050; // PUT YOUR MIN VALUE HERE REPLACE 1050 =~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~
//==================INSERT VALUES HERE /\ ====================================================================================================
void setup() {
Serial.begin(9600); // set up Serial monitor at 9600 bps
motor1.run(RELEASE); //This releases the motors via the motorshield
motor2.run(RELEASE); //^ This is to make sure the motors dont start w/o control
pinMode(pin1, INPUT); //Setting up RC input pins
pinMode(pin2, INPUT); //Setting up RC input pins
void loop() {
int sV1; //These are all different speed values that will be mapped
int sV2;
int sV3;
int sV4;
int sV5;
int sV6;
int speedValue = pulseIn(pin1, HIGH); //Getting the pulse in value from pin 1 and defining pin1 as speed
int directionValue = pulseIn(pin2, HIGH); //Getting the pulse in value from pin 2 and defining pin1 as turning
//Serial.println(speedValue);
//Serial.println(directionValue);
//These can be printed for debugging
if (speedValue >= 1420 && speedValue <= 1500) { //If the sticks are Neutral, then stay still
motor1.run(RELEASE); //releases motors
motor2.run(RELEASE); // releases motors
delay(50); //checks again every 50 micro Seconds
}
if (speedValue > 1500 && directionValue >= 1420 && directionValue <= 1500) { //if the throttle is up and turning is neutral
sV1 = map(speedValue, 1500, 1870, 140, 255); //Maps the pulseIn speedvalue to numbers the motors can read
//Motor is set to 140-255 instead of 0-255 so that the motor doesnt stall if
//it is powered at too low of a level
if(sV1 > 255){ //If the speed value is over 255 then set it to 255. .
sV1 = 255; //This prevents overpowering the motor if there is a pulsein glitch
}
motor1.setSpeed(sV1); // Sets the speed of motor1 to the speedValue
motor2.setSpeed(sV1); // Sets the speed of motor2 to the speedValue
motor1.run(FORWARD); //Makes the motor go forward at the speed defined above
motor2.run(FORWARD); //Makes the motor go forward at the speed defined above
delay(50); //Repeat every 50 micro seconds
}
if (directionValue > neutralh && speedValue > neutralh) { //Speed value is forward, direction is right
sV2 = map(speedValue, neutralh, maxval, 140, 255); //RIGHT FORWARD
if(sV2 > 255){
sV2 = 255;
}
motor1.setSpeed(0);
motor2.setSpeed(sV2);
motor1.run(FORWARD);
motor2.run(FORWARD);
Serial.println("B");
delay(50);
}
if (directionValue < neutrall && speedValue > neutralh) { //Speed is forward, Direction is left
sV3 = map(speedValue, neutralh, maxval, 140, 255); //LEFT FORWARD
if(sV3 > 255){
sV3 = 255;
}
motor1.setSpeed(sV3);
motor2.setSpeed(0);
motor1.run(FORWARD);
motor2.run(FORWARD);
Serial.println("C");
delay(50);
}
if (speedValue < neutrall && directionValue >= neutrall && directionValue <= neutralh) { //Speed is reverse and direction value is centered
sV4 = map(speedValue, neutrall, minval, 140, 255); //REVERSE
if(sV4 > 255){
sV4 = 255;
}
motor1.setSpeed(sV4);
motor2.setSpeed(sV4);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
Serial.println("D");
delay(50);
}
if (directionValue > neutralh && speedValue < neutrall) {
sV5 = map(speedValue, neutralh, minval, 140, 255); //BACK RIGHT
if(sV5 > 255){
sV5 = 255;
}
motor1.setSpeed(sV5);
motor2.setSpeed(0);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
Serial.println("E");
delay(50);
}
if (directionValue < neutrall && speedValue < neutrall) {
sV6 = map(speedValue, neutralh, minval, 140, 255); //BACK LEFT
if(sV6 > 255){
sV6 = 255;
}
motor1.setSpeed(0);
motor2.setSpeed(sV6);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
Serial.println("F");
delay(50);
}
}
Subscribe to:
Posts (Atom)


