# Which of these is technically better?

These two programmes both do virtually the same thing - they will do the same thing once the equation in no. 2 is sorted out to be perfect. The only differences (apart from comments) is the middle section is coloured and bold (it seems this cannot be done to code, so the relevant section is in-between two lines)

``````//  Metronome  - This is a metronome that uses the resistance of the potentiometer to control the speed of the flashing LEDs

//  setting up the variables
int led1 = 13;
int led2 = 12;
int i;
int n = 3;
int sensPin = A0;
int sensVal = 0;
int delayVal = 0;

void setup()
{
//  these are the outputs
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}

void loop()
{
-------------------------------------------------------------------------------------------------------------------
//  first of all, I set it so that the value of the sensor is the voltage on pin A0
//  I then map the possible range of values onto the range of tempos it will provide for (20 - 280 bpm) calculation is '60 / bpm * 1000'
delayVal = map(sensVal, 0, 1023, 214, 3000);
--------------------------------------------------------------------------------------------------------------------
for(i = 0; i < n; i++) //  n here is equal to three - thus it does it three times before moving on. This could later be controlled a pot for time signature
{
digitalWrite(led1, HIGH);
delay(0.25 * delayVal); //  delayVal is the number of milliseconds per beat. '0.5 * delayVal' because there are two of them
digitalWrite(led1, LOW);
delay(0.75 * delayVal);
}
//  this section lights up two LEDs on the time signiature beat
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
delay(0.25 * delayVal);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(0.75 * delayVal);
}
``````

The other follows:

``````//  Metronome  - This is a metronome that uses the resistance of the potentiometer to control the speed of the flashing LEDs

//  setting up the variables
int led1 = 13;
int led2 = 12;
int i;
int n = 3;
int sensPin = A0;
int sensVal = 0;
int delayVal = 0;

void setup()
{
//  these are the outputs
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}

void loop()
{
-----------------------------------------------------------------------------------------------------
//  first of all, I set it so that the value of the sensor is the voltage on pin A0
//  these are the results of a calculation, namely '(60 / BPM) * 1000'
sensVal = map(sensVal, 0, 1023, 214, 3000);
-----------------------------------------------------------------------------------------------------
//  now we begin the actuall timing section
for(i = 0; i < n; i++) //  n here is equal to three - thus it does it three times before moving on. This could later be controlled a pot for time signature
{
digitalWrite(led1, HIGH);
delay(0.5 * delayVal); //  delayVal is the number of milliseconds per beat. '0.5 * delayVal' because there are two of them
digitalWrite(led1, LOW);
delay(0.5 * delayVal);
}
//  this section lights up two LEDs on the time signiature beat
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
delay(0.5 * delayVal);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(0.5 * delayVal);
}
``````

I suppose my question is - is it better to do the maths where possible, or to let the Arduino do this kind of maths - which would you recommend?

My post was rubbish - it is now all better (I hope!!)

The only difference between the code in between the lines is which variable the map() return value is being assigned to. I have a feeling that's not what you intended, because it isn't congruent with what your post says.

I am aware of that and my code works I was just enquiring as to what might be a more useful approach in the world of programming in general and what I should strive to do in more complicated programmes

JJC114: I am aware of that and my code works I was just enquiring as to what might be a more useful approach in the world of programming in general and what I should strive to do in more complicated programmes

Well, considering the difference, they don't do the same thing. So I guess my answer would be whichever one does what you want it to do.

Is the block below the two lines in the second set of code supposed to be using sensVal instead of delayVal?