diff --git a/Gaia2Anniversary/.vs/Babito2Anniversary/v16/.suo b/Gaia2Anniversary/.vs/Babito2Anniversary/v16/.suo index d6dc408..6e90e46 100644 Binary files a/Gaia2Anniversary/.vs/Babito2Anniversary/v16/.suo and b/Gaia2Anniversary/.vs/Babito2Anniversary/v16/.suo differ diff --git a/Gaia2Anniversary/.vs/Babito2Anniversary/v16/ipch/AutoPCH/3c19b9977f65f591/.BABITO2ANNIVERSARY.VSARDUINO.ipch b/Gaia2Anniversary/.vs/Babito2Anniversary/v16/ipch/AutoPCH/3c19b9977f65f591/.BABITO2ANNIVERSARY.VSARDUINO.ipch index 40ac661..97c5037 100644 Binary files a/Gaia2Anniversary/.vs/Babito2Anniversary/v16/ipch/AutoPCH/3c19b9977f65f591/.BABITO2ANNIVERSARY.VSARDUINO.ipch and b/Gaia2Anniversary/.vs/Babito2Anniversary/v16/ipch/AutoPCH/3c19b9977f65f591/.BABITO2ANNIVERSARY.VSARDUINO.ipch differ diff --git a/Gaia2Anniversary/Babito2Anniversary/Babito2Anniversary.ino b/Gaia2Anniversary/Babito2Anniversary/Babito2Anniversary.ino index be7721e..e2ad372 100644 --- a/Gaia2Anniversary/Babito2Anniversary/Babito2Anniversary.ino +++ b/Gaia2Anniversary/Babito2Anniversary/Babito2Anniversary.ino @@ -5,13 +5,30 @@ */ #include +#include -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(); } } \ No newline at end of file diff --git a/Gaia2Anniversary/Babito2Anniversary/Babito2Anniversary.skb b/Gaia2Anniversary/Babito2Anniversary/Babito2Anniversary.skb index f41aac5..2f1a8b6 100644 Binary files a/Gaia2Anniversary/Babito2Anniversary/Babito2Anniversary.skb and b/Gaia2Anniversary/Babito2Anniversary/Babito2Anniversary.skb differ diff --git a/Gaia2Anniversary/Babito2Anniversary/Babito2Anniversary.skp b/Gaia2Anniversary/Babito2Anniversary/Babito2Anniversary.skp index b7dafd9..c62cdf6 100644 Binary files a/Gaia2Anniversary/Babito2Anniversary/Babito2Anniversary.skp and b/Gaia2Anniversary/Babito2Anniversary/Babito2Anniversary.skp differ diff --git a/Gaia2Anniversary/Babito2Anniversary/Babito2Anniversary.vcxproj b/Gaia2Anniversary/Babito2Anniversary/Babito2Anniversary.vcxproj index 39f2287..240a8cd 100644 --- a/Gaia2Anniversary/Babito2Anniversary/Babito2Anniversary.vcxproj +++ b/Gaia2Anniversary/Babito2Anniversary/Babito2Anniversary.vcxproj @@ -93,9 +93,9 @@ true true true - $(ProjectDir)..\Babito2Anniversary;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\libraries\Servo\src;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard;$(ProjectDir)..\..\..\ARDUIN~1\GAIA2A~1\BABITO~1;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\\lib\gcc\avr\7.3.0\include;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\avr\include;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\\lib\gcc\avr\7.3.0\include;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\avr\include-fixed;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\avr\include\avr;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.9.2\include;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.9.2\include;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.9.3\include;%(AdditionalIncludeDirectories) + $(ProjectDir)..\Babito2Anniversary;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\libraries\Servo\src;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM\src;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\variants\eightanaloginputs;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\\lib\gcc\avr\7.3.0\include;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\avr\include;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\\lib\gcc\avr\7.3.0\include;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\avr\include-fixed;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\avr\include\avr;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.9.2\include;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.9.2\include;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.9.3\include;%(AdditionalIncludeDirectories) $(ProjectDir)__vm\.Babito2Anniversary.vsarduino.h;%(ForcedIncludeFiles) - __AVR_atmega328p__;__AVR_ATmega328P__;__AVR_ATmega328p__;F_CPU=16000000L;ARDUINO=108013;ARDUINO_AVR_UNO;ARDUINO_ARCH_AVR;__cplusplus=201103L;_VMICRO_INTELLISENSE;%(PreprocessorDefinitions) + __AVR_atmega328p__;__AVR_ATmega328P__;__AVR_ATmega328p__;F_CPU=16000000L;ARDUINO=108013;ARDUINO_AVR_NANO;ARDUINO_ARCH_AVR;__cplusplus=201103L;_VMICRO_INTELLISENSE;%(PreprocessorDefinitions) true @@ -126,7 +126,7 @@ - + \ No newline at end of file diff --git a/Gaia2Anniversary/Babito2Anniversary/__vm/.Babito2Anniversary.vsarduino.h b/Gaia2Anniversary/Babito2Anniversary/__vm/.Babito2Anniversary.vsarduino.h index 55ce1c9..190d0cc 100644 --- a/Gaia2Anniversary/Babito2Anniversary/__vm/.Babito2Anniversary.vsarduino.h +++ b/Gaia2Anniversary/Babito2Anniversary/__vm/.Babito2Anniversary.vsarduino.h @@ -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)