0

I was trying to do a LED display that turns on a LED at pin 13 that goes on then turns off after a delay then the next pin (which is pin 12 counting down) will turn on and then turn off.

I also put in a potentiometer that can change the delay to make it faster or slower.

Here's the code:

float pinOn = 13;
int outMin = 0;
int outMax = 13;
float delayNum = 1000;

void setup () {
  for (int i=outMin; i<=outMax; i++) {
  pinMode(i, OUTPUT); }
  Serial.begin(9600);
}

void loop () {
  // Reads Potentiometer -- sets delay relative to the Potentiometer input
  int sensorValue = analogRead(A0);
  delayNum = sensorValue;
  // Turns on the Pin designated by 'pinOn'
  digitalWrite(pinOn, HIGH);
  delay(delayNum);
  digitalWrite(pinOn, LOW);
  // Checks if 'pinOn' is 0 which then resets it back to 13 if not 0 then counts down
  if (pinOn = 0) {
    pinOn + 13;
  } else {
    pinOn - 1;
  }
}

All it does is turn on pin 13 for a moment then permanently turns on pin 1 and 0 and it gets stuck like that.

  • Bugs like this are why everyone should learn to use a debugger, and enable all warnings on your compiler -- every modern compiler will catch if (a = b) and warn you about it if you enable all warnings and, ideally, make it treat warnings as errors. I also think this counts as a "general coding" problem; if you replaced the contents of the blocks with puts("foo") or other non-Arduino C statements, the answer would be exactly the same. – Nic Hartley Nov 29 '17 at 18:24
2

if (pinOn = 0) is doing an assignment (single =) not a comparison (double ==), so pinOn is begin set to 0 each time through the loop. The result of an assignment is the value being assigned. So it's the same as if ( 0 ), which is false.

Now, on to the two branches. pinOn + 13 is a statement and the result is not being assigned to anything. You need an equals sign somewhere:

if (pinOn == 0) { // == means "is equal to" 
  pinOn += 13; // same as pinOn = pinOn + 13
}
else {
  pinOn -= 1;
}
  • Thanks so much! I forgot that the comparator equals is supposed to be '==' not '=' :P it works now thank you! – Dudamesh 192 May 18 '17 at 2:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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