0

I am using the Arduino Uno. I am attempting to only light a red LED when both a blue LED is on and the push button is pressed. However, I keep receiving an error message stating too few arguments in my if statement. I don't know whether I am simply overlooking something in my code.

The code is as follows:

int redLEDPin=7;
int blueLEDPin=3;
int redLEDOn=2000;
int redLEDOff=1000;
int blueLEDOn=2000;
int blueLEDOff=1000;
int leverR=2;

void setup() {
    pinMode(redLEDPin, OUTPUT);
    pinMode(blueLEDPin, OUTPUT);
    pinMode(leverR, INPUT);
    }

void loop() {  
    digitalWrite(blueLEDPin, HIGH);
    delay(blueLEDOn);
    digitalWrite(blueLEDPin, LOW);
    delay(blueLEDOff);
    }

  if ((digitalWrite(blueLEDPin) == HIGH) && (digitalRead(leverR) == HIGH)) {
    digitalWrite(redLEDPin, HIGH);
    delay(redLEDOn);
    digitalWrite(redLEDPin, LOW);
    delay(redLEDOff);
    }
  else
  {
    digitalWrite(redLEDPin, LOW);
  } 
}

enter image description here

| improve this question | | | | |
  • 1
    Please include the complete error message in your question. – Craig Jun 28 '18 at 15:09
  • 1
    In function 'void loop()': 24:31: error: too few arguments to function 'void digitalWrite(uint8_t, uint8_t)' – ajf444 Jun 28 '18 at 15:18
  • 1
    I get the following error: testSE:22: error: expected unqualified-id before 'if'. Please include the error that you are getting (copy and paste it). There's an extraneous } just before your if – Greenonline Jun 28 '18 at 17:57
5

The digitalWrite statement in the if statement needs a value to write. It looks like you meant digitalRead, since you're comparing the result to something and the other condition is a digitalRead.

 if ((digitalWrite(blueLEDPin) == HIGH) && (digitalRead(leverR) == HIGH)) {

vs

 if ((digitalRead(blueLEDPin) == HIGH)  && (digitalRead(leverR) == HIGH)) {
| improve this answer | | | | |
  • I want the arduino to cause the blue LED to blink. Only when it blinks and the button is pressed will the red LED be activated. I think that means I need a digitalWrite for the blueLEDPin and a digitalRead for the leverR – ajf444 Jun 28 '18 at 15:24
  • Either way, digitalWrite needs 2 parameters: one for the pin and one for the value. digitalWrite also does not return anything so you can't test if it's equal to something. – that it guy Jun 28 '18 at 15:33
  • So would I be able to set another arduino pin as an input channel that corresponds to the output produced by the blue LED? – ajf444 Jun 28 '18 at 16:30
3

This is your loop() function:

void loop() {  
    digitalWrite(blueLEDPin, HIGH);
    delay(blueLEDOn);
    digitalWrite(blueLEDPin, LOW);
    delay(blueLEDOff);
    }

Your if() statement is outside of your loop. Check your brackets. In the Arduino IDE, if you put your cursor to the right of a bracket, it will highlight which bracket corresponds to that one.

Large Edit:

To blink two LEDs concurrently, you need to get rid of the delays. Please read about using the millis() function: https://learn.adafruit.com/multi-tasking-the-arduino-part-1/using-millis-for-timing

I don't know how to explain this succinctly, so I am providing the code (note that the on and off times for the LEDs are equal -- if you want them on longer than they are off, you can work on that part of it):

int redLEDPin=7;
int blueLEDPin=3;
int redLEDOn=2000;  //not used
int redLEDOff=250;
int blueLEDOn=2000;
int blueLEDOff=1000;  //not used
int leverR=2;

// the following variables are needed for timing and tracking LED state:
long previousMillisBlue = 0;
long previousMillisRed = 0;
bool ledStateBlue = LOW;
bool ledStateRed = LOW;

void setup() {
    pinMode(redLEDPin, OUTPUT);
    pinMode(blueLEDPin, OUTPUT);
    pinMode(leverR, INPUT_PULLUP); //I am using internal pullup resistor
}

void loop() {  
    unsigned long currentMillis = millis();

    if(currentMillis - previousMillisBlue > blueLEDOn) 
    {
      previousMillisBlue = currentMillis;

      if (ledStateBlue == LOW)
        {ledStateBlue = HIGH;}
      else
        {ledStateBlue = LOW;}

      digitalWrite(blueLEDPin, ledStateBlue);
    }

    //since we are tracking ledStateBlue above, we can reference it here
    if(digitalRead(leverR) == HIGH && ledStateBlue)
    {
      if(currentMillis - previousMillisRed > redLEDOff) 
      {
        previousMillisRed = currentMillis;

        if (ledStateRed == LOW)
          {ledStateRed = HIGH;}
        else
          {ledStateRed = LOW;}

        digitalWrite(redLEDPin, ledStateRed);
      }
    }
    else
    {digitalWrite(redLEDPin, LOW);}

}
| improve this answer | | | | |
  • @ajf444 please take a look at this and let me know if you have questions – J Duke Jun 28 '18 at 22:36
  • A more succinct way to write the ledStateBlue if/else would be: ledStateBlue = !ledStateBlue; (and similar for ledStateRed) – J Duke Jun 28 '18 at 22:43
1

Your if statement is outside of the loop() function.

Remove the } after delay(blueLEDOff);

| improve this answer | | | | |
  • Thank you for that. I removed the misplaced } but it still gives me an error message of too few arguments. – ajf444 Jun 28 '18 at 15:16
  • @ajf444: digitalWrite() takes 2 arguments (a pin# and a bool) - your calls only supply one. – JRobert Jun 28 '18 at 20:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.