Update Gaia anniversary code

This commit is contained in:
Bea 2021-01-10 00:32:31 +01:00
parent 9c8909765f
commit 663af68dbd
7 changed files with 381 additions and 17 deletions

View File

@ -5,13 +5,30 @@
*/
#include <Servo.h>
#include <EEPROM.h>
const int buzzerPin = 6;
const int servoPin = 9;
const unsigned long baudRate = 115200;
const unsigned long baudRates[] =
{
300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 31250, 38400, 57600, 115200
};
const int buzzerPin = 3;
const int servoPin = 5;
const int buttonPin = 7;
const int switchPin = 9;
const int led1Pin = 10;
const int led2Pin = 6;
const int pause = 0;
const int gL = 196;
const int gSL = 207;
const int aL = 220;
const int aSL = 233;
const int bL = 247;
const int c = 261;
const int d = 294;
const int dS = 311;
const int e = 329;
const int f = 349;
const int g = 391;
@ -46,52 +63,249 @@ const int alpiniNotes[] =
const int alpiniDurations[] =
{
500,500,250,250,250,250,250,125,125,250,250,500,500,1000,
500,500,250,250,250,250,250,125,125,250,250,500,50,375,125,500,1000,
500,500,250,250,250,250,250,125,125,250,250,500,50,375,125,500,750,
500,500,250,250,250,250,250,125,125,250,250,500,500,1000,
500,500,250,250,250,250,250,125,125,250,250,500,50,375,125,500,1000,
500,500,250,250,250,250,250,125,125,250,250,500,50,375,125,500,750,
250,250,250,500,500,250,250,250,250,500,500,250,250,250,250,500,500,375,125,250,250,500,500,500,
250,250,250,500,500,250,250,250,250,500,500,250,250,250,250,500,500,375,125,250,250,500,500,250,
250,250,250,500,500,250,250,250,250,500,500,250,250,250,250,500,500,500,500,1000,1000,
10000
};
int alpiniMusicSize = 0;
const int jingleBellsNotes[] =
{
e,e,e,e,e,e,e,g,c,d,e,pause,
f,f,f,f,f,e,e,e,e,e,d,d,e,d,pause,g,pause,
e,e,e,e,e,e,e,g,c,d,e,pause,
f,f,f,f,f,e,e,e,e,g,g,f,d,c,pause,
gL,gL,gL,e,d,c,gL,pause,
gL,gL,gL,e,d,c,aL,pause,
aL,aL,aL,f,e,d,bL,pause,
g,g,g,g,f,d,e,pause,
gL,gL,gL,e,d,c,gL,pause,
gL,gL,gL,e,d,c,aL,pause,
aL,aL,aL,f,e,d,g,g,g,
g,a,g,f,d,c,pause,
pause
};
const int jingleBellsDurations[] =
{
250,250,500,250,250,500,250,250,375,125,500,500,
250,250,375,125,250,250,250,125,125,250,250,250,250,400,100,400,100,
250,250,500,250,250,500,250,250,375,125,500,500,
250,250,375,125,250,250,250,125,125,250,250,250,250,500,250,
125,125,250,250,250,250,500,250,
125,125,250,250,250,250,500,250,
125,125,250,250,250,250,500,250,
125,125,250,250,250,250,500,250,
125,125,250,250,250,250,500,250,
125,125,250,250,250,250,500,250,
125,125,250,250,250,250,250,250,250,
250,250,250,250,250,500,250,
10000
};
int jingleBellsMusicSize = 0;
const int windowsShutdownNotes[] =
{
pause,gS,dS,gSL,aSL,pause
};
const int windowsShutdownDurations[] =
{
1000,250,250,250,500,100
};
int windowsShutdownMusicSize = 0;
int musicPosition = 0;
int currentSong = 0;
unsigned long currentMillis;
unsigned long lastMillisNote = 0;
unsigned long lastMillisServo = 0;
unsigned long lastButtonPress = 0;
unsigned long lastSwitchUpdate = 0;
unsigned long lastEEPROMWrite = 0;
const int buttonPressDelay = 500;
const int switchUpdateDelay = 200;
const int eepromWriteDelay = 1000;
bool isLeftLed = false;
bool isSwitchOn = false;
bool poweredUp = false;
Servo servo;
int servoAngle = 90;
const int eepromAngleAddress = 0;
void setup() {
sendSerials();
Serial.end();
Serial.begin(baudRate);
Serial.println("Serial Ready");
pinMode(buzzerPin, OUTPUT);
pinMode(servoPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(switchPin, INPUT);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
servo.write(EEPROM.read(eepromAngleAddress));
servo.attach(servoPin);
servo.write(servoAngle);
currentMillis = millis();
lastMillisNote = millis();
alpiniMusicSize = ((sizeof(alpiniNotes) / sizeof(alpiniNotes[0])));
alpiniMusicSize = (sizeof(alpiniNotes) / sizeof(alpiniNotes[0]));
jingleBellsMusicSize = (sizeof(jingleBellsNotes) / sizeof(jingleBellsNotes[0]));
windowsShutdownMusicSize = (sizeof(windowsShutdownNotes) / sizeof(windowsShutdownNotes[0]));
checkAndResetAngle(1.0f);
}
void loop() {
openLid(0.7f);
if (isSwitchOn)
{
openLid(0.7f);
}
else
{
closeLid(0.7f);
}
playMusic(0.7f);
if (poweredUp)
{
if (currentSong == 0)
{
playMusicAlpini(0.7f);
}
if (currentSong == 1)
{
playMusicJingleBells(1.0f);
}
if (currentSong == 404)
{
playShutdownSong(1.0f);
}
}
checkButton();
checkSwitch();
updateEEPROM();
currentMillis = millis();
}
void updateEEPROM()
{
if (currentMillis >= lastEEPROMWrite + eepromWriteDelay)
{
Serial.println("Updating EEPROM...");
lastEEPROMWrite = millis();
int currentAngle = servo.read();
int savedAngle = EEPROM.read(eepromAngleAddress);
if (currentAngle != savedAngle)
{
Serial.print("Written Angle: ");
Serial.println(currentAngle);
EEPROM.update(eepromAngleAddress, currentAngle);
}
}
}
void checkButton()
{
if (digitalRead(buttonPin) == HIGH && isSwitchOn)
{
if (currentMillis >= lastButtonPress + buttonPressDelay)
{
lastButtonPress = millis();
if (currentSong < 2) {
currentSong++;
noTone(buzzerPin);
}
else {
currentSong = 0;
}
musicPosition = 0;
}
}
}
void checkSwitch()
{
if (currentMillis >= lastSwitchUpdate + switchUpdateDelay)
{
if (digitalRead(switchPin) == HIGH)
{
if (!isSwitchOn)
{
isSwitchOn = true;
currentSong = 0;
musicPosition = 0;
Serial.println("Switch On");
if (!poweredUp)
{
poweredUp = true;
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
}
}
}
else
{
if (isSwitchOn)
{
isSwitchOn = false;
currentSong = 404;
musicPosition = 0;
Serial.println("Switch Off");
}
}
lastSwitchUpdate = millis();
}
}
void openLid(float speed)
@ -109,15 +323,72 @@ void openLid(float speed)
}
else
{
servo.detach();
//servo.detach();
}
lastMillisServo = millis();
}
}
void closeLid(float speed)
{
void playMusic(float speed)
if (currentMillis >= lastMillisServo + (100.0 * 1 / speed))
{
if (servoAngle <= 90)
{
servoAngle++;
}
if (servoAngle < 90)
{
servo.write(servoAngle);
}
lastMillisServo = millis();
}
}
void checkAndResetAngle(float speed)
{
Serial.print("Checking Angle... ");
int angle = servo.read();
Serial.println(angle);
if (angle < 90)
{
for (int currentAngle = angle; currentAngle <= 90; currentAngle++)
{
Serial.println(currentAngle);
servo.write(currentAngle);
delay(100.0 * 1 / speed);
currentMillis = millis();
updateEEPROM();
}
} else if (angle > 90)
{
for (int currentAngle = angle; currentAngle >= 90; currentAngle--)
{
Serial.print("B: ");
Serial.println(currentAngle);
servo.write(currentAngle);
delay(100.0 * 1 / speed);
currentMillis = millis();
updateEEPROM();
}
}
Serial.println("Angle Fixed!");
}
void playMusicAlpini(float speed)
{
if (currentMillis >= lastMillisNote + alpiniDurations[musicPosition - 1] * 1 / speed)
{
@ -125,6 +396,7 @@ void playMusic(float speed)
if (!(alpiniNotes[musicPosition] == pause))
{
switchLed();
tone(buzzerPin, alpiniNotes[musicPosition], (alpiniDurations[musicPosition] * 1 / speed) - 10);
}
@ -139,5 +411,97 @@ void playMusic(float speed)
}
lastMillisNote = millis();
}
}
void playMusicJingleBells(float speed)
{
if (currentMillis >= lastMillisNote + jingleBellsDurations[musicPosition - 1] * 1 / speed)
{
noTone(buzzerPin);
if (!(jingleBellsNotes[musicPosition] == pause))
{
switchLed();
tone(buzzerPin, jingleBellsNotes[musicPosition], (jingleBellsDurations[musicPosition] * 1 / speed) - 10);
}
if (musicPosition < jingleBellsMusicSize)
{
musicPosition++;
}
else
{
musicPosition = 0;
}
lastMillisNote = millis();
}
}
void playShutdownSong(float speed)
{
if (currentMillis >= lastMillisNote + windowsShutdownDurations[musicPosition - 1] * 1 / speed)
{
noTone(buzzerPin);
if (!(windowsShutdownNotes[musicPosition] == pause))
{
switchLed();
tone(buzzerPin, windowsShutdownNotes[musicPosition], (windowsShutdownDurations[musicPosition] * 1 / speed) - 10);
}
if (musicPosition < windowsShutdownMusicSize)
{
musicPosition++;
}
else
{
musicPosition = 0;
currentSong = 405;
noTone(buzzerPin);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
}
lastMillisNote = millis();
}
}
void switchLed() {
if (isLeftLed)
{
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, HIGH);
}
else
{
digitalWrite(led2Pin, LOW);
digitalWrite(led1Pin, HIGH);
}
isLeftLed = !isLeftLed;
}
void sendSerials()
{
int baudRatesArraySize = sizeof(baudRates) / sizeof(baudRates[0]);
for (int i = 0; i < baudRatesArraySize; i++)
{
unsigned long currentBaudRate = baudRates[i];
Serial.end();
Serial.begin(currentBaudRate);
Serial.println();
Serial.println();
Serial.print("Set baud rate to ");
Serial.println(baudRate);
Serial.println();
Serial.end();
}
}

File diff suppressed because one or more lines are too long

View File

@ -6,7 +6,7 @@
All non-arduino files created by visual micro and all visual studio project or solution files can be freely deleted and are not required to compile a sketch (do not delete your own code!).
Note: debugger breakpoints are stored in '.sln' or '.asln' files, knowledge of last uploaded breakpoints is stored in the upload.vmps.xml file. Both files are required to continue a previous debug session without needing to compile and upload again
Hardware: Arduino Uno, Platform=avr, Package=arduino
Hardware: ATmega328P (Old Bootloader) (Arduino Nano), Platform=avr, Package=arduino
*/
#if defined(_VMICRO_INTELLISENSE)
@ -18,7 +18,7 @@
#define __AVR_ATmega328p__
#define F_CPU 16000000L
#define ARDUINO 108013
#define ARDUINO_AVR_UNO
#define ARDUINO_AVR_NANO
#define ARDUINO_ARCH_AVR
#define __cplusplus 201103L
#define _Pragma(x)