Megan AltmanZhongyi Chen
Published

Interactive Keyboard for Toddlers

This keyboard uses buttons, LEDs, Servos, and Slide Switches to allow a child to interact with music.

IntermediateFull instructions provided3 hours551
Interactive Keyboard for Toddlers

Things used in this project

Hardware components

SparkFun RedBoard
SparkFun RedBoard
×1
SparkFun button
2 packs of 4, for 8 buttons total
×2
SparkFun RGB LED
2 packs of 5 for a total of 10. Generic LEDs can be used instead if simpler wiring and programming is desired
×2
SparkFun Switch
×1
SparkFun Speaker
×1
SparkFun Slide Potentiometer
×2
SparkFun Power Button
×1
Servo
×1
Solderless Breadboard Full Size
Solderless Breadboard Full Size
×1
SparkFun Mono Audio Amp Breakout - TPA2005D1
SparkFun Mono Audio Amp Breakout - TPA2005D1
×1
Tupperware
Two Identical pieces of tupperware are required to create the enclosure. We used these which I bought at the dollar store, and are about the size of a shoe box.
×2

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Hand Drill
Dremel

Story

Read more

Custom parts and enclosures

Enclosure-Outer Layer

This is a picture of the outer layer. Tape marks areas that will be see-through after painted, There are cutouts to allow access to buttons.

Enclosure- Inner Layer

This is a picture of the inner layer. Holes were drilled in the sides. One side has the speaker, the other is for the arduino. There were also holes cut away for the LEDs, buttons, switches, and servo to sit and wires to route through, this can all be changed based on preference casings available.

Schematics

LED Schematic

This is a way of wiring the LEDs in a way that requires minimal pins.

Code

Piano Code

Arduino
This is the program code for the toy. Just copy and paste this into the Arduino IDE and upload it your board.
#include "pitches.h"
#include <Servo.h>
Servo turner;
boolean gameMode = false;
int modeSwitch = 3;
int buttons1 = A0;
int buttons2 = A1;
int A = 4;
int B = 5;
int C = 6;
int D = 7;
int E = 8;
int F = 11;
int G = 12;
int H = A2;
int LEDs[8][4][2] = {
  {{B,A},{C,A},{D,A}},
  {{A,B},{C,B},{D,B}},
  {{A,C},{B,C},{D,C}},
  {{A,D},{B,D},{C,D}},
  {{A,E},{B,E},{C,E}},
  {{A,F},{B,F},{C,F}},
  {{A,G},{B,G},{C,G}},
  {{A,H},{B,H},{C,H}}};
int Notes[8] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5};

int totalS = 2; //This is the same value as the number of songs in Songs, adjust accordingly
//Follow this pattern if you want to add your own songs; songs are limited to only the notes in Notes and 50 total notes
int Songs[   2  ][50] = { 
  //First song example; each value corresponds with the index of the note in Notes
  {0,0,4,4,5,5,4,3,3,2,2,1,1,0,-1},
  //Second song example
  {4,2,4,4,2,4,5,4,3,2,1,2,3,2,3,4,0,0,0,0,0,1,2,3,4,4,1,1,3,2,1,0,-1}//,
  //A -1 at the end of the array indicates that the song has ended.
//{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}
  }; 
int songPos = 0;
int prevPos = -1;
int color = 0;
int currentSong = 0;

void setup() {
  Serial.begin(9600);
  set();
  turner.attach(10);
  pinMode(modeSwitch, OUTPUT);
  pinMode(A4, OUTPUT);
  pinMode(buttons1, OUTPUT);
  pinMode(buttons2, OUTPUT);
}

void set() {
  pinMode(A, INPUT);
  pinMode(B, INPUT);
  pinMode(C, INPUT);
  pinMode(D, INPUT);
  pinMode(E, INPUT);
  pinMode(F, INPUT);
  pinMode(G, INPUT);
  pinMode(H, INPUT);
}

void set2() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(H, OUTPUT);
}

void loop() {
  digitalWrite(modeSwitch, HIGH);
  digitalWrite(A4, HIGH);
  digitalWrite(buttons1, HIGH);
  digitalWrite(buttons2, HIGH);
  int val = analogRead(buttons1);
  int val2 = analogRead(buttons2);
  int modeVal = analogRead(modeSwitch);
  //Serial.println(analogRead(val));
  if (modeVal <= 100 && !gameMode) {
    gameMode = true;
    load("Game");
    currentSong = random(0, totalS);
  }
  else if (modeVal > 100 && gameMode) {
    //modeChange = true;
    gameMode = false;
    load("Normal");
    songPos = 0;
    currentSong = 0;
  }
  if (gameMode)
  {
    if (prevPos != songPos){
      color = random(0, 3);
      prevPos = songPos;
    }
    light(LEDs[Songs[currentSong][songPos]][color]);
  }
  if (val < 500)
  {
    handleActivity(7);
  }
  if (val+5 >= 955 && val-5 <= 955)
  {
    handleActivity(6);
  }
  if (val+5 >= 989 && val-5 <= 989)
  {
    handleActivity(5);
  }
  if (val+5 >= 1001 && val-5 <= 1001 && val2 > 500)
  {
    handleActivity(4);
  }
  if (val2 < 500)
  {
    handleActivity(3);
  }
  if (val2+5 >= 955 && val2-5 <= 955)
  {
    handleActivity(2);
  }
  if (val2+5 >= 989 && val2-5 <= 989)
  {
    handleActivity(1);
  }
  if (val2+5 >= 1001 && val2-5 <= 1001 && val > 500)
  {
    handleActivity(0);
  }
}

void playSound(int order, int colr)
{
    tone(9, Notes[order], 2000/4);
    light(LEDs[order][colr]);
    delay(2000/4*1.3);
    set();
    noTone(8);
}

void playSound(int order)
{
    tone(9, Notes[order], 2000/4);
    delay(2000/4*1.3);
    set();
    noTone(8);
}

// handles activty for both modes
void handleActivity(int order)
{
  if (gameMode && Songs[currentSong][songPos] == order)
  {
    playSound(order);
    if (songPos < 49 && Songs[currentSong][songPos] != -1){
      songPos++;
    }
    else {
      load("Game");
      songPos = 0;
      currentSong = random(0, totalS);
    }
  }
  else if (!gameMode)
    playSound(order, random(0, 3));
}

void load(String mode)
{
  for(int i = 0; i < 3; i++)
  {
    if (mode == "Normal"){
      turner.write(45);
    }
    else if (mode == "Game"){
      turner.write(135);
    }
    for(int j = 0; j < 8; j++)
    {
      light(LEDs[j][i]);
      delay(50);
      set();
    }
  }
}

void light(int pins[2])
{
  pinMode(pins[0], OUTPUT);
  pinMode(pins[1], OUTPUT);
  digitalWrite(pins[0], 1);
  if(digitalRead(pins[1]) == 1){
    digitalWrite(pins[1], 1);
    digitalWrite(pins[1], 0);
  } else {
    digitalWrite(pins[1], 0);
  }
}

pitches.h

Arduino
This is a supplementary file for the main program. It contains a list of possible pitches that the toy can play. In the Arduino IDE, create a new tab and copy and paste this code in.
/*************************************************
 * Public Constants
 *************************************************/

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

Credits

Megan Altman

Megan Altman

2 projects • 7 followers
College Freshman. I have been programming for several years with FIRST Robotics.
Zhongyi Chen

Zhongyi Chen

2 projects • 8 followers

Comments