Go Down

Topic: Program Integrity (Read 1 time) previous topic - next topic

tuxduino

My 2 cents worth of suggestions...

This line:
Code: [Select]

digitalRead(button);

doesn't do anything. It's probably a leftover. It should be deleted.

Use more meaningful names for variables. For example:
Code: [Select]

const int btnPin = 7;
const int ledPin1 = 9;
const int ledPin2 = 10;
const int ledPin3 = 11;
const int ledPin4 = 13;
int btnState = 0;
int numBlinkCycles = 0;
int analogPin = 0;


Also, comments should not be a description of what the code line does, but rather why it does it or how it relates to the problem domain.

Use the handy map() function:
Code: [Select]

numBlinkCycles = analogRead(analogPin)
numBlinkCycles = map(numBlinkCycles, 0, 1023, 0, 60);


Use named constants:
Code: [Select]

const int MIN_DELAY_CNT = 5;

if (numBlinkCycles < MIN_DELAY_CNT) {
    numBlinkCycles = MIN_DELAY_CNT;
}


When one uses "good" variable and named constant names, comments often become superfluous. And that means the code is more readable. Which is a Good Thing (TM).

Quote
Looks reasonable to me. The delays will probably debounce it for you.


When value1 is 60, the sketch samples the button once in 2 minutes. IOW, the button pin has to read HIGH when the blink sequence ends and loop() restarts, or it won't be detected. This is sub optimal IMHO, however it could be OK in this particular application (it depends on the hardware context).

HTH

Nick Gammon

It's not going to affect the integrity of the program, but this is rather amusing:

Code: [Select]

const int led1 = 10;      // led output pin
const int led2 = 11;      // led output pin
const int led3 = 13;    // led on arduino board

...

  digitalWrite(led1, LOW);    //stop second led
  digitalWrite(led2, HIGH);   //start third led 


led1 is the second led and led2 is the third led. So what is led1? This is probably an example of comments confusing rather than helping.

brewie

Thanks to all above for your suggestions/comments - it's all valid stuff and this is a big learning curve for me. it's  one thing to write some code to make things go but another thing all together to write it efficiently and with readability. Back to the drawing board for a bit of a tidy up!
T.Brewerton

Go Up