mirror of
https://github.com/KevinMidboe/Arduino.git
synced 2025-10-29 17:40:11 +00:00
Init commit with many years of arduino sketches and projects. I dont know if the esp8266 includes much, but there are also libraries. I hope they dont have crazy automatic versioning through the Arduino IDE.
This commit is contained in:
25
Projects/ProgLab/Instrument/Instrument.ino
Normal file
25
Projects/ProgLab/Instrument/Instrument.ino
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <Servo.h>
|
||||
|
||||
Servo myservo;
|
||||
|
||||
int potpin = 0;
|
||||
int potReading;
|
||||
int photocellPin = 1;
|
||||
int photocellReading;
|
||||
|
||||
void setup()
|
||||
{
|
||||
myservo.attach(9);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
potReading = analogRead(potpin);
|
||||
potReading = map(potReading, 0, 1023, 0, 179);
|
||||
myservo.write(potReading);
|
||||
photocellReading = analogRead(photocellPin);
|
||||
|
||||
int thisPitch = map(photocellReading, 1, 1000, 120, 1500);
|
||||
tone(8, thisPitch, 10);
|
||||
delay(1);
|
||||
}
|
||||
1
Projects/ProgLab/Instrument2/Instrument2.ino
Normal file
1
Projects/ProgLab/Instrument2/Instrument2.ino
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
107
Projects/ProgLab/LineFollow/LineFollow.ino
Normal file
107
Projects/ProgLab/LineFollow/LineFollow.ino
Normal file
@@ -0,0 +1,107 @@
|
||||
#include <PLab_ZumoMotors.h>
|
||||
#include <Pushbutton.h>
|
||||
#include <QTRSensors.h>
|
||||
#include <ZumoBuzzer.h>
|
||||
#include <ZumoMotors.h>
|
||||
#include <ZumoReflectanceSensorArray.h>
|
||||
|
||||
|
||||
/*
|
||||
* Demo line-following code for the Pololu Zumo Robot
|
||||
*
|
||||
* This code will follow a black line on a white background, using a
|
||||
* PID-based algorithm. It works decently on courses with smooth, 6"
|
||||
* radius curves and has been tested with Zumos using 30:1 HP and
|
||||
* 75:1 HP motors. Modifications might be required for it to work
|
||||
* well on different courses or with different motors.
|
||||
*
|
||||
* http://www.pololu.com/catalog/product/2506
|
||||
* http://www.pololu.com
|
||||
* http://forum.pololu.com
|
||||
*/
|
||||
|
||||
|
||||
ZumoReflectanceSensorArray reflectanceSensors;
|
||||
ZumoMotors motors;
|
||||
Pushbutton button(ZUMO_BUTTON);
|
||||
int lastError = 0;
|
||||
|
||||
// This is the maximum speed the motors will be allowed to turn.
|
||||
// (400 lets the motors go at top speed; decrease to impose a speed limit)
|
||||
const int MAX_SPEED = 400;
|
||||
// Set constants for PID control
|
||||
const float KP = 0.5; // Proportional constant
|
||||
const float KD = 6; // Derivative constant
|
||||
const int SV = 2500; // Set-value for position (in the middle of sensors)
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
|
||||
reflectanceSensors.init();
|
||||
button.waitForButton(); // wait to start calibration
|
||||
|
||||
// Turn on LED to indicate we are in calibration mode
|
||||
pinMode(13, OUTPUT);
|
||||
digitalWrite(13, HIGH);
|
||||
|
||||
// Wait 1 second and then begin automatic sensor calibration
|
||||
// by rotating in place to sweep the sensors over the line
|
||||
delay(1000);
|
||||
int i;
|
||||
for(i = 0; i < 80; i++)
|
||||
{
|
||||
if ((i > 10 && i <= 30) || (i > 50 && i <= 70))
|
||||
motors.setSpeeds(-200, 200);
|
||||
else
|
||||
motors.setSpeeds(200, -200);
|
||||
|
||||
reflectanceSensors.calibrate();
|
||||
|
||||
// Since our counter runs to 80, the total delay will be
|
||||
// 80*20 = 1600 ms.
|
||||
delay(20);
|
||||
}
|
||||
motors.setSpeeds(0,0);
|
||||
|
||||
// Turn off LED to indicate we are through with calibration
|
||||
digitalWrite(13, LOW);
|
||||
|
||||
// Wait for the user button to be pressed and released
|
||||
button.waitForButton();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
unsigned int sensors[6];
|
||||
|
||||
int pv = reflectanceSensors.readLine(sensors);
|
||||
// Our "error" is how far we are away from the center of the line, which
|
||||
// corresponds to position 2500.
|
||||
int error = pv - SV;
|
||||
// do PD computation ( Integral is not used)
|
||||
int speedDifference = KP*error + KD * (error - lastError);
|
||||
|
||||
lastError = error;
|
||||
|
||||
// Get individual motor speeds. The sign of speedDifference
|
||||
// determines if the robot turns left or right.
|
||||
int m1Speed = MAX_SPEED + speedDifference;
|
||||
int m2Speed = MAX_SPEED - speedDifference;
|
||||
|
||||
// Here we constrain our motor speeds to be between 0 and MAX_SPEED.
|
||||
// Generally speaking, one motor will always be turning at MAX_SPEED
|
||||
// and the other will be at MAX_SPEED-|speedDifference| if that is positive,
|
||||
// else it will be stationary. For some applications, you might want to
|
||||
// allow the motor speed to go negative so that it can spin in reverse.
|
||||
if (m1Speed < 0)
|
||||
m1Speed = 0;
|
||||
if (m2Speed < 0)
|
||||
m2Speed = 0;
|
||||
if (m1Speed > MAX_SPEED)
|
||||
m1Speed = MAX_SPEED;
|
||||
if (m2Speed > MAX_SPEED)
|
||||
m2Speed = MAX_SPEED;
|
||||
|
||||
motors.setSpeeds(m1Speed, m2Speed);
|
||||
}
|
||||
53
Projects/ProgLab/LineFollower/LineFollower.ino
Normal file
53
Projects/ProgLab/LineFollower/LineFollower.ino
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <PLab_ZumoMotors.h>
|
||||
#include <Pushbutton.h>
|
||||
#include <QTRSensors.h>
|
||||
#include <ZumoBuzzer.h>
|
||||
#include <ZumoMotors.h>
|
||||
#include <ZumoReflectanceSensorArray.h>
|
||||
|
||||
#define driveSpeed 150
|
||||
#define lightThreshold 800
|
||||
|
||||
ZumoMotors motors;
|
||||
Pushbutton button(ZUMO_BUTTON);
|
||||
unsigned int sensorValues[6];
|
||||
ZumoReflectanceSensorArray reflectanceSensors;
|
||||
|
||||
void setup()
|
||||
{
|
||||
reflectanceSensors.init();
|
||||
motors.setSpeeds(0,0);
|
||||
button.waitForButton();
|
||||
}
|
||||
|
||||
void findLine()
|
||||
{
|
||||
reflectanceSensors.read(sensorValues);
|
||||
if (sensorValues[0] > lightThreshold)
|
||||
{
|
||||
turnLeft();
|
||||
}
|
||||
else if (sensorValues[5] > lightThreshold)
|
||||
{
|
||||
turnRight();
|
||||
}
|
||||
}
|
||||
|
||||
void turnLeft()
|
||||
{
|
||||
motors.setSpeeds(-driveSpeed, -300);
|
||||
delay(400);
|
||||
}
|
||||
void turnRight()
|
||||
{
|
||||
motors.setSpeeds(-300, -driveSpeed);
|
||||
delay(400);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
findLine();
|
||||
motors.setSpeeds(driveSpeed, driveSpeed);
|
||||
}
|
||||
|
||||
|
||||
57
Projects/ProgLab/Lyskryss/Lyskryss.ino
Normal file
57
Projects/ProgLab/Lyskryss/Lyskryss.ino
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <Servo.h>
|
||||
#include <NewPing.h>
|
||||
|
||||
#define Trigger_Pin 12
|
||||
#define Echo_Pin 11
|
||||
#define Max_Distance 200
|
||||
|
||||
// Green car - 10
|
||||
// Yellow car - 9
|
||||
// Red car - 8
|
||||
// Green person - 7
|
||||
// Red person - 6
|
||||
// ECHO 11 & 12
|
||||
|
||||
|
||||
NewPing sonar(Trigger_Pin, Echo_Pin, Max_Distance);
|
||||
|
||||
Servo gateServo;
|
||||
int servoPos = 0;
|
||||
|
||||
int lightPinArray[] = {
|
||||
10, 9, 9, 8, 7, 6, 6, 7, 9, 8, 9};
|
||||
boolean lightStateArray[] = {
|
||||
LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, HIGH, LOW, LOW};
|
||||
int lightDelayArray[] = {
|
||||
1000, 0, 500, 0, 500, 0, 2000, 0, 500, 500, 0};
|
||||
|
||||
void setup()
|
||||
{
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
pinMode(i + 6, OUTPUT);
|
||||
}
|
||||
gateServo.attach(4);
|
||||
}
|
||||
|
||||
void personDetected()
|
||||
{
|
||||
for(int i = 0; i < 12; i++)
|
||||
{
|
||||
delay(lightDelayArray[i]);
|
||||
digitalWrite(lightPinArray[i], lightStateArray[i]);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
unsigned int uS = sonar.ping();
|
||||
if ((uS /US_ROUNDTRIP_CM) < 5 && uS /US_ROUNDTRIP_CM > 1)
|
||||
{
|
||||
personDetected();
|
||||
}
|
||||
else
|
||||
{
|
||||
digitalWrite(7, HIGH);
|
||||
digitalWrite(10, HIGH);
|
||||
}
|
||||
}
|
||||
BIN
Projects/ProgLab/Lyskryss/data/plab-library-master.zip
Normal file
BIN
Projects/ProgLab/Lyskryss/data/plab-library-master.zip
Normal file
Binary file not shown.
89
Projects/ProgLab/Zumo_1/Zumo_1.ino
Normal file
89
Projects/ProgLab/Zumo_1/Zumo_1.ino
Normal file
@@ -0,0 +1,89 @@
|
||||
#include <PLab_ZumoMotors.h>
|
||||
#include <Pushbutton.h>
|
||||
#include <QTRSensors.h>
|
||||
#include <ZumoBuzzer.h>
|
||||
#include <ZumoMotors.h>
|
||||
#include <ZumoReflectanceSensorArray.h>
|
||||
|
||||
ZumoReflectanceSensorArray reflectanceSensors;
|
||||
unsigned int sensor_values[6];
|
||||
ZumoMotors motors;
|
||||
Pushbutton button(ZUMO_BUTTON);
|
||||
int lastError = 0;
|
||||
|
||||
const int MAX_SPEED = 800;
|
||||
// Set constants for PID control
|
||||
const float KP = 0.5; // Proportional constant
|
||||
const float KD = 6; // Derivative constant
|
||||
const int SV = 2500; // Set-value for position (in the middle of sensors)
|
||||
|
||||
unsigned long Timer; //"ALWAYS use unsigned long for timers, not int"
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
reflectanceSensors.init();
|
||||
pinMode(13, OUTPUT);
|
||||
digitalWrite(13, HIGH);
|
||||
button.waitForButton();
|
||||
for(int i = 0; i < 80; i++)
|
||||
{
|
||||
if ((i > 10 && i <= 30) || (i > 50 && i <= 70))
|
||||
motors.setSpeeds(-200, 200);
|
||||
else
|
||||
motors.setSpeeds(200, -200);
|
||||
|
||||
reflectanceSensors.calibrate();
|
||||
|
||||
// Since our counter runs to 80, the total delay will be
|
||||
// 80*20 = 1600 ms.
|
||||
delay(20);
|
||||
}
|
||||
motors.setSpeeds(0, 0);
|
||||
button.waitForButton();
|
||||
|
||||
digitalWrite(13, LOW);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
reflectanceSensors.read(sensor_values);
|
||||
if (sensor_values[5] < 1800)
|
||||
{
|
||||
motors.setSpeeds(-100, -100);
|
||||
delay(1000);
|
||||
motors.setSpeeds(-100, 100);
|
||||
delay(1000);
|
||||
}
|
||||
else if (sensor_values[0] < 1800)
|
||||
{
|
||||
motors.setSpeeds(-100, -100);
|
||||
delay(1000);
|
||||
motors.setSpeeds(100, -100);
|
||||
delay(1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
motors.setSpeeds(100, 100);
|
||||
}
|
||||
|
||||
int pv = reflectanceSensors.readLine(sensor_values);
|
||||
// Our "error" is how far we are away from the center of the line, which
|
||||
// corresponds to position 2500.
|
||||
int error = pv - SV;
|
||||
// do PD computation ( Integral is not used)
|
||||
int speedDifference = KP*error + KD * (error - lastError);
|
||||
|
||||
|
||||
//Serial.println(error);
|
||||
lastError = error;
|
||||
|
||||
// Get individual motor speeds. The sign of speedDifference
|
||||
// determines if the robot turns left or right.
|
||||
int m1Speed = MAX_SPEED + speedDifference;
|
||||
int m2Speed = MAX_SPEED - speedDifference;
|
||||
|
||||
|
||||
|
||||
motors.setSpeeds(m1Speed, m2Speed);
|
||||
}
|
||||
54
Projects/ProgLab/Zumo_2.0/Zumo_2.0.ino
Normal file
54
Projects/ProgLab/Zumo_2.0/Zumo_2.0.ino
Normal file
@@ -0,0 +1,54 @@
|
||||
#include <PLab_ZumoMotors.h>
|
||||
#include <Pushbutton.h>
|
||||
#include <QTRSensors.h>
|
||||
#include <ZumoBuzzer.h>
|
||||
#include <ZumoMotors.h>
|
||||
#include <ZumoReflectanceSensorArray.h>
|
||||
|
||||
#define maxSpeed 400
|
||||
#define reverseSpeed 400
|
||||
#define lightThreshold 1800
|
||||
|
||||
ZumoMotors motors;
|
||||
Pushbutton button(ZUMO_BUTTON);
|
||||
unsigned int sensorValues[6];
|
||||
ZumoReflectanceSensorArray reflectaneceSensors;
|
||||
|
||||
void setup()
|
||||
{
|
||||
reflectaneceSensors.init();
|
||||
motors.setSpeeds(0,0);
|
||||
button.waitForButton();
|
||||
}
|
||||
|
||||
void findLine()
|
||||
{
|
||||
reflectaneceSensors.read(sensorValues);
|
||||
if (sensorValues[0] > lightThreshold)
|
||||
{
|
||||
turnLeft(1000);
|
||||
}
|
||||
else if (sensorValues[5] > lightThreshold)
|
||||
{
|
||||
turnRight(1000);
|
||||
}
|
||||
}
|
||||
|
||||
void turnLeft(int delayTime)
|
||||
{
|
||||
motors.setSpeeds(0, 200);
|
||||
delay(delayTime);
|
||||
}
|
||||
void turnRight(int delayTime)
|
||||
{
|
||||
motors.setSpeeds(200, 0);
|
||||
delay(delayTime);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
findLine();
|
||||
motors.setSpeeds(200, 200);
|
||||
}
|
||||
|
||||
|
||||
81
Projects/ProgLab/Zumo_2/Zumo_2.ino
Normal file
81
Projects/ProgLab/Zumo_2/Zumo_2.ino
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
Drive forward and turn left or right when border is detected
|
||||
- Only reflectionsensor 0 and 5 are used.
|
||||
*/
|
||||
#include <ZumoMotors.h>
|
||||
#include <Pushbutton.h>
|
||||
#include <QTRSensors.h>
|
||||
#include <ZumoReflectanceSensorArray.h>
|
||||
#include <NewPing.h>
|
||||
|
||||
#define LED 13
|
||||
|
||||
// this might need to be tuned for different lighting conditions, surfaces, etc.
|
||||
#define lightThreshold 1800 //
|
||||
|
||||
#define reverseSpeed 200
|
||||
#define turnSpeed 200
|
||||
#define forwardSpeed 100
|
||||
#define revDuration 200
|
||||
#define turnDuration 500
|
||||
ZumoMotors motors;
|
||||
Pushbutton button(ZUMO_BUTTON); // pushbutton on pin 12
|
||||
#define NUM_SENSORS 6
|
||||
unsigned int sensor_values[NUM_SENSORS];
|
||||
ZumoReflectanceSensorArray sensors;
|
||||
|
||||
const int echoPin1 = 2;
|
||||
const int echoPin2 = 4;
|
||||
const int triggerPin1 = 3;
|
||||
const int triggerPin2 = 5;
|
||||
const int maxDistance = 40;
|
||||
|
||||
NewPing sonar1(triggerPin1, echoPin1, maxDistance);
|
||||
NewPing sonar2(triggerPin2, echoPin2, maxDistance);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
sensors.init();
|
||||
button.waitForButton();
|
||||
}
|
||||
|
||||
void turnLeft()
|
||||
{
|
||||
motors.setSpeeds(-reverseSpeed, -reverseSpeed);
|
||||
delay(revDuration);
|
||||
motors.setSpeeds(turnSpeed, -turnSpeed);
|
||||
delay(turnDuration);
|
||||
motors.setSpeeds(forwardSpeed, forwardSpeed);
|
||||
}
|
||||
|
||||
void turnRight()
|
||||
{
|
||||
motors.setSpeeds(-reverseSpeed, -reverseSpeed);
|
||||
delay(revDuration);
|
||||
motors.setSpeeds(-turnSpeed, turnSpeed);
|
||||
delay(turnDuration);
|
||||
motors.setSpeeds(forwardSpeed, forwardSpeed);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
sensors.read(sensor_values);
|
||||
if (sensor_values[0] < lightThresHold || sensor_values[5] < lightThreshold)
|
||||
{
|
||||
if (sensor_values[0] < lightThreshold)
|
||||
{
|
||||
turnLeft();
|
||||
}
|
||||
else if (sensor_values[5] < lightThreshold)
|
||||
{
|
||||
turnRight();
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int time = sonar1.ping();
|
||||
if (
|
||||
|
||||
// If haven't done anything, go forward
|
||||
motors.setSpeeds(forwardSpeed, forwardSpeed);
|
||||
}
|
||||
128
Projects/ProgLab/Zumo__/Zumo__.ino
Normal file
128
Projects/ProgLab/Zumo__/Zumo__.ino
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
Drive forward and turn left or right when border is detected
|
||||
- Only reflectionsensor 0 and 5 are used.
|
||||
*/
|
||||
#include <ZumoMotors.h>
|
||||
#include <Pushbutton.h>
|
||||
#include <QTRSensors.h>
|
||||
#include <ZumoReflectanceSensorArray.h>
|
||||
#include <NewPing.h>
|
||||
|
||||
#define LED 13
|
||||
|
||||
// this might need to be tuned for different lighting conditions, surfaces, etc.
|
||||
#define lightThreshold 1800 //
|
||||
|
||||
#define reverseSpeed 400
|
||||
#define turnSpeed 400
|
||||
#define forwardSpeed 400
|
||||
#define revDuration 300
|
||||
#define turnDuration 200
|
||||
#define triggerPin 3
|
||||
#define echoPin 2
|
||||
#define maxDistance 200
|
||||
|
||||
ZumoMotors motors;
|
||||
Pushbutton button(ZUMO_BUTTON); // pushbutton on pin 12
|
||||
#define NUM_SENSORS 6
|
||||
unsigned int sensor_values[NUM_SENSORS];
|
||||
ZumoReflectanceSensorArray sensors;
|
||||
|
||||
NewPing sonar(triggerPin, echoPin, maxDistance);
|
||||
|
||||
unsigned int pingSpeed = 100;
|
||||
unsigned long pingTimer;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
sensors.init();
|
||||
pingTimer = millis();
|
||||
//button.waitForButton();
|
||||
}
|
||||
|
||||
boolean findLine()
|
||||
{
|
||||
sensors.read(sensor_values);
|
||||
if (sensor_values[0] < lightThreshold || sensor_values[5] < lightThreshold)
|
||||
{
|
||||
if (sensor_values[0] < lightThreshold)
|
||||
{
|
||||
turnLeft();
|
||||
return true;
|
||||
}
|
||||
else if (sensor_values[5] < lightThreshold)
|
||||
{
|
||||
turnRight();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void turnLeft()
|
||||
{
|
||||
motors.setSpeeds(-reverseSpeed, -reverseSpeed);
|
||||
delay(revDuration);
|
||||
motors.setSpeeds(turnSpeed, -turnSpeed);
|
||||
delay(turnDuration);
|
||||
}
|
||||
|
||||
void turnRight()
|
||||
{
|
||||
motors.setSpeeds(-reverseSpeed, -reverseSpeed);
|
||||
delay(revDuration);
|
||||
motors.setSpeeds(-turnSpeed, turnSpeed);
|
||||
delay(turnDuration);
|
||||
}
|
||||
/*
|
||||
void search()
|
||||
{
|
||||
time = sonar.ping();
|
||||
distance = sonar.convert_cm(time);
|
||||
if (distance < 40)
|
||||
motors.setSpeeds(310, 400);
|
||||
else
|
||||
{
|
||||
motors.setSpeeds(400, 400);
|
||||
delay(20);
|
||||
time = sonar.ping();
|
||||
distance = sonar.convert_cm(time);
|
||||
if (distance < 40)
|
||||
motors.setSpeeds(310, 400);
|
||||
}
|
||||
}*/
|
||||
|
||||
void echoCheck()
|
||||
{
|
||||
if (sonar.check_timer())
|
||||
{
|
||||
if (sonar.ping_result / US_ROUNDTRIP_CM < 20)
|
||||
{
|
||||
Serial.println(sonar.ping_result / US_ROUNDTRIP_CM);
|
||||
motors.setSpeeds(310, 400);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Nothing here!");
|
||||
motors.setSpeeds(400, 400);
|
||||
delay(20);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void scan()
|
||||
{
|
||||
// Init at start, find the target.
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
findLine();
|
||||
motors.setSpeeds(310, 400);
|
||||
if (millis() >= pingTimer)
|
||||
{
|
||||
pingTimer += pingSpeed;
|
||||
sonar.ping_timer(echoCheck);
|
||||
}
|
||||
}
|
||||
221
Projects/ProgLab/toneAC.h/toneAC.h
Normal file
221
Projects/ProgLab/toneAC.h/toneAC.h
Normal file
@@ -0,0 +1,221 @@
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// toneAC Library - v1.2 - 01/27/2013
|
||||
|
||||
//
|
||||
|
||||
// AUTHOR/LICENSE:
|
||||
|
||||
// Created by Tim Eckel - teckel@leethost.com
|
||||
|
||||
// Copyright 2013 License: GNU GPL v3 http://www.gnu.org/licenses/gpl-3.0.html
|
||||
|
||||
//
|
||||
|
||||
// LINKS:
|
||||
|
||||
// Project home: http://code.google.com/p/arduino-tone-ac/
|
||||
|
||||
// Blog: http://arduino.cc/forum/index.php/topic,142097.msg1066968.html
|
||||
|
||||
//
|
||||
|
||||
// DISCLAIMER:
|
||||
|
||||
// This software is furnished "as is", without technical support, and with no
|
||||
|
||||
// warranty, express or implied, as to its usefulness for any purpose.
|
||||
|
||||
//
|
||||
|
||||
// PURPOSE:
|
||||
|
||||
// Replacement to the standard tone library with the advantage of nearly twice
|
||||
|
||||
// the volume, higher frequencies (even if running at a lower clock speed),
|
||||
|
||||
// higher quality (less clicking), nearly 1.5k smaller compiled code and less
|
||||
|
||||
// stress on the speaker. Disadvantages are that it must use certain pins and
|
||||
|
||||
// it uses two pins instead of one. But, if you're flexible with your pin
|
||||
|
||||
// choices, this is a great upgrade. It also uses timer 1 instead of timer 2,
|
||||
|
||||
// which may free up a conflict you have with the tone library. It exclusively
|
||||
|
||||
// uses port registers for the fastest and smallest code possible.
|
||||
|
||||
//
|
||||
|
||||
// USAGE:
|
||||
|
||||
// Connection is very similar to a piezo or standard speaker. Except, instead
|
||||
|
||||
// of connecting one speaker wire to ground you connect both speaker wires to
|
||||
|
||||
// Arduino pins. The pins you connect to are specific, as toneAC lets the
|
||||
|
||||
// ATmega microcontroller do all the pin timing and switching. This is
|
||||
|
||||
// important due to the high switching speed possible with toneAC and to make
|
||||
|
||||
// sure the pins are alyways perfectly out of phase with each other
|
||||
|
||||
// (push/pull). See the below CONNECTION section for which pins to use for
|
||||
|
||||
// different Arduinos. Just as usual when connecting a speaker, make sure you
|
||||
|
||||
// add an inline 100 ohm resistor between one of the pins and the speaker wire.
|
||||
|
||||
//
|
||||
|
||||
// CONNECTION:
|
||||
|
||||
// Pins 9 & 10 - ATmega328, ATmega128, ATmega640, ATmega8, Uno, Leonardo, etc.
|
||||
|
||||
// Pins 11 & 12 - ATmega2560/2561, ATmega1280/1281, Mega
|
||||
|
||||
// Pins 12 & 13 - ATmega1284P, ATmega644
|
||||
|
||||
// Pins 14 & 15 - Teensy 2.0
|
||||
|
||||
// Pins 25 & 26 - Teensy++ 2.0
|
||||
|
||||
//
|
||||
|
||||
// SYNTAX:
|
||||
|
||||
// toneAC( frequency [, volume [, length [, background ]]] ) - Play a note.
|
||||
|
||||
// Parameters:
|
||||
|
||||
// * frequency - Play the specified frequency indefinitely, turn off with toneAC().
|
||||
|
||||
// * volume - [optional] Set a volume level. (default: 10, range: 0 to 10 [0 = off])
|
||||
|
||||
// * length - [optional] Set the length to play in milliseconds. (default: 0 [forever], range: 0 to 2^32-1)
|
||||
|
||||
// * background - [optional] Play note in background or pause till finished? (default: false, values: true/false)
|
||||
|
||||
// toneAC() - Stop playing.
|
||||
|
||||
// noToneAC() - Same as toneAC().
|
||||
|
||||
//
|
||||
|
||||
// HISTORY:
|
||||
|
||||
// 01/27/2013 v1.2 - Fixed a counter error which went "over the top" and caused
|
||||
|
||||
// periods of silence (thanks Krodal). For advanced users needing tight code,
|
||||
|
||||
// the TONEAC_TINY switch in toneAC.h activates a version of toneAC() that
|
||||
|
||||
// saves 110 bytes. With TONEAC_TINY, the syntax is toneAC(frequency, length)
|
||||
|
||||
// while playing the note at full volume forever in the background. Added
|
||||
|
||||
// support for the ATmega 640, 644, 1281, 1284P and 2561 microcontrollers.
|
||||
|
||||
//
|
||||
|
||||
// 01/16/2013 v1.1 - Option to play notes in background, returning control back
|
||||
|
||||
// to your sketch for processing while note plays (similar to the way the tone
|
||||
|
||||
// library works). Volume is now linear and in the range from 0-10. Now uses
|
||||
|
||||
// prescaler 256 instead of 64 for frequencies below 122 Hz so it can go down
|
||||
|
||||
// to 1 Hz no matter what speed the CPU is clocked at (helpful if using toneAC
|
||||
|
||||
// to control a two-pin dual LED).
|
||||
|
||||
//
|
||||
|
||||
// 01/11/2013 v1.0 - Initial release.
|
||||
|
||||
//
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
#ifndef toneAC_h
|
||||
|
||||
#define toneAC_h
|
||||
|
||||
|
||||
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#else
|
||||
|
||||
#include <WProgram.h>
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
//#define TONEAC_TINY // Uncomment to use alternate function toneAC(frequency, length) that saves 110 bytes.
|
||||
|
||||
|
||||
|
||||
#if defined (__AVR_ATmega32U4__) || defined(__AVR_ATmega640__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__)
|
||||
|
||||
#define PWMT1AMASK DDB5
|
||||
|
||||
#define PWMT1BMASK DDB6
|
||||
|
||||
#define PWMT1DREG DDRB
|
||||
|
||||
#define PWMT1PORT PORTB
|
||||
|
||||
#elif defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__)
|
||||
|
||||
#define PWMT1AMASK DDD4
|
||||
|
||||
#define PWMT1BMASK DDD5
|
||||
|
||||
#define PWMT1DREG DDRD
|
||||
|
||||
#define PWMT1PORT PORTD
|
||||
|
||||
#else
|
||||
|
||||
#define PWMT1AMASK DDB1
|
||||
|
||||
#define PWMT1BMASK DDB2
|
||||
|
||||
#define PWMT1DREG DDRB
|
||||
|
||||
#define PWMT1PORT PORTB
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__)
|
||||
|
||||
#define TIMSK1 TIMSK
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifndef TONEAC_TINY
|
||||
|
||||
void toneAC(unsigned long frequency = 0, uint8_t volume = 10, unsigned long length = 0, uint8_t background = false);
|
||||
|
||||
#else
|
||||
|
||||
void toneAC(unsigned long frequency = 0, unsigned long length = 0);
|
||||
|
||||
#endif
|
||||
|
||||
void noToneAC();
|
||||
|
||||
#endif
|
||||
35
Projects/ProgLab/toneAC.h/toneAC.h.ino
Normal file
35
Projects/ProgLab/toneAC.h/toneAC.h.ino
Normal file
@@ -0,0 +1,35 @@
|
||||
// ---------------------------------------------------------------------------
|
||||
// Connect your piezo buzzer (without internal oscillator) or speaker to these pins:
|
||||
// Pins 9 & 10 - ATmega328, ATmega128, ATmega640, ATmega8, Uno, Leonardo, etc.
|
||||
// Pins 11 & 12 - ATmega2560/2561, ATmega1280/1281, Mega
|
||||
// Pins 12 & 13 - ATmega1284P, ATmega644
|
||||
// Pins 14 & 15 - Teensy 2.0
|
||||
// Pins 25 & 26 - Teensy++ 2.0
|
||||
// Be sure to include an inline 100 ohm resistor on one pin as you normally do when connecting a piezo or speaker.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#include "toneAC.h"
|
||||
|
||||
// Melody liberated from the toneMelody Arduino example sketch by Tom Igoe.
|
||||
int melody[] = { 262, 196, 196, 220, 196, 0, 247, 262 };
|
||||
int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 };
|
||||
|
||||
void setup() {} // Nothing to setup, just start playing!
|
||||
|
||||
void loop() {
|
||||
for (unsigned long freq = 125; freq <= 15000; freq += 10) {
|
||||
toneAC(freq); // Play the frequency (125 Hz to 15 kHz sweep in 10 Hz steps).
|
||||
delay(1); // Wait 1 ms so you can hear it.
|
||||
}
|
||||
toneAC(); // Turn off toneAC, can also use noToneAC().
|
||||
|
||||
delay(1000); // Wait a second.
|
||||
|
||||
for (int thisNote = 0; thisNote < 8; thisNote++) {
|
||||
int noteDuration = 1000/noteDurations[thisNote];
|
||||
toneAC(melody[thisNote], 10, noteDuration, true); // Play thisNote at full volume for noteDuration in the background.
|
||||
delay(noteDuration * 4 / 3); // Wait while the tone plays in the background, plus another 33% delay between notes.
|
||||
}
|
||||
|
||||
while(1); // Stop (so it doesn't repeat forever driving you crazy--you're welcome).
|
||||
}
|
||||
Reference in New Issue
Block a user