C (Arduino) Will this work?

Thread Starter

Zane Finner

Joined Jan 29, 2018
30
My goal is to controll pitch with both range and buttons. I will add 3 more buttons that, when combined with a given range, will output a tone. Will this be sufficient for a single button so far? Can I continue with this and finish properly? What did I forget?
The reason I need to tell you if this is ready of not is because I need to bike to the library in order to upload it but I don't want to be there for a while.
Thanks in advance!
Code:
int myCounter=0;  //declare your variable myCounter and set to 0
float pingTime;  //time for ping to travel from sensor to target and return
float targetDistance; //Distance to Target in inches
float speedOfSound=776.5;

/*
10 per button
slide == 4
*/
const int noteC4N = 261.6255653005986;
const int noteC4S = 277.1826309768721;
const int noteD4N = 293.6647679174076;
const int noteD4S = 311.1269837220809;
const int noteE4N = 329.6275569128699;
const int noteF4N = 349.2282314330039;
const int noteF4S = 369.9944227116344;
const int noteG4N = 391.99543598174927;
const int noteG4S = 415.3046975799451;
const int noteA4N = 440;
const int noteA4S = 466.1637615180899;
const int noteB4N = 493.8833012561241;
const int noteC5N = 523.2511306011972;
long duration;
int distance;
//Buttons
const int b1 = 3;
const int b2 = 4;
const int b3 = 5;
const int b4 = 6;

//Pins renamed
int trigPin=13;
int echoPin=11;
void setup()
{
    pinMode(b1, INPUT);
    pinMode(b2, INPUT);
    pinMode(b3, INPUT);
    pinMode(b4, INPUT);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    pinMode(A0, OUTPUT);
}

void loop()
{
  digitalWrite(trigPin, LOW); //Set trigger pin low
  delayMicroseconds(2000); //Let signal settle
  digitalWrite(trigPin, HIGH); //Set trigPin high
  delayMicroseconds(15); //Delay in high state
  digitalWrite(trigPin, LOW); //ping has now been sent
  delayMicroseconds(10); //Delay in high state
 
  pingTime = pulseIn(echoPin, HIGH);  //pingTime is presented in microceconds
  pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
  pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
  targetDistance= speedOfSound * pingTime;  //This will be in miles, since speed of sound was miles per hour
  targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
  targetDistance= targetDistance*63360;
/*
   |    Now we need to make  |
   |  it so that when a      |
   |  range is applied with  |
   |  a button, a note or    |
   |  'tone' is generated.   |
*/
    if (b1, LOW && b2, LOW && b3, LOW && b4, LOW)    {
        noTone(A0);
    }

    if (b1 == HIGH && targetDistance < 10);    {
        tone(A0, noteC4N);
    }
    if (b1 == HIGH && targetDistance <= 10, targetDistance < 20)    {
        tone(A0, noteC4S);
    }
    if (b1 == HIGH && targetDistance <= 20, targetDistance < 30)    {
        tone(A0, noteD4N);
    }
    if (b1 == HIGH && targetDistance <= 30, targetDistance < 40)    {
        tone(A0, noteD4S);
    }
    if (b1 == HIGH && targetDistance <= 40, targetDistance < 50)    {
        tone(A0, noteE4N);
    }
    if (b1 == HIGH && targetDistance <= 50, targetDistance < 60)    {
        tone(A0, noteF4N);
    }
    if (b1 == HIGH && targetDistance <= 60, targetDistance < 70)    {
        tone(A0, noteF4S);
    }
    if (b1 == HIGH && targetDistance <= 70, targetDistance < 80)    {
        tone(A0, noteG4N);
    }
    if (b1 == HIGH && targetDistance <= 80, targetDistance < 90)    {
        tone(A0, noteG4S);
    }
}
 

WBahn

Joined Mar 31, 2012
32,823
What is a "const int" that has 13 places to the right of the decimal point?

Do you REALLY need or know these frequencies to 16 sig figs (or somewhere around 53 bits)?

How can anyone tell you whether or not you can continue and "finish properly" since you give no hint as to what it means to finish, let alone do so "properly".
 

-live wire-

Joined Dec 22, 2017
959
There is an exponential increase of about 1.06 times from one note to the next. A0 is 27.5Hz. This info should be very helpful and allow a lot less code. And the arduino has a 16MHz clock, meaning that it cannot do computation like that instantly. For higher frequencies this could be a problem.
 
Top