/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
modified on 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Knob
*/
#include <Servo.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUMFLAKES 10 // Number of snowflakes in the animation example
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
static const unsigned char PROGMEM logo_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000 };
//#include <pitches.h>
Servo Arm; // Servo 1 zuweisen
// maximaler Ohr Winkel. Je nach Tassen Höhe anpassen
int max_Winkel=20;
int min_Winkel=100;
// notes in the melody:
//int melody_1[] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations_1[] = {4, 8, 8, 4, 4, 4, 4, 4};
int LED1=3; // LED 3 minuten
int LED2=5; // LED 5 minuten
int LED3=6; // LED 6 minuten
int speakerOut = 10; // Piezo Lautsprecher
int Time=3; // Time for Tea in minuten
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int val_old; // variable to read the value from the analog pin
int val_pot; // variable to read the value from the analog pin
int val_pot_old; // variable to read the value from the analog pin
int db=2; // Debuglevel
void setup() {
Serial.begin(15200);
Serial.println("Setup Teeodohr3");
pinMode(speakerOut, OUTPUT); // Lautsprecher Ausgang
pinMode(LED1, OUTPUT); // LED 1
pinMode(LED2, OUTPUT); // LED 2
pinMode(LED3, OUTPUT); // LED 3
pinMode(A1, INPUT); // ON and minutes select switch
pinMode(A2, INPUT); // Start / Stop select switch
pinMode(A3, INPUT); // Start / Stop select switch
analogWrite(LED1, 255); // 3 Minuten LED einschalten
analogWrite(LED2, 0); // alle anderen aus
analogWrite(LED3, 0); // alle anderen aus
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(500); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
// Draw a single pixel in white
//display.drawPixel(10, 10, SSD1306_WHITE);
display.setTextColor(WHITE);
display.setCursor(10,10);
display.println("Hello Theodohr!");
// Show the display buffer on the screen. You MUST call display() after
// drawing commands to make them visible on screen!
display.display();
delay(500);
//Arm.attach(11); // attaches the servo Arm to pin D11 servo object
Arm.attach(9); // attaches the servo Arm to pin D11 servo object
Arm.write(min_Winkel); // tell servo 1 to go to position 10°
ON_OFF_Sound(); // Play Sound. Ready to make Tea
if (db >=1){
Serial.println(Time);
Serial.println(min_Winkel);
}
display.setTextSize(2); // Draw 2X-scale text
display.setCursor(10,20);
display.print("Time:");
display.setCursor(80,20);
display.println(" ");
display.setCursor(80,20);
display.println(Time);
display.display();
Serial.println("Setup Ende");
}
void loop() {
if (analogRead(A1) > 900 ) SelectNextTime(); // Mit Taster 1 Zeit einstellen
if (analogRead(A2) > 900 ) TeaTime() ; // Mit Taster 2 Tea Time Starten
if (analogRead(A3) > 900 ) TeaStop() ; // Mit Taster 3 Tea Time Reseten
ReadAnalog ();
}
// ------------------------------------------------------------------
// ------------------------------------------------------------------
void SelectNextTime(){
// Warten bis Taster losgelassen
if (db >=1){
Serial.print(analogRead(A1));
}
Serial.print("; ");
do { delay(20);
Serial.print("Select");
Serial.println(analogRead(A1));
} while (analogRead(A1)>500);
// Zeit einstellen. 3,5,7,10 & 12 minuten
switch (Time) {
case 3:
Time=5;
if (db >=1){
Serial.println(Time);
}
analogWrite(LED1, 0); // LED's
analogWrite(LED2, 255);
analogWrite(LED3, 0);
break;
case 5:
Time=6;
if (db >=1){
Serial.println(Time);
}
analogWrite(LED1, 0); // LED's
analogWrite(LED2, 0);
analogWrite(LED3, 255);
break;
case 6:
Time=8;
if (db >=1){
Serial.println(Time);
}
analogWrite(LED1, 255); // LED's
analogWrite(LED2, 0);
analogWrite(LED3, 255);
break;
case 8:
Time=10;
if (db >=1){
Serial.println(Time);
}
analogWrite(LED1, 0); // LED's
analogWrite(LED2, 255);
analogWrite(LED3, 255);
break;
case 10:
Time=3;
if (db >=1){
Serial.println(Time);
}
analogWrite(LED1, 255); // LED's
analogWrite(LED2, 0);
analogWrite(LED3, 0);
}
ReadAnalog ();
// Clear the buffer
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setCursor(10,20);
display.println("Hello Theodohr!");
display.setCursor(10,30);
display.print("Time-:");
display.setCursor(50,30);
display.println(" ");
display.setCursor(50,30);
//display.println(Time);
display.println(val_pot);
display.display();
}
// ----------------------------
void TeaTime () {
// Warten bis Taster losgelassen
do { delay(20); }while (analogRead(A2)>500);
// Schleife für die Anzahl der Minuten
for (int i = 1; i <= Time; i++){
if (db >=2){
Serial.print("Dauer: ");
Serial.println(i);
}
// Tee Beutel langsam abwärts bis zum eingestellten Winkel
for (int p = min_Winkel; p >=max_Winkel; p--){
if (db >=2){
Serial.println("runter --");
//Serial.println(p);
}
Arm.write(p);
delay(20);
}
// Diese Schleife dauert 1minute.
for (int x=0;x<=20;x++){
if (db >=2){
Serial.print("Schleife: ");
Serial.println(x);
}
// Kurz vor ende der Minute den Beutel anheben.
if (x==19){
for (int p = max_Winkel; p <=min_Winkel; p++){
if (db >=2){
Serial.println("heben ++ ");
}
Arm.write(p); // tell servo 1 to go to position xx degree out of Tea slowly !!
delay(20);
}
}
// LED's fade in /out in increments of 5 points:
for (int fadeValue = 15 ; fadeValue <= 250; fadeValue += 5) {
analogWrite(LED1, fadeValue);
analogWrite(LED2, 255-fadeValue);
analogWrite(LED3, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
if (analogRead(A2) > 900 ){
if (db >=2){
Serial.println("break 1");
}
break; // Tea Time Stoppen
}
}
// LED's fade in / out in increments of 5 points:
for (int fadeValue = 250 ; fadeValue >= 15; fadeValue -= 5) {
analogWrite(LED1, fadeValue);
analogWrite(LED2, 255-fadeValue);
analogWrite(LED3, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
if (analogRead(A2) > 900 ){
if (db >=2){
Serial.println("break 2");
}
//TeaReset ();
break; // Tea Time Stoppen
}
}
if (analogRead(A2) > 900 ){
if (db >=2){
Serial.println("break 3");
}
break; // Tea Time Stoppen
}
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setCursor(10,10);
display.println("Hello Theodohr!");
display.setCursor(10,30);
display.print("Time-:");
display.setCursor(50,30);
display.println(" ");
display.setCursor(50,30);
//display.println(Time);
display.println(val_pot);
display.setCursor(10,50);
display.print("Dauer: ");
display.setCursor(50,50);
display.println(" ");
display.setCursor(50,50);
//display.println(Time);
display.println(x);
display.display();
}
if (analogRead(A2) > 900 ){
if (db >=2){
Serial.println("break 4");
}
break; // Tea Time Stoppen
}
}
// Arm nach oben
for (int p = max_Winkel; p >=min_Winkel; p--){
if (db >=2){
Serial.println("rauf --");
}
Arm.write(p);
delay(20);
}
// Warten bis Taster losgelassen
do { delay(20); }while (analogRead(A2)>500);
ON_OFF_Sound(); // Play sound ready to make Tea
// Zeit wieder auf 3 min.
Time=3;
analogWrite(LED1, 255); // 3 Minuten LED einschalten
analogWrite(LED2, 0); // alle anderen aus
analogWrite(LED3, 0); // alle anderen aus
}
void TeaStop () {
// Warten bis Taster losgelassen
do {
delay(20);
} while (analogRead(A3) > 500);
ON_OFF_Sound(); // Play sound ready to make Tea
// Zeit wieder auf 3 min.
Time = 3;
analogWrite(LED1, 255); // 3 Minuten LED einschalten
analogWrite(LED2, 0); // alle anderen aus
analogWrite(LED3, 0); // alle anderen aus
// Arm nach oben
for (int p = max_Winkel; p <=min_Winkel; p++){
Serial.println("stop rauf --");
Arm.write(p);
delay(20);
}
}
void TeaReset () {
ON_OFF_Sound(); // Play sound ready to make Tea
// Zeit wieder auf 3 min.
Time = 3;
analogWrite(LED1, 255); // 3 Minuten LED einschalten
analogWrite(LED2, 0); // alle anderen aus
analogWrite(LED3, 0); // alle anderen aus
// Arm nach oben
for (int p = max_Winkel; p <=min_Winkel; p++){
Serial.println("reset rauf --");
Arm.write(p);
delay(20);
}
}
void ON_OFF_Sound(){
for (int thisNote = 0; thisNote < 8; thisNote++) {
int noteDuration_1 = 1000 / noteDurations_1[thisNote];
//tone(speakerOut, melody_1[thisNote], noteDuration_1);
int pauseBetweenNotes = noteDuration_1 * 1.30;
delay(pauseBetweenNotes);
noTone(speakerOut);
}
}
void ReadAnalog(){
val_pot = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
//val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
val_pot = map(val_pot, 0, 1023, 2, 12); // scale it to use it with the servo (value between 0 and 180)
if (db >=2){
if (val_pot != val_pot_old){
Serial.println(val_pot);
}
}
val_pot_old = val_pot;
}