Yunho and Kashyapa – Toy Electric Guitar

Concept

We made a very simple toy electric guitar that anyone can use and make music with just by pressing the buttons in the right order. The tempo of the notes can also be changed using the potentiometer, which challenges the user by seeing how quick they can play the guitar.

Initially, we wanted to use the potentiometer to change the volume of the speakers and we managed to use the potentiometer as a variable resistor almost to change how much current went into the speaker, but the speakers only got soft in the middle range of the potentiometer and got louder at the two extremes, which is unsuitable to use as a switch. Because of this we decided to use it to change the tempo of the notes instead.

Below is an image of the circuit.

Here is the code and video:

#include "sound.h"

int counter = 0;
int group = 0;
int playMode = 0;

byte But1, PBut1, But2, PBut2, But3, PBut3, But4, PBut4, But5, PBut5;

bool lastnote = false;

int But1Note [] = {NOTE_A2, NOTE_B2, NOTE_C3, NOTE_D3, NOTE_E3, NOTE_F3, NOTE_A3, NOTE_B3, NOTE_C4};
int But2Note [] = {NOTE_B2, NOTE_C3, NOTE_D3, NOTE_E3, NOTE_F3, NOTE_G3, NOTE_D4, NOTE_C4, NOTE_B3};
int But3Note [] = {NOTE_C3, NOTE_D3, NOTE_E3, NOTE_F3, NOTE_G3, NOTE_A3};

int ending [] = {NOTE_A5, NOTE_G5, NOTE_F5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_B3, NOTE_A3};

int play[3];

int octave = 0;

int toneNum;
unsigned long timer = 0;
unsigned long timer2 = 0;

void setup() {
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);

  Serial.begin(9600);
}

void loop() {

  if (millis() > timer2) {
    // 3 Buttons that plays 3 notes of the guitar solo
    But1 = digitalRead(7);
    if (But1 == HIGH && PBut1 == LOW) {
      counter = 0;
      for (int i=0; i<3; i++){
        play[i] = But1Note[i+3*group];
      }
    }
    PBut1 = But1;
  
    But2 = digitalRead(6);
    if (But2 == HIGH && PBut2 == LOW) {
      counter = 0;
      for (int i=0; i<3; i++){
        play[i] = But2Note[i+3*group];
      }
    }
    PBut2 = But2;
  
    But3 = digitalRead(5);
    if (But3 == HIGH && PBut3 == LOW) {
      if (group == 2) {}
      else{
        counter = 0;
        for (int i=0; i<3; i++){
        play[i] = But3Note[i+3*group];
        }  
      }
    }
    PBut3 = But3;
  
    // this button switches the notes to the next group of the solo
    But4 = digitalRead(4);
    if (But4 == HIGH && PBut4 == LOW) {
      if (group<2){
        group++;
      }
      else group = 0;
    }
    PBut4 = But4;
  
    // this button switches to the next octave
    But5 = digitalRead(2);
    if (But5 == HIGH && PBut5 == LOW) {
        group = 0;
      if (octave<2){
        octave++;
      }
      else if (octave == 2) {octave++; counter =300;}
      else {octave = 0;}
    }
    PBut5 = But5;
    
    timer2 = millis() + 10;
  }
  
  // input from Pot.
  long potVal = analogRead(A0);
  long mapVal = map(potVal, 0, 1023, 100, 350);

  // sound player
  if (millis() > timer) {
    
    if (counter > 2 && octave !=3) {noTone(3);}
    else if (octave == 3 && counter<21) {tone(3, ending[counter], mapVal+50);}
    else if (octave == 3 && counter>=21) {noTone(3);}
    else{
      tone(3, play[counter]*pow(2,octave), mapVal+50);
    }
    
    counter++;
    timer = millis() + mapVal;
  }
}

 

Leave a Reply