Sunday, June 19, 2016

Arduino Instrument Code and Fritzing

Code:
int trig = 10;
int echo = 11;
long duration;
long distance;
int force;

void setup() {
  pinMode(echo, INPUT);

  pinMode(trig, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  digitalWrite(trig, LOW); //triggers on/off and then reads data
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  distance = (duration / 2) * .0344;    //344 m/s = speed of sound. We're converting into cm



  int notes[7] = {261, 294, 329, 349, 392, 440, 494}; //Putting several notes in an array
  //          mid C  D   E   F   G   A   B

  force = analogRead(A0); //defining force as FSR data


  if (distance < 0 || distance > 50 || force < 100) { //if not presed and not in front

    noTone(12); //dont play music

  }

  else if ((force > 100)) {  //if pressed

    int sound = map(distance, 0, 50, 0, 6);  //map distance to the array of notes
    tone(12, notes[sound]);  //call a certain note depending on distance

  }


}

Fritzing:



3 comments:

  1. Arduino: 1.8.19 (Mac OS X), Board: "Arduino Uno"

    Sketch uses 4274 bytes (13%) of program storage space. Maximum is 32256 bytes.
    Global variables use 221 bytes (10%) of dynamic memory, leaving 1827 bytes for local variables. Maximum is 2048 bytes.
    avrdude: ser_open(): can't open device "/dev/cu.usbmodem14101": No such file or directory
    Problem uploading to board. See https://support.arduino.cc/hc/en-us/sections/360003198300 for suggestions.


    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.

    its Verifing its-self but not uploading

    ReplyDelete