3

I would like to make this code using the Arduino Uno. I am using three on/off switches and based on the truth table would like to turn on one of 8 LEDs. When I use digitalWrite(), I get an error code saying that there are too few arguments.

Here is the code

int x = A3;    // select the input pin for the switch
int y = A4;
int z = A5;

int ledPin1 = 2;      // select the pin for the LED
int ledPin2 = 3;
int ledPin3 = 4;
int ledPin4 = 7;
int ledPin5 = 8;
int ledPin6 = 9;
int ledPin7 = 12;
int ledPin8 = 13;

void setup(){

  // declare the ledPin as an OUTPUT:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  pinMode(ledPin8, OUTPUT);

  pinMode(x, INPUT);
  pinMode(y, INPUT);
  pinMode(z, INPUT);
}

void loop() {

  if ( (x = LOW) && (y = LOW) && (z = LOW))              //condition 1
  {
    digitalWrite (ledPin1 = HIGH); 
    digitalWrite (ledPin2 = LOW); 
    digitalWrite (ledPin3 = LOW); 
    digitalWrite (ledPin4 = LOW); 
    digitalWrite (ledPin5 = LOW); 
    digitalWrite (ledPin6 = LOW); 
    digitalWrite (ledPin7 = LOW); 
    digitalWrite (ledPin8 = LOW); 
  }

  if ( (x = LOW) && (y = LOW) && (z = HIGH))             //condition 2
  {
    ledPin1 = LOW; 
    ledPin2 = HIGH; 
    ledPin3 = LOW; 
    ledPin4 = LOW; 
    ledPin5 = LOW; 
    ledPin6 = LOW; 
    ledPin7 = LOW; 
    ledPin8 = LOW; 
  }

  if ( (x = LOW) && (y = HIGH) && (z = LOW))             //condition 3
  {
    ledPin1 = LOW; 
    ledPin2 = LOW; 
    ledPin3 = HIGH; 
    ledPin4 = LOW; 
    ledPin5 = LOW; 
    ledPin6 = LOW; 
    ledPin7 = LOW; 
    ledPin8 = LOW;
  }

  if ( (x = LOW) && (y = HIGH) && (z = HIGH))              //condition 4
  {
    ledPin1 = LOW; 
    ledPin2 = LOW; 
    ledPin3 = LOW; 
    ledPin4 = HIGH; 
    ledPin5 = LOW; 
    ledPin6 = LOW; 
    ledPin7 = LOW; 
    ledPin8 = LOW;
  }

  if ( (x = HIGH) && (y = LOW) && (z = LOW))               //condition 5
  {
    ledPin1 = LOW; 
    ledPin2 = LOW; 
    ledPin3 = LOW; 
    ledPin4 = LOW; 
    ledPin5 = HIGH; 
    ledPin6 = LOW; 
    ledPin7 = LOW; 
    ledPin8 = LOW;
  }

  if ( (x = HIGH) && (y = LOW) && (z = HIGH))               //condition 6
  {
    ledPin1 = LOW; 
    ledPin2 = LOW; 
    ledPin3 = LOW; 
    ledPin4 = LOW; 
    ledPin5 = LOW; 
    ledPin6 = HIGH; 
    ledPin7 = LOW; 
    ledPin8 = LOW;
  }

  if ( (x = HIGH) && (y = HIGH) && (z = LOW))                //condition 7
  {
    ledPin1 = LOW; 
    ledPin2 = LOW; 
    ledPin3 = LOW; 
    ledPin4 = LOW; 
    ledPin5 = LOW; 
    ledPin6 = LOW; 
    ledPin7 = HIGH; 
    ledPin8 = LOW;
  }

  if ( (x = HIGH) && (y = HIGH) && (z = HIGH))                //condition 8
  {
    ledPin1 = LOW; 
    ledPin2 = LOW; 
    ledPin3 = LOW; 
    ledPin4 = LOW; 
    ledPin5 = LOW; 
    ledPin6 = LOW; 
    ledPin7 = LOW; 
    ledPin8 = HIGH;
  }
}

Fritzing diagram

  • I used your code as a reference but when I uploaded it, it gives me "too few arguments to function". Any solutions? – khan Sep 18 '17 at 9:17
4

digitalWrite (ledPin1 = HIGH); should be digitalWrite (ledPin1, HIGH);

PS if ( (x = LOW) && (y = LOW) && (z = LOW)) should be if ( (x == LOW) && (y == LOW) && (z == LOW)). = is for assignment, == is for comparison.

| improve this answer | |
  • Thanks I changed all of that in the code. I just dont know why the simulation program is still not lighting the LED. – richard Jul 15 '15 at 19:17
  • you should simply write code to compute the state necessary for each led based on the truth table and write out that state to the specific digital pin only once; you would not need so many if/else conditions and so many calls to the digitalWrite function either; by the way your code will also work fine if you update your assignments to digitalWrite statements in your code in all the if/else blocks; – techniche Sep 18 '17 at 10:45
4

void loop() {

 if ( (x = LOW) && (y = LOW) && (z = LOW))              //condition 1
 {

Apart from the issue of needing == instead of = you are never reading those pins. In fact you are comparing the pin numbers (which will never change), not the data on those pins.

You need:

void loop() {

  if ( (digitalRead (x) == LOW) && 
       (digitalRead (y) == LOW) && 
       (digitalRead (z) == LOW))              //condition 1
    {

... and so on.


This is wrong too, I'm surprised it compiled:

   digitalWrite (ledPin1 = HIGH); 
   digitalWrite (ledPin2 = LOW); 
   digitalWrite (ledPin3 = LOW); 
   digitalWrite (ledPin4 = LOW); 
   digitalWrite (ledPin5 = LOW); 
   digitalWrite (ledPin6 = LOW); 
   digitalWrite (ledPin7 = LOW); 
   digitalWrite (ledPin8 = LOW); 

It should be:

  digitalWrite (ledPin1, HIGH); 
  digitalWrite (ledPin2, LOW); 
  digitalWrite (ledPin3, LOW); 
  digitalWrite (ledPin4, LOW); 
  digitalWrite (ledPin5, LOW); 
  digitalWrite (ledPin6, LOW); 
  digitalWrite (ledPin7, LOW); 
  digitalWrite (ledPin8, LOW); 

when i use digital write i get an error code saying that there are too few arguments. ... I just dont know why the simulation program is still not lighting the LED

Oh I see, it did not compile. No wonder the LEDs didn't turn on.

| improve this answer | |
2

Here's a little tip for you (it has nothing to do with your problem, it's just a good programming method). This will massively reduce the size of your program.

You have three boolean state variables, and you are comparing the state of all combinations - that's 8 combinations. If you take each boolean value as 1 bit of a value between 0 and 7 - that is values (as binary) 000, 001, 010, 011, 100, 101, 110, and 111 - then you can use much simpler methods for controlling your LEDs.

For instance, this (untested) code will turn three digital reads into a single number between 0 and 7:

uint8_t value = digitalRead(x) | (digitalRead(y) << 1) | (digitalRead(z) << 2);

x is the least significant bit, and z is the most significant. So value will now be a value between 0 and 7 representing the state of those three pins.

Now you can use that value variable to decide which pin to turn on. Here's another trick: Store you LED pins as an array. That means, instead of having ledPin1, ledPin2, etc, you just have a list of pin numbers known as ledPin[x]:

const uint8_t ledPin[8] = { 2, 3, 4, 7, 8, 9, 12, 13 };

Now you can use a for loop to iterate though that list and turn on the LED you want:

for (uint8_t i = 0; i < 8; i++) {
    if (i == value) {
        digitalWrite(ledPin[i], HIGH);
    } else {
        digitalWrite(ledPin[i], LOW);
    }
}

In fact, that can be further compressed, since HIGH and LOW are basically truth values (HIGH is 1, or true, and LOW is 0, or false):

for (uint8_t i = 0; i < 8; i++) {
    digitalWrite(ledPin[i], i == value);
}

So your whole program could look like this:

const uint8_t x = A3;    // select the input pin for the switch
const uint8_t y = A4;
const uint8_t z = A5;

const uint8_t ledPin[8] = { 2, 3, 4, 7, 8, 9, 12, 13 };

void setup() {
    for (uint8_t i = 0; i < 8; i++) {
        pinMode(ledPin[i], OUTPUT);
    }
    pinMode(x, INPUT);
    pinMode(y, INPUT);
    pinMode(z, INPUT);
}

void loop() {
    uint8_t value = digitalRead(x) | (digitalRead(y) << 1) | (digitalRead(z) << 2);
    for (uint8_t i = 0; i < 8; i++) {
        digitalWrite(ledPin[i], i == value);
    }
}

Saves a huge amount of typing and also ends up using considerably less Flash memory.

| improve this answer | |
  • Thanks I am still learning about arduino programming. this is definitely an easier way to go about it. thanks again for teaching me this! – richard Jul 16 '15 at 14:12
  • Great way to get beginners interested in arrays and booleans by way of showing their utility value. Nice. – Systembolaget Aug 22 '18 at 8:42
1

Since you mentioned you are new to programming, I am posting your own solution modified to work with right things; this code can be optimize to a great extent if you get good at programming; For your benefit, I will post both your approach and a slightly better approach

Your approach:

int x = A3;    // select the input pin for the switch
int y = A4;
int z = A5;

int ledPin1 = 2;      // select the pin for the LED
int ledPin2 = 3;
int ledPin3 = 4;
int ledPin4 = 7;
int ledPin5 = 8;
int ledPin6 = 9;
int ledPin7 = 12;
int ledPin8 = 13;

void setup(){

    // declare the ledPin as an OUTPUT:
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);
    pinMode(ledPin4, OUTPUT);
    pinMode(ledPin5, OUTPUT);
    pinMode(ledPin6, OUTPUT);
    pinMode(ledPin7, OUTPUT);
    pinMode(ledPin8, OUTPUT);

    pinMode(x, INPUT);
    pinMode(y, INPUT);
    pinMode(z, INPUT);
}

void loop() {

    if ( (x = LOW) && (y = LOW) && (z = LOW))              //condition 1
    {
        digitalWrite (ledPin1, HIGH); 
        digitalWrite (ledPin2, LOW); 
        digitalWrite (ledPin3, LOW); 
        digitalWrite (ledPin4, LOW); 
        digitalWrite (ledPin5, LOW); 
        digitalWrite (ledPin6, LOW); 
        digitalWrite (ledPin7, LOW); 
        digitalWrite (ledPin8, LOW); 
    }

    if ( (x = LOW) && (y = LOW) && (z = HIGH))             //condition 2
    {
        digitalWrite(ledPin1, LOW);  
        digitalWrite(ledPin2, HIGH); 
        digitalWrite(ledPin3, LOW);  
        digitalWrite(ledPin4, LOW);  
        digitalWrite(ledPin5, LOW);  
        digitalWrite(ledPin6, LOW);  
        digitalWrite(ledPin7, LOW);  
        digitalWrite(ledPin8, LOW);  
    }

    if ( (x = LOW) && (y = HIGH) && (z = LOW))             //condition 3
    {
        digitalWrite(ledPin1, LOW);  
        digitalWrite(ledPin2, LOW);  
        digitalWrite(ledPin3, HIGH); 
        digitalWrite(ledPin4, LOW);  
        digitalWrite(ledPin5, LOW);  
        digitalWrite(ledPin6, LOW);  
        digitalWrite(ledPin7, LOW);  
        digitalWrite(ledPin8, LOW); 
    }

    if ( (x = LOW) && (y = HIGH) && (z = HIGH))              //condition 4
    {
        digitalWrite(ledPin1, LOW);  
        digitalWrite(ledPin2, LOW);  
        digitalWrite(ledPin3, LOW);  
        digitalWrite(ledPin4, HIGH); 
        digitalWrite(ledPin5, LOW);  
        digitalWrite(ledPin6, LOW);  
        digitalWrite(ledPin7, LOW);  
        digitalWrite(ledPin8, LOW); 
    }

    if ( (x = HIGH) && (y = LOW) && (z = LOW))               //condition 5
    {
        digitalWrite(ledPin1, LOW);  
        digitalWrite(ledPin2, LOW);  
        digitalWrite(ledPin3, LOW);  
        digitalWrite(ledPin4, LOW);  
        digitalWrite(ledPin5, HIGH); 
        digitalWrite(ledPin6, LOW);  
        digitalWrite(ledPin7, LOW);  
        digitalWrite(ledPin8, LOW); 
    }

    if ( (x = HIGH) && (y = LOW) && (z = HIGH))               //condition 6
    {
        digitalWrite(ledPin1, LOW);  
        digitalWrite(ledPin2, LOW);  
        digitalWrite(ledPin3, LOW);  
        digitalWrite(ledPin4, LOW);  
        digitalWrite(ledPin5, LOW);  
        digitalWrite(ledPin6, HIGH); 
        digitalWrite(ledPin7, LOW);  
        digitalWrite(ledPin8, LOW); 
    }

    if ( (x = HIGH) && (y = HIGH) && (z = LOW))                //condition 7
    {
        digitalWrite(ledPin1, LOW);  
        digitalWrite(ledPin2, LOW);  
        digitalWrite(ledPin3, LOW);  
        digitalWrite(ledPin4, LOW);  
        digitalWrite(ledPin5, LOW);  
        digitalWrite(ledPin6, LOW);  
        digitalWrite(ledPin7, HIGH); 
        digitalWrite(ledPin8, LOW); 
    }

    if ( (x = HIGH) && (y = HIGH) && (z = HIGH))                //condition 8
    {
        digitalWrite(ledPin1, LOW);  
        digitalWrite(ledPin2, LOW);  
        digitalWrite(ledPin3, LOW);  
        digitalWrite(ledPin4, LOW);  
        digitalWrite(ledPin5, LOW);  
        digitalWrite(ledPin6, LOW);  
        digitalWrite(ledPin7, LOW);  
        digitalWrite(ledPin8, HIGH);
    }
}

Slightly better approach:(better programmers may be able to write even more optimized code)

int x = A3;    // select the input pin for the switch
int y = A4;
int z = A5;

int ledPin1 = 2;      // select the pin for the LED
int ledPin2 = 3;
int ledPin3 = 4;
int ledPin4 = 7;
int ledPin5 = 8;
int ledPin6 = 9;
int ledPin7 = 12;
int ledPin8 = 13;

void setup(){

  // declare the ledPin as an OUTPUT:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  pinMode(ledPin8, OUTPUT);

  pinMode(x, INPUT);
  pinMode(y, INPUT);
  pinMode(z, INPUT);
}

void loop() {
  int pinToTurnHigh = 2; // just making sure its a valid pin
  if ( (x = LOW) && (y = LOW) && (z = LOW))              //condition 1
  {
    pinToTurnHigh = ledPin1;
  }

  if ( (x = LOW) && (y = LOW) && (z = HIGH))             //condition 2
  {
    pinToTurnHigh = ledPin2;
  }

  if ( (x = LOW) && (y = HIGH) && (z = LOW))             //condition 3
  {
    pinToTurnHigh = ledPin3;
  }

  if ( (x = LOW) && (y = HIGH) && (z = HIGH))              //condition 4
  {
    pinToTurnHigh = ledPin4;
  }

  if ( (x = HIGH) && (y = LOW) && (z = LOW))               //condition 5
  {
    pinToTurnHigh = ledPin5;
  }

  if ( (x = HIGH) && (y = LOW) && (z = HIGH))               //condition 6
  {
    pinToTurnHigh = ledPin6;
  }

  if ( (x = HIGH) && (y = HIGH) && (z = LOW))                //condition 7
  {
    pinToTurnHigh = ledPin7;
  }

  if ( (x = HIGH) && (y = HIGH) && (z = HIGH))                //condition 8
  {
    pinToTurnHigh = ledPin8;
  }
  resetAllLEDs();
  digitalWrite(pinToTurnHigh, HIGH);
}

void resetAllLEDs()
{  
  digitalWrite(ledPin1, LOW);  
  digitalWrite(ledPin2, LOW);  
  digitalWrite(ledPin3, LOW);  
  digitalWrite(ledPin4, LOW);  
  digitalWrite(ledPin5, LOW);  
  digitalWrite(ledPin6, LOW);  
  digitalWrite(ledPin7, LOW);  
  digitalWrite(ledPin8, LOW);
}
| improve this answer | |
0

The sole error in digitalWrites is that digitalWrite (ledPin1 = HIGH); should be digitalWrite (ledPin1, HIGH); same for other digitalWrite calls;

"too few arguments to function" clearly implies that the number of arguments you are passing are less than expected by the function;

by the way your code will also work fine if you update your assignments to digitalWrite statements in your code in all the if/else blocks;

you can read more about the digitalWrite function in the link below https://www.arduino.cc/en/Reference/DigitalWrite

| improve this answer | |

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