//--------------------- INCLUDE LIBRARIES ----------------------------
#include <Servo.h> // SERVO
#include <SPI.h> // OLED
#include <Wire.h> // OLED
#include <Adafruit_GFX.h> // OLED
#include <Adafruit_SSD1306.h> // OLED
//--------------------- OLED DEFINE ------------------------------------
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // OLED reset pin
#define I2C_ADDRESS 0x3C // Define address
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//--------------------- CREATE MOTOR OBJECTS ----------------------------
Servo Tip_Down; // servo object to control a servo
Servo Jaw; // servo object to control a servo
Servo servo_Z; // servo object to control a servo
Servo servo_X; // servo object to control a servo
Servo servo_C; // servo object to control a servo
Servo servo_Tip_Up; // servo object to control a servo
Servo servo_B; // servo object to control a servo
Servo servo_Sides; // servo object to control a servo
//--------------------- CREATE BUTTONS ---------------------------------------
int dial_value = 0; // Used to determine value of buttons
//--------------------- CREATE DISPLAY NAMES ------------------------------
char *sound_item[] = {"/ch/", "chair",
"/d/", "do",
"/g/", "go",
"/j/", "judge",
"/k/", "kick",
"/l/", "like",
"/n/", "no",
"/ng/", "sing",
"/r/", "retroflex",
"/s/", "sad",
"/sh/", "shut",
"/t/", "tap",
"/th/", "think",
"/th/", "voiceless",
"/z/", "zoo"};
//--------------------- VARIABLES USED WITHIN CODE -------------------------
int number_of_sounds = 15; // Used to divide poteniometer into the amount of sounds programmed
void setup() { // THIS LOOP RUNS ONE TIME TO "ACTIVATE" ELECTRONICS
Serial.begin(115200); // Rate of serial data transmission (bits per second "baud")
// OLED: Initalize and Define
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
// SERVO MOTORS: Define Pins
Tip_Down.attach(A1); // Tip Down
Jaw.attach(5); // Jaw
servo_Z.attach(7); // Z
servo_X.attach(6); // X
servo_C.attach(A2); // C
servo_Tip_Up.attach(A3); // Tip Up
servo_B.attach(A0); // B
servo_Sides.attach(9); // Sides
// SERVO MOTORS: Define Pins
int tip_down = 180; int tip_up = 160; int servo_b = 0; int servo_c = 180; int servo_sides = 3; int jaw = 20; int servo_x = 3; int servo_z = 180;
Tip_Down.write(tip_down); servo_Tip_Up.write(tip_up); servo_B.write(servo_b); servo_C.write(servo_c); servo_Sides.write(servo_sides); Jaw.write(jaw); servo_X.write(servo_x); servo_Z.write(servo_z); // Zero Motor Positions
// BUTTONS
pinMode(2, INPUT_PULLUP);
}
void loop() { // MAIN PROGRAM
// VARIABLES THAT LOOP 1 TIME
byte old_dial_value = 100; // compare if the poteniometer has entered a new state
unsigned int first; // used to change display only on first new iteration for poteniometer
unsigned int wanted_wait_time = 3000; // milliseconds. Time for the motors to pause in position
byte clicked = 1; // use to get rid of second movement to position
byte up_clicked = 1;
byte down_clicked = 1;
int time_past = 1000; // milliseconds
int dial_value;
int trigger = 0;
int button = 2;
int tip_down = 180; int tip_up = 160; int servo_b = 0; int servo_c = 180; int servo_sides = 3; int jaw = 20; int servo_x = 3; int servo_z = 180; // not linked to servo in setup
while(1){ // THIS LOOP RUNS INDEFINITLEY
dial_value = ceil(analogRead(A7)/73); // convert poteniometer signal to variable
if (old_dial_value != dial_value){ // if the rotary encoder changes its value, the screen will clear itself
display.clearDisplay();
first = 1; // updates the value of first to display the new sound
}
switch (dial_value){ // Switches between possible sounds from movement of rotary encoder
case 0: // ch
old_dial_value = 0; // tells program that it was in this state last for the next loop
if (first == 1){ // IF IT IS THE FIRST TIME THE KNOB HAS BEEN MOVED TO THIS CASE
first = 0; // Reset so the next iteration knows it is not the first time at this case
display.setTextSize(4); display.setTextColor(WHITE); display.setCursor(20,5); display.println(sound_item[0]); // display "/SOUND/"
display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(35,45); display.println(sound_item[1]); display.display(); // display "EXAMPLE"
}
if (digitalRead(button) == trigger){ // IF THE BUTTON IS CLICKED
Tip_Down.write(180); servo_Tip_Up.write(70); servo_B.write(0); servo_C.write(180); servo_Sides.write(3); Jaw.write(30); servo_X.write(30); servo_Z.write(180); // Position Motors// Moves tongue to wanted position
Serial.write(old_dial_value); // Send sound number to the audio arduino
delay(wanted_wait_time);
Tip_Down.write(tip_down); servo_Tip_Up.write(tip_up); servo_B.write(servo_b); servo_C.write(servo_c); servo_Sides.write(servo_sides); Jaw.write(jaw); servo_X.write(servo_x); servo_Z.write(servo_z); // Zero Motor Positions
}
break; // Exits case statment
case 1: // d
old_dial_value = 1; // tells program that it was in this state last for the next loop
if (first == 1){ // IF IT IS THE FIRST TIME THE KNOB HAS BEEN MOVED TO THIS CASE
first = 0; // Reset so the next iteration knows it is not the first time at this case
display.setTextSize(4); display.setTextColor(WHITE); display.setCursor(28,5); display.println(sound_item[2]); // display "/SOUND/"
display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(52,45); display.println(sound_item[3]); display.display(); }// display "EXAMPLE"
if (digitalRead(button) == trigger){ // IF THE BUTTON IS CLICKED
Tip_Down.write(180); servo_Tip_Up.write(100); servo_B.write(20); servo_C.write(160); servo_Sides.write(3); Jaw.write(30); servo_X.write(10); servo_Z.write(180); // Position Motors// Moves tongue to wanted position
Serial.write(old_dial_value); // Send sound number to the audio arduino
delay(wanted_wait_time);
Tip_Down.write(tip_down); servo_Tip_Up.write(tip_up); servo_B.write(servo_b); servo_C.write(servo_c); servo_Sides.write(servo_sides); Jaw.write(jaw); servo_X.write(servo_x); servo_Z.write(servo_z); // Zero Motor Positions
}
break; // Exits case statment
case 2: // g
old_dial_value = 2; // tells program that it was in this state last for the next loop
if (first == 1){ // IF IT IS THE FIRST TIME THE KNOB HAS BEEN MOVED TO THIS CASE
first = 0; // Reset so the next iteration knows it is not the first time at this case
display.setTextSize(4); display.setTextColor(WHITE); display.setCursor(28,5); display.println(sound_item[4]); // display "/SOUND/"
display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(52,45); display.println(sound_item[5]); display.display(); }// display "EXAMPLE"
if (digitalRead(button) == trigger){ // IF THE BUTTON IS CLICKED
Tip_Down.write(180); servo_Tip_Up.write(180); servo_B.write(0); servo_C.write(180); servo_Sides.write(3); Jaw.write(30); servo_X.write(0); servo_Z.write(180); // Position Motors// Moves tongue to wanted position
Serial.write(old_dial_value); // Send sound number to the audio arduino
delay(wanted_wait_time);
Tip_Down.write(tip_down); servo_Tip_Up.write(tip_up); servo_B.write(servo_b); servo_C.write(servo_c); servo_Sides.write(servo_sides); Jaw.write(jaw); servo_X.write(servo_x); servo_Z.write(servo_z); // Zero Motor Positions
}
break; // Exits case statment
case 3: // j (no j clay image)
old_dial_value = 3; // tells program that it was in this state last for the next loop
if (first == 1){ // IF IT IS THE FIRST TIME THE KNOB HAS BEEN MOVED TO THIS CASE
first = 0; // Reset so the next iteration knows it is not the first time at this case
display.setTextSize(4); display.setTextColor(WHITE); display.setCursor(28,5); display.println(sound_item[6]); // display "/SOUND/"
display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(45,45); display.println(sound_item[7]); display.display(); }// display "EXAMPLE"
if (digitalRead(button) == trigger){ // IF THE BUTTON IS CLICKED
Tip_Down.write(180); servo_Tip_Up.write(160); servo_B.write(0); servo_C.write(180); servo_Sides.write(3); Jaw.write(30); servo_X.write(0); servo_Z.write(180); // Position Motors// Moves tongue to wanted position
Serial.write(old_dial_value); // Send sound number to the audio arduino
delay(wanted_wait_time);
Tip_Down.write(tip_down); servo_Tip_Up.write(tip_up); servo_B.write(servo_b); servo_C.write(servo_c); servo_Sides.write(servo_sides); Jaw.write(jaw); servo_X.write(servo_x); servo_Z.write(servo_z); // Zero Motor Positions
}
break; // Exits case statment
case 4: //k
old_dial_value = 4; // tells program that it was in this state last for the next loop
if (first == 1){ // IF IT IS THE FIRST TIME THE KNOB HAS BEEN MOVED TO THIS CASE
first = 0; // Reset so the next iteration knows it is not the first time at this case
display.setTextSize(4); display.setTextColor(WHITE); display.setCursor(28,5); display.println(sound_item[8]); // display "/SOUND/"
display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(38,45); display.println(sound_item[9]); display.display(); }// display "EXAMPLE"
if (digitalRead(button) == trigger){ // IF THE BUTTON IS CLICKED
Tip_Down.write(180); servo_Tip_Up.write(180); servo_B.write(0); servo_C.write(180); servo_Sides.write(3); Jaw.write(30); servo_X.write(0); servo_Z.write(180); // Position Motors// Moves tongue to wanted position
Serial.write(old_dial_value); // Send sound number to the audio arduino
delay(wanted_wait_time);
Tip_Down.write(tip_down); servo_Tip_Up.write(tip_up); servo_B.write(servo_b); servo_C.write(servo_c); servo_Sides.write(servo_sides); Jaw.write(jaw); servo_X.write(servo_x); servo_Z.write(servo_z); // Zero Motor Positions
}
break; // Exits case statment
case 5:// l (ch but further forward)
old_dial_value = 5; // tells program that it was in this state last for the next loop
if (first == 1){ // IF IT IS THE FIRST TIME THE KNOB HAS BEEN MOVED TO THIS CASE
first = 0; // Reset so the next iteration knows it is not the first time at this case
display.setTextSize(4); display.setTextColor(WHITE); display.setCursor(28,5); display.println(sound_item[10]); // display "/SOUND/"
display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(45,45); display.println(sound_item[11]); display.display(); }// display "EXAMPLE"
if (digitalRead(button) == trigger){ // IF THE BUTTON IS CLICKED
Tip_Down.write(180); servo_Tip_Up.write(70); servo_B.write(0); servo_C.write(180); servo_Sides.write(3); Jaw.write(30); servo_X.write(50); servo_Z.write(180); // Position Motors// Moves tongue to wanted position
Serial.write(old_dial_value); // Send sound number to the audio arduino
delay(wanted_wait_time);
Tip_Down.write(tip_down); servo_Tip_Up.write(tip_up); servo_B.write(servo_b); servo_C.write(servo_c); servo_Sides.write(servo_sides); Jaw.write(jaw); servo_X.write(servo_x); servo_Z.write(servo_z); // Zero Motor Positions
}
break; // Exits case statment
case 6: // n
old_dial_value = 6; // tells program that it was in this state last for the next loop
if (first == 1){ // IF IT IS THE FIRST TIME THE KNOB HAS BEEN MOVED TO THIS CASE
first = 0; // Reset so the next iteration knows it is not the first time at this case
display.setTextSize(4); display.setTextColor(WHITE); display.setCursor(28,5); display.println(sound_item[12]); // display "/SOUND/"
display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(45,45); display.println(sound_item[13]); display.display(); }// display "EXAMPLE"
if (digitalRead(button) == trigger){ // IF THE BUTTON IS CLICKED
Tip_Down.write(180); servo_Tip_Up.write(160); servo_B.write(20); servo_C.write(160); servo_Sides.write(3); Jaw.write(30); servo_X.write(5); servo_Z.write(180); // Position Motors// Moves tongue to wanted position
Serial.write(old_dial_value); // Send sound number to the audio arduino
delay(wanted_wait_time);
Tip_Down.write(tip_down); servo_Tip_Up.write(tip_up); servo_B.write(servo_b); servo_C.write(servo_c); servo_Sides.write(servo_sides); Jaw.write(jaw); servo_X.write(servo_x); servo_Z.write(servo_z); // Zero Motor Positions
}
break; // Exits case statment
case 7: // ng
old_dial_value = 7; // tells program that it was in this state last for the next loop
if (first == 1){ // IF IT IS THE FIRST TIME THE KNOB HAS BEEN MOVED TO THIS CASE
first = 0; // Reset so the next iteration knows it is not the first time at this case
display.setTextSize(4); display.setTextColor(WHITE); display.setCursor(28,5); display.println(sound_item[14]); // display "/SOUND/"
display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(52,45); display.println(sound_item[15]); display.display(); }// display "EXAMPLE"
if (digitalRead(button) == trigger){ // IF THE BUTTON IS CLICKED
Tip_Down.write(180); servo_Tip_Up.write(170); servo_B.write(40); servo_C.write(180); servo_Sides.write(10); Jaw.write(30); servo_X.write(0); servo_Z.write(180); // Position Motors// Moves tongue to wanted position
Serial.write(old_dial_value); // Send sound number to the audio arduino
delay(wanted_wait_time);
Tip_Down.write(tip_down); servo_Tip_Up.write(tip_up); servo_B.write(servo_b); servo_C.write(servo_c); servo_Sides.write(servo_sides); Jaw.write(jaw); servo_X.write(servo_x); servo_Z.write(servo_z); // Zero Motor Positions
}
break; // Exits case statment
case 8: //r
old_dial_value = 8; // tells program that it was in this state last for the next loop
if (first == 1){ // IF IT IS THE FIRST TIME THE KNOB HAS BEEN MOVED TO THIS CASE
first = 0; // Reset so the next iteration knows it is not the first time at this case
display.setTextSize(4); display.setTextColor(WHITE); display.setCursor(28,5); display.println(sound_item[16]); // display "/SOUND/"
display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(11,45); display.println(sound_item[17]); display.display(); }// display "EXAMPLE"
if (digitalRead(button) == trigger){ // IF THE BUTTON IS CLICKED
Tip_Down.write(180); servo_Tip_Up.write(70); servo_B.write(30); servo_C.write(150); servo_Sides.write(3); Jaw.write(30); servo_X.write(0); servo_Z.write(180); // Position Motors// Moves tongue to wanted position
Serial.write(old_dial_value); // Send sound number to the audio arduino
delay(wanted_wait_time);
Tip_Down.write(tip_down); servo_Tip_Up.write(tip_up); servo_B.write(servo_b); servo_C.write(servo_c); servo_Sides.write(servo_sides); Jaw.write(jaw); servo_X.write(servo_x); servo_Z.write(servo_z); // Zero Motor Positions
}
break; // Exits case statment
case 9: // s (same as z)
old_dial_value = 9; // tells program that it was in this state last for the next loop
if (first == 1){ // IF IT IS THE FIRST TIME THE KNOB HAS BEEN MOVED TO THIS CASE
first = 0; // Reset so the next iteration knows it is not the first time at this case
display.setTextSize(4); display.setTextColor(WHITE); display.setCursor(28,5); display.println(sound_item[18]); // display "/SOUND/"
display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(28,45); display.println(sound_item[19]); display.display(); }// display "EXAMPLE"
if (digitalRead(button) == trigger){ // IF THE BUTTON IS CLICKED
Tip_Down.write(180); servo_Tip_Up.write(160); servo_B.write(0); servo_C.write(180); servo_Sides.write(30); Jaw.write(30); servo_X.write(0); servo_Z.write(180); // Position Motors// Moves tongue to wanted position
Serial.write(old_dial_value); // Send sound number to the audio arduino
delay(wanted_wait_time);
Tip_Down.write(tip_down); servo_Tip_Up.write(tip_up); servo_B.write(servo_b); servo_C.write(servo_c); servo_Sides.write(servo_sides); Jaw.write(jaw); servo_X.write(servo_x); servo_Z.write(servo_z); // Zero Motor Positions
}
break; // Exits case statment
case 10: // sh (same as s and z but Z servo lifts)
old_dial_value = 10; // tells program that it was in this state last for the next loop
if (first == 1){ // IF IT IS THE FIRST TIME THE KNOB HAS BEEN MOVED TO THIS CASE
first = 0; // Reset so the next iteration knows it is not the first time at this case
display.setTextSize(4); display.setTextColor(WHITE); display.setCursor(28,5); display.println(sound_item[20]); // display "/SOUND/"
display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(44,45); display.println(sound_item[21]); display.display(); }// display "EXAMPLE"
if (digitalRead(button) == trigger){ // IF THE BUTTON IS CLICKED
Tip_Down.write(180); servo_Tip_Up.write(160); servo_B.write(0); servo_C.write(180); servo_Sides.write(30); Jaw.write(30); servo_X.write(0); servo_Z.write(170); // Position Motors// Moves tongue to wanted position
Serial.write(old_dial_value); // Send sound number to the audio arduino
delay(wanted_wait_time);
Tip_Down.write(tip_down); servo_Tip_Up.write(tip_up); servo_B.write(servo_b); servo_C.write(servo_c); servo_Sides.write(servo_sides); Jaw.write(jaw); servo_X.write(servo_x); servo_Z.write(servo_z); // Zero Motor Positions
}
break; // Exits case statment
case 11: // t
old_dial_value = 11; // tells program that it was in this state last for the next loop
if (first == 1){ // IF IT IS THE FIRST TIME THE KNOB HAS BEEN MOVED TO THIS CASE
first = 0; // Reset so the next iteration knows it is not the first time at this case
display.setTextSize(4); display.setTextColor(WHITE); display.setCursor(18,5); display.println(sound_item[22]); // display "/SOUND/"
display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(40,45); display.println(sound_item[23]); display.display(); }// display "EXAMPLE"
if (digitalRead(button) == trigger){ // IF THE BUTTON IS CLICKED
Tip_Down.write(180); servo_Tip_Up.write(70); servo_B.write(40); servo_C.write(180); servo_Sides.write(3); Jaw.write(30); servo_X.write(5); servo_Z.write(180); // Position Motors// Moves tongue to wanted position
Serial.write(old_dial_value); // Send sound number to the audio arduino
delay(wanted_wait_time);
Tip_Down.write(tip_down); servo_Tip_Up.write(tip_up); servo_B.write(servo_b); servo_C.write(servo_c); servo_Sides.write(servo_sides); Jaw.write(jaw); servo_X.write(servo_x); servo_Z.write(servo_z); // Zero Motor Positions
}
break; // Exits case statment
case 12: // th (Voiced)
old_dial_value = 12; // tells program that it was in this state last for the next loop
if (first == 1){ // IF IT IS THE FIRST TIME THE KNOB HAS BEEN MOVED TO THIS CASE
first = 0; // Reset so the next iteration knows it is not the first time at this case
display.setTextSize(4); display.setTextColor(WHITE); display.setCursor(28,5); display.println(sound_item[24]); // display "/SOUND/"
display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(10,45); display.println(sound_item[25]); display.display(); }// display "EXAMPLE"
if (digitalRead(button) == trigger){ // IF THE BUTTON IS CLICKED
Tip_Down.write(180); servo_Tip_Up.write(160); servo_B.write(0); servo_C.write(180); servo_Sides.write(3); Jaw.write(180); servo_X.write(40); servo_Z.write(180); // Position Motors// Moves tongue to wanted position
Serial.write(old_dial_value); // Send sound number to the audio arduino
delay(wanted_wait_time);
Tip_Down.write(tip_down); servo_Tip_Up.write(tip_up); servo_B.write(servo_b); servo_C.write(servo_c); servo_Sides.write(servo_sides); Jaw.write(jaw); servo_X.write(servo_x); servo_Z.write(servo_z); // Zero Motor Positions
}
break; // Exits case statment
case 13: // th (Voiceless)
old_dial_value = 13; // tells program that it was in this state last for the next loop
if (first == 1){ // IF IT IS THE FIRST TIME THE KNOB HAS BEEN MOVED TO THIS CASE
first = 0; // Reset so the next iteration knows it is not the first time at this case
display.setTextSize(4); display.setTextColor(WHITE); display.setCursor(20,5); display.println(sound_item[26]); // display "/SOUND/"
display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(10,45); display.println(sound_item[27]); display.display(); }// display "EXAMPLE"
if (digitalRead(button) == trigger){ // IF THE BUTTON IS CLICKED
Tip_Down.write(180); servo_Tip_Up.write(160); servo_B.write(0); servo_C.write(180); servo_Sides.write(3); Jaw.write(180); servo_X.write(40); servo_Z.write(180); // Position Motors// Moves tongue to wanted position
Serial.write(old_dial_value); // Send sound number to the audio arduino
delay(wanted_wait_time);
Tip_Down.write(tip_down); servo_Tip_Up.write(tip_up); servo_B.write(servo_b); servo_C.write(servo_c); servo_Sides.write(servo_sides); Jaw.write(jaw); servo_X.write(servo_x); servo_Z.write(servo_z); // Zero Motor Positions
}
break; // Exits case statment
case 14: // z (same as s)
old_dial_value = 14; // tells program that it was in this state last for the next loop
if (first == 1){ // IF IT IS THE FIRST TIME THE KNOB HAS BEEN MOVED TO THIS CASE
first = 0; // Reset so the next iteration knows it is not the first time at this case
display.setTextSize(4); display.setTextColor(WHITE); display.setCursor(28,5); display.println(sound_item[28]); // display "/SOUND/"
display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(45,45); display.println(sound_item[29]); display.display(); }// display "EXAMPLE"
if (digitalRead(button) == trigger){ // IF THE BUTTON IS CLICKED
Tip_Down.write(180); servo_Tip_Up.write(160); servo_B.write(0); servo_C.write(180); servo_Sides.write(30); Jaw.write(30); servo_X.write(0); servo_Z.write(180); // Position Motors// Moves tongue to wanted position
Serial.write(old_dial_value); // Send sound number to the audio arduino
delay(wanted_wait_time);
Tip_Down.write(tip_down); servo_Tip_Up.write(tip_up); servo_B.write(servo_b); servo_C.write(servo_c); servo_Sides.write(servo_sides); Jaw.write(jaw); servo_X.write(servo_x); servo_Z.write(servo_z); // Zero Motor Positions
}
break; // Exits case statment
} // End of Case
}// END OF WHILE
}