Home > OS >  How can I get this code to make LEDs linked to my arduino flash at either the value the dial outputs
How can I get this code to make LEDs linked to my arduino flash at either the value the dial outputs

Time:05-13

Before I get started I want to make it clear I am still a beginner in both C and Arduino electronics, so the answer may be painfully obvious to someone with even a week more experience than me.
But to me, it is a total mystery. Thank you for any help.

To begin this is the code:

int potPosition;
int delayTime;

void setup() {
   Serial.begin(9600);

   pinMode(13, OUTPUT);
   pinMode(12, OUTPUT);
   pinMode(11, OUTPUT);
   pinMode(10, OUTPUT);
   pinMode(9, OUTPUT);
 }

 void loop() {
   potPosition = analogRead(A0);
   Serial.println(potPosition);

   if (potPosition < 16) {
     delayTime == 16;
   } else if (potPosition >= 16){
       delayTime == analogRead(A0);
   }

   digitalWrite(13, HIGH);
   digitalWrite(12, HIGH);
   digitalWrite(11, HIGH);
   digitalWrite(10, HIGH);
   digitalWrite(9, HIGH);
   delay(delayTime);


   digitalWrite(13, LOW);
   digitalWrite(12, LOW);
   digitalWrite(11, LOW);
   digitalWrite(10, LOW);
   digitalWrite(9, LOW);
   delay(delayTime);

 }

This is all written in the Arduino Genius IDE and I am outputting it to a Sparkfun electronics Arduino with 5 LEDs connected to it controlled by a potentiometer, which sets the on and then off delay by outputting a value I take as milliseconds of delay to make the LEDs flash. All the electronic wiring is correct, as when I remove the if statement and just run it as normal, everything works as I'd expect.

So you may be wondering, what's the issue? Well, currently when the potPosition exceeds a certain value - a delay value in ms - which is too low, the LEDs no longer appear to flash, as the delay is far to small for the flash to be visible. This is an expected outcome, as a delay of nearing 0ms is extremely small.

What I am trying to do with my if statement:

if (potPosition < 16) {
     delayTime == 16;
   } else if (potPosition >= 16){
       delayTime == analogRead(A0);
   }

is if the potPosition - the value of the dial which is the ms delay - goes below a certain number (the last value which is a long enough delay for a flashing effect to still be visible) set my delayTime back to that value and if the potPosition is that value or anything higher, then set the delayTime to whatever delay the potentiometer is outputting.

However, currently, when I download this code onto my Arduino all my lights appear dim (the same way they do when my delay time is extremely low) no matter what value my potentiometer is outputting, which is not what I want it to do, and I have no idea why my if statement is not resetting my delay time correctly.

I've never really coded C before starting on a Sparkfun electronics kit, and only ever used Python, so I may be missing a key aspect of programming in C , but I have no idea what that would be.

Any help with this issue would be great, and I am more than happy to answer any questions.

CodePudding user response:

This delayTime == analogRead(A0); and this delayTime == 16; are comparisons, i.e. delayTime remains uninitialised and you get undefined behaviour.
I bet you want to write a value there, as in delayTime = analogRead(A0); or maybe delayTime = potPosition; (same for the other ==). The latter seems more plausible to me, so as to only have one analog read per loop iteration.

  • Related