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:
2019-05-30 23:41:53 +02:00
parent 2d047634f2
commit 6c84b31f2c
1480 changed files with 198581 additions and 0 deletions

View 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).
}

View File

@@ -0,0 +1,25 @@
// ---------------------------------------------------------------------------
// Connect a two-pin dual LED to the following pins with inline 220 ohm resistor.
// 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
// Connect the center lead of a potentiometer to analog pin A0 and the other two leads to +5V and ground.
// ---------------------------------------------------------------------------
#include <toneAC.h>
unsigned long timestamp = 0; // Stores when the next time the routine is set to run.
void setup() {}
void loop() {
if (millis() > timestamp) { // Is it time yet?
timestamp += 500; // Set the next time routine will run. 500 ms because the lowest frequency is 2 Hz, which is a half second.
int pot = analogRead(A0); // Read the potentiometer connected to analog pin A0 to control alternating flashing speed.
int freq = map(pot, 0, 1023, 2, 40); // Convert pot analog values to a range from 2 to 40 Hz.
toneAC(freq, 10, 0, true); // Set the frequency and have it run forever in the background (next event should take over in 500 ms).
}
/* Do a bunch of other stuff here, it won't affect toneAC doing its thing. */
}

View File

@@ -0,0 +1,18 @@
###################################
# Syntax Coloring Map For toneAC
###################################
###################################
# Datatypes (KEYWORD1)
###################################
###################################
# Methods and Functions (KEYWORD2)
###################################
toneAC KEYWORD2
noToneAC KEYWORD2
###################################
# Constants (LITERAL1)
###################################

View File

@@ -0,0 +1,68 @@
// ---------------------------------------------------------------------------
// Created by Tim Eckel - teckel@leethost.com
// Copyright 2013 License: GNU GPL v3 http://www.gnu.org/licenses/gpl-3.0.html
//
// See "toneAC.h" for purpose, syntax, version history, links, and more.
// ---------------------------------------------------------------------------
#include "toneAC.h"
unsigned long _tAC_time; // Used to track end note with timer when playing note in the background.
#ifndef TONEAC_TINY
uint8_t _tAC_volume[] = { 200, 100, 67, 50, 40, 33, 29, 22, 11, 2 }; // Duty for linear volume control.
#endif
#ifndef TONEAC_TINY
void toneAC(unsigned long frequency, uint8_t volume, unsigned long length, uint8_t background) {
if (frequency == 0 || volume == 0) { noToneAC(); return; } // If frequency or volume are 0, turn off sound and return.
if (volume > 10) volume = 10; // Make sure volume is in range (1 to 10).
#else
void toneAC(unsigned long frequency, unsigned long length) {
if (frequency == 0) { noToneAC(); return; } // If frequency is 0, turn off sound and return.
#endif
PWMT1DREG |= _BV(PWMT1AMASK) | _BV(PWMT1BMASK); // Set timer 1 PWM pins to OUTPUT (because analogWrite does it too).
uint8_t prescaler = _BV(CS10); // Try using prescaler 1 first.
unsigned long top = F_CPU / frequency / 2 - 1; // Calculate the top.
if (top > 65535) { // If not in the range for prescaler 1, use prescaler 256 (122 Hz and lower @ 16 MHz).
prescaler = _BV(CS12); // Set the 256 prescaler bit.
top = top / 256 - 1; // Calculate the top using prescaler 256.
}
#ifndef TONEAC_TINY
unsigned int duty = top / _tAC_volume[volume - 1]; // Calculate the duty cycle (volume).
#else
unsigned int duty = top >> 1; // 50% duty cycle (loudest and highest quality).
#endif
#ifndef TONEAC_TINY
if (length > 0 && background) { // Background tone playing, returns control to your sketch.
#else
if (length > 0) { // Background tone playing, returns control to your sketch.
#endif
_tAC_time = millis() + length; // Set when the note should end.
TIMSK1 |= _BV(OCIE1A); // Activate the timer interrupt.
}
ICR1 = top; // Set the top.
if (TCNT1 > top) TCNT1 = top; // Counter over the top, put within range.
TCCR1B = _BV(WGM13) | prescaler; // Set PWM, phase and frequency corrected (top=ICR1) and prescaler.
OCR1A = OCR1B = duty; // Set the duty cycle (volume).
TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1B0); // Inverted/non-inverted mode (AC).
#ifndef TONEAC_TINY
if (length > 0 && !background) { delay(length); noToneAC(); } // Just a simple delay, doesn't return control till finished.
#endif
}
void noToneAC() {
TIMSK1 &= ~_BV(OCIE1A); // Remove the timer interrupt.
TCCR1B = _BV(CS11); // Default clock prescaler of 8.
TCCR1A = _BV(WGM10); // Set to defaults so PWM can work like normal (PWM, phase corrected, 8bit).
PWMT1PORT &= ~_BV(PWMT1AMASK); // Set timer 1 PWM pins to LOW.
PWMT1PORT &= ~_BV(PWMT1BMASK); // Other timer 1 PWM pin also to LOW.
}
ISR(TIMER1_COMPA_vect) { // Timer interrupt vector.
if (millis() >= _tAC_time) noToneAC(); // Check to see if it's time for the note to end.
}

View 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