Efficiency

Hello, I am new to Arduino, and wanted to know if there was a more efficient way I could have done my coding.

// Constants
// Pins 13, 2, and 3 will attach to the Buttons.
const int leftB = 13;
const int midB = 2;
const int rightB = 3;
// Pins 4, 5, and 6 will have the LED.
const int left = 4;
const int mid = 5;
const int right = 6;

// Variables

int leftS = 0;
int midS = 0;
int rightS = 0;

// The setup routine runs once when you press reset:
void setup() {
// Outputs
pinMode (left, OUTPUT);
pinMode (mid, OUTPUT);
pinMode (right, OUTPUT);
// Inputs
pinMode (leftB, INPUT);
pinMode (midB, INPUT);
pinMode (rightB,INPUT);
}

// The loop routine runs over and over again forever:
void loop() {
// Button states are read.
leftS = digitalRead (leftB);
midS = digitalRead (midB);
rightS = digitalRead (rightB);
// Check which button is pressed.
if (leftS == HIGH) {
//left LED blinks
for (int l = 0; l = 10; ++l){
digitalWrite (left, HIGH);
delay(500);
digitalWrite (left, LOW);
delay(500);
}
}
else
if (midB == HIGH) {
//mid LED blinks
do {
digitalWrite (mid, HIGH);
delay(500);
digitalWrite (mid, LOW);
delay(500);
} while (midB == HIGH);
}
else
if (rightB == HIGH) {
//right LED blinks
for (int r = 0; r = 10; ++r){
digitalWrite (right, HIGH);
delay(500);
digitalWrite (right, LOW);
delay(500);
}
}
else {
// LEDS are off
digitalWrite (left, LOW);
digitalWrite (mid, LOW);
digitalWrite (right, LOW);
}

}

Signals.ino (1.54 KB)

Efficiency is for hardware. Does the sketch perform the functions you want accomplished at the speed you need? If so efficiency doesn't really matter does it?

It does what I want, and now that I think about it I guess I don't need to shorten the coding. Thanks so much.

There are different ways to write the code, but one of my best professors said: "don't optimize until you need to."

With that in mind, you could write your code as a state machine that operates based on the button presses. Instead of the if-else structure, you'd have multiple states.

It wouldn't make your code shorter, it would be longer. However, it would also be much more flexible for future changes.

It does what I want

Really?

    if (midB == HIGH) {
      //mid LED blinks
      do  {
        digitalWrite (mid, HIGH);
        delay(500);
        digitalWrite (mid, LOW);
        delay(500);
      } while (midB == HIGH);
    }

You want it to go into an infinite loop? You're strange.

Aha, turns out my partner for the project wired it so that the LED turns on while the button is pressed, but does not blink. Would you have any suggestions as to how I would prevent it from infinitely looping?

turns out my partner for the project wired it so that the LED turns on while the button is pressed, but does not blink.

That is nothing to do with the wiring. It is down to the program.

How to stop it looping forever ?

     if (midB == HIGH) {
      //mid LED blinks
      do  {
        digitalWrite (mid, HIGH);
        delay(500);
        digitalWrite (mid, LOW);
        delay(500);
      } while (midB == HIGH);
    }

Do something, such as reading an input, that changes the value of midB from HIGH to LOW, in the do/while loop.

UKHeliBob:
Do something, such as reading an input, that changes the value of midB from HIGH to LOW, in the do/while loop.

Just read up on the break function; would it be possible to break from the loop when the input of midB is LOW?

Schmoodled:

UKHeliBob:
Do something, such as reading an input, that changes the value of midB from HIGH to LOW, in the do/while loop.

Just read up on the break function; would it be possible to break from the loop when the input of midB is LOW?

Why not try it and see what happens?

The Arduino system allows you to do that much more quickly than asking questions here.

...R

It's not very efficient because it repeats code and is blocking. You want several LEDs to blink if their associated button is held. A class is a great approach for this. Here's an example.

// make an led blink while a button is held down
// create LED object with switch
class myBlinker
{
    byte ledPin;
    byte buttonPin;
    unsigned long blinkInterval;
    unsigned long lastBlinkMillis;

  public:

    myBlinker(byte led, byte button)
    {
      ledPin = led;
      buttonPin = button;
    }

    void begin(unsigned long interval)
    {
      pinMode(ledPin, OUTPUT);
      pinMode(buttonPin, INPUT_PULLUP);
      blinkInterval = interval;
    }

    void run()
    {
      if (digitalRead(buttonPin) == LOW)
      {
        unsigned long currentMillis = millis();
        if (currentMillis - lastBlinkMillis >= blinkInterval)
        {
          lastBlinkMillis = currentMillis;
          digitalWrite(ledPin, !digitalRead(ledPin));
        }
      }
      else digitalWrite(ledPin, LOW);
    }
};

const unsigned long BLINK_INTERVAL = 500UL;

myBlinker left(13, 2);
myBlinker middle(5, 2);
myBlinker right(6, 3);

void setup()
{
  left.begin(BLINK_INTERVAL);
  middle.begin(BLINK_INTERVAL);
  right.begin(BLINK_INTERVAL);
}

void loop()
{
  left.run();
  middle.run();
  right.run();
}

This sketch uses internal pullups, if you use external resistors you'll have to make some changes.

Schmoodled:

UKHeliBob:
Do something, such as reading an input, that changes the value of midB from HIGH to LOW, in the do/while loop.

Just read up on the break function; would it be possible to break from the loop when the input of midB is LOW?

      do  {
        digitalWrite (mid, HIGH);
        delay(500);
        digitalWrite (mid, LOW);
        delay(500);
      } while (midB == HIGH);

This loop will carry on until mibD goes LOW. You do not need to use the break command to get out of the loop but you must change the value of midB somehow.

@jimmy60 - I have very mixed feelings about classes on a small device like an Arduino.

In the case of your example I have a few objections ...

There is an awful lot of code wrapped round the important line if (currentMillis - lastBlinkMillis >= blinkInterval)

The example is too specific in assuming that the user wants to use a button on a pin to trigger the led

The user would have a useful technique with a wide reange of application if s/he learned the Blink Without Delay concept

If, for whatever reason (a bug, or a design limitation) the class doesn't do what the user wants the user will waste time figuring out how to "fix" the class that would be better spent on his/her own project.

...R