mirror of
https://github.com/KevinMidboe/Arduino.git
synced 2026-01-30 12:56:00 +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:
35
Projects/sketch_jan27a/sketch_jan27a.ino
Normal file
35
Projects/sketch_jan27a/sketch_jan27a.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).
|
||||
}
|
||||
111
Projects/sketch_jan27a/toneAC.h
Normal file
111
Projects/sketch_jan27a/toneAC.h
Normal file
@@ -0,0 +1,111 @@
|
||||
// ---------------------------------------------------------------------------
|
||||
// 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
|
||||
Reference in New Issue
Block a user