0

I am trying to make some LED lights to turn on when i flick a switch on my RC crontroller. I will connect the signal wire from RC controller to pin 8. The plan is: if i flick the swich, the LED's (pin 12,11,10,9) will turn on, and if i don't flick the swich the LED's wont turn on. This is a Arduino nano what will be connected on a drone.I found another question HERE, but there was no real answers. I get a error that say:

exit status 1 could not convert 'digitalWrite(8u, 1u)' from 'void' to 'bool'

How can i fix this? Should i write the code another way?

Arduino Code:

    void setup() {

  pinMode(4, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, INPUT);



}


void loop() {

  if (digitalWrite(8, HIGH));
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH); 
  else 
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);

  digitalWrite(4,HIGH);
  delay(1500);
  digitalWrite(4,LOW);
}
3
  • why has this question upvotes?
    – Juraj
    Commented Nov 25, 2018 at 21:12
  • 1
    Because it’s a clear, answerable question with an error message and (relatively) minimal reproduction @Juraj
    – RubberDuck
    Commented Nov 26, 2018 at 2:08
  • upvote is for "usefull". and I agree with the text in bubble over the downvote button
    – Juraj
    Commented Nov 26, 2018 at 6:07

2 Answers 2

1

You should be using digitalRead(8) instead of digitalWrite(8, HIGH). Your sketch is also missing some curly brackets.

void setup(){
  pinMode(4, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, INPUT);
}

void loop(){
  if(digitalRead(8) == HIGH){
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);
  } 
  else{
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
  }
  digitalWrite(4,HIGH);
  delay(1500);
  digitalWrite(4,LOW);
}

The use of delay() will block the sketch from doing anything else, and should be replaced with a millis() timer. Have a look at the BlinkWithoutDelay example in the IDE.

2
  • Can't i just make another void loop() and have the blinking there? Then it won't interfere with the if statement.
    – Thomas
    Commented Nov 25, 2018 at 21:14
  • You can only have 1 void loop(). There are several libraries available that are easier to use than writing your own code, such as avdweb.nl/arduino/timing/virtualdelay. This library lets you use a similar syntax as the delay() function, and is non blocking.
    – VE7JRO
    Commented Nov 25, 2018 at 21:31
1

This line of code is the source of your problem.

void loop() {

  if (digitalWrite(8, HIGH));

If expressions expect a boolean (true/false), but if you check the documentation for digitalWrite(), you’ll see it returns “Nothing” (i.e. void).

You need to use digitalRead() to check the current state of the pin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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