top of page

Source code


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

const int FLEX_PIN0 = A0; // Pin connected to voltage divider output
const int FLEX_PIN1 = A1; // Pin connected to voltage divider output
const int FLEX_PIN2 = A2; // Pin connected to voltage divider output
const int FLEX_PIN3 = A3; // Pin connected to voltage divider output
const int FLEX_PIN4 = A4; // Pin connected to voltage divider output
// Measure the voltage at 5V and the actual resistance of your
// 47k resistor, and enter them below:
const float VCC = 3.3; // Measured voltage of Ardunio 3.3V line
const float R_DIV = 47500.0; // Measured resistance of 3.3k resistor

const float STRAIGHT_RESISTANCE = 37300.0; // resistance when straight
const float BEND_RESISTANCE = 90000.0; // resistance at 90 deg
int minangle0 = 1000;
int maxangle0 = 9000;

 


void setup() {
  negra = 640;
  pinMode(pushbutton, INPUT);
  Serial.begin(9600);
  pinMode(FLEX_PIN0, INPUT);
  pinMode(FLEX_PIN1, INPUT);
  pinMode(FLEX_PIN2, INPUT);
  pinMode(FLEX_PIN3, INPUT);
  pinMode(13,OUTPUT);
  pinMode(5,OUTPUT);
  digitalWrite(5,LOW);
}

void loop() {
digitalWrite(5,LOW);

  // Read the ADC, and calculate voltage and resistance from it
  int flexADC = analogRead(FLEX_PIN0);
  float flexV = flexADC * VCC / 1023.0;
  float flexR = R_DIV * (VCC / flexV - 1.0);

  // Use the calculated resistance to estimate the sensor's
  // bend angle:
  float angle = map(flexR, STRAIGHT_RESISTANCE, BEND_RESISTANCE,
                    0, 90.0);
  //  Serial.println("Bend: " + String(angle) + " degrees");
    Serial.println();
  int avg = (minangle0 + maxangle0) / 2;
  //Serial.println(angle);
  //Serial.println(minangle0);
  if (angle < minangle0) {
    x1 = 0;
  } else {
    if (angle < avg) {
      x1 = 1;
    } else if (angle >= avg) {
      x1 = 2;
    }
  }

  delay(50);

  flexADC = analogRead(FLEX_PIN1);
  //Serial.println(flexADC);
  flexV = flexADC * VCC / 1023.0;
  flexR = R_DIV * (VCC / flexV - 1.0);

  // Use the calculated resistance to estimate the sensor's
  // bend angle:
  angle = map(flexR, STRAIGHT_RESISTANCE, BEND_RESISTANCE,
              0, 90.0);
   Serial.println();
  avg = (minangle0 + maxangle0) / 2;
  //Serial.println(angle);
  //Serial.println(minangle0);
  if (angle < minangle0) {
    x2 = 0;
  } else {
    if (angle < avg) {
      x2 = 1;
    } else if (angle >= avg) {
      x2 = 2
    }
  }
  delay(50);
  // Read the ADC, and calculate voltage and resistance from it
  flexADC = analogRead(FLEX_PIN2);
  //Serial.println(flexADC);
  flexV = flexADC * VCC / 1023.0;
  flexR = R_DIV * (VCC / flexV - 1.0);
  //Serial.println("Resistance: " + String(flexR) + " ohms");

  // Use the calculated resistance to estimate the sensor's
  // bend angle:
  angle = map(flexR, STRAIGHT_RESISTANCE, BEND_RESISTANCE,
              0, 90.0);
  //  Serial.println("Bend: " + String(angle) + " degrees");
  //  Serial.println();
  avg = (minangle0 + maxangle0);
  //Serial.println(angle);
  //Serial.println(minangle0);
  if (angle < minangle0) {
    x3 = 0;
  } else {
    if (angle < avg) {
      x3 = 1;
    } else if (angle >= avg) {
      x3 = 2;
    }
  }
  delay(50);
  // Read the ADC, and calculate voltage and resistance from it
  flexADC = analogRead(FLEX_PIN3);
  //Serial.println(flexADC);
  flexV = flexADC * VCC / 1023.0;
  flexR = R_DIV * (VCC / flexV - 1.0);
  //Serial.println("Resistance: " + String(flexR) + " ohms");

  // Use the calculated resistance to estimate the sensor's
  // bend angle:
  angle = map(flexR, STRAIGHT_RESISTANCE, BEND_RESISTANCE,
              0, 90.0);
  //  Serial.println("Bend: " + String(angle) + " degrees");
  //  Serial.println();
  avg = (minangle0 + maxangle0);
  //Serial.println(angle);
  //Serial.println(minangle0);
  if (angle < minangle0) {
    x4 = 0;
  } else {
    if (angle < avg) {
      x4 = 1;
    } else if (angle >= avg) {
      x4 = 2;
    }
  }
  delay(50);
  // Read the ADC, and calculate voltage and resistance from it
  flexADC = analogRead(FLEX_PIN4);
  //Serial.println(flexADC);
  flexV = flexADC * VCC / 1023.0;
  flexR = R_DIV * (VCC / flexV - 1.0);
  //Serial.println("Resistance: " + String(flexR) + " ohms");

  // Use the calculated resistance to estimate the sensor's
  // bend angle:
  angle = map(flexR, STRAIGHT_RESISTANCE, BEND_RESISTANCE,
              0, 90.0);
  //  Serial.println("Bend: " + String(angle) + " degrees");
  //  Serial.println();
  avg = (minangle0 + maxangle0);
  //Serial.println(angle);
  //Serial.println(minangle0);
  if (angle < minangle0) {
    x5 = 0;
  } else {
    if (angle < avg) {
      x5 = 1;
    } else if (angle >= avg) {
      x5 = 2;
    }
  }
  delay(50);
  Serial.println(angle);
  //
  //


  value = digitalRead(pushbutton);
  //Serial.println(value);
  delay(50);
  //Serial.println(value);
  while (value == HIGH) { // If button is pressed, play desplacito melody . Link - https://create.arduino.cc/projecthub/msr048/despacito-melody-9dcab7
    notars(negra, NOTE_D5);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    notars(negra, NOTE_CS5);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    notars(negra / 2, NOTE_B4);
    notars(negra / 4, NOTE_FS4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    delay(negra / 4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    notars(negra / 4, NOTE_FS4);
    notars(negra / 4, NOTE_FS4);
    notars(negra / 4, NOTE_FS4);
    notars(negra / 4, NOTE_FS4);
    notars(negra / 4, NOTE_FS4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    notars(negra / 4, NOTE_B4);
    notars(negra / 4, NOTE_B4);
    notars(negra / 4, NOTE_B4);
    notars(negra / 2, NOTE_B4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }

    notars(negra / 4, NOTE_A4);

    notars(negra / 4, NOTE_B4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    delay(negra / 4);
    delay(negra / 4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    notars(negra / 4, NOTE_G4);
    delay(negra / 4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    notars(negra / 4, NOTE_G4);
    notars(negra / 4, NOTE_G4);
    notars(negra / 4, NOTE_G4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    notars(negra / 4, NOTE_G4);
    notars(negra / 4, NOTE_G4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    notars(negra / 4, NOTE_B4);
    notars(negra / 4, NOTE_B4);
    notars(negra / 4, NOTE_B4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    notars(negra / 2, NOTE_B4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }

    notars(negra / 4, NOTE_CS5);
    notars(negra / 4, NOTE_D5);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    delay(negra / 4);
    delay(negra / 4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    notars(negra / 4, NOTE_A4);

    delay(negra / 4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }

    notars(negra / 4, NOTE_A4);
    notars(negra / 4, NOTE_A4);
    notars(negra / 4, NOTE_A4);
    notars(negra / 4, NOTE_A4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    notars(negra / 4, NOTE_D5);
    notars(negra / 4, NOTE_CS5);
    notars(negra / 4, NOTE_D5);
    notars(negra / 4, NOTE_CS5);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    notars(negra / 2, NOTE_D5);
    notars(negra / 4, NOTE_E5);
    notars_jai(negra / 2, NOTE_E5, negra / 4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    //-----------------------------------------------
    tone(3, NOTE_CS5 , negra * 3 / 4);
    //-----------------------------------------------
    delay(negra / 4);
    delay(negra / 4);
    delay(negra / 4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    noTone(3);
    delay(negra / 4);
    delay(negra / 4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
    delay(negra / 4);
    delay(negra / 4);
    value = digitalRead(pushbutton);
    if (value != HIGH) {
      break;
    }
  }

  while (value == LOW) {
    //Serial.println("yes");
    int l[5];
    l[0] = x1;
    l[1] = x2;
    l[2] = x3;
    l[3] = x4;
    l[4] = x5;
    maincode(l);
    value = digitalRead(pushbutton);
  }


}

void maincode(int l[]) {
  int buzzer = 6;
  int flag = 0;
  if (l[0] == 0 and l[1] == 0) {
    if (l[2] == 0 and l[3] == 0) {
      if (l[4] == 0) {
        flag = 1;
        noTone(buzzer);
      }
    }
  }
  // Serial.println("hii");
  //Serial.println(flag);
  if (flag == 0) {
    int freq = 0;
    int count = 0;
    for (int i = 0; i < 5; i++) {
      if (l[i] == 1) {

int g = ran(count,i);
        freq += (200 + (i) * 400);
        count += 1;
      } else if (l[i] == 2) {

int g = ran(count,i);
        freq += (300 + i * 400);
        count += 1;
      }
    }
    tone(buzzer, freq / count);
  }


}
void notars(int noteDuration, int LaNota ) {

  //noteDuration=negra/4;
  tone(6, LaNota , noteDuration * 0.9);
  //      pauseBetweenNotes = noteDuration * 1.30;
  pauseBetweenNotes = noteDuration * 1.1;
  delay(pauseBetweenNotes);
  noTone(6);


}
void notars_jai(int noteDuration, int LaNota , int Desfase) {

  //noteDuration=negra/4;
  tone(6, LaNota , noteDuration * 0.9);
  //      pauseBetweenNotes = noteDuration * 1.30;
  pauseBetweenNotes = noteDuration * 1.1;
  delay(Desfase);

  delay(pauseBetweenNotes - Desfase);
  noTone(6);

 

int ran(int i,int p)

{

   int j=2;

   int k=4;

   int l= 6;

   int m=8

if (p==0)

   return i*k;

else if(p==1);

   return i*j;

else if(p==2);

   return i*l;

else

   return i*m;
}

  • Facebook - White Circle
  • Pinterest - White Circle
  • Instagram - White Circle

© 2023 by Jade&Andy. Proudly created with Wix.com

bottom of page