1

Here is my code:

#include <FastLED.h>

#define LED_PIN     7
#define NUM_LEDS    20

CRGB leds[NUM_LEDS];

void setup() {
    pinMode(A0, INPUT_PULLUP);
    FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
}

void loop() {
    if (digitalRead(A0) == HIGH) {
        TEST ();
    }

}
void TEST() {

    leds[0] = CRGB(255, 0, 0);
    FastLED.show();
    delay(500);
    leds[1] = CRGB(0, 255, 0);
    FastLED.show();
    delay(500);
    leds[2] = CRGB(0, 0, 255);
    FastLED.show();
    delay(500);
    leds[3] = CRGB(150, 0, 255);
    FastLED.show();
    delay(500);
    leds[4] = CRGB(255, 200, 20);
    FastLED.show();
    delay(500);
    leds[5] = CRGB(85, 60, 180);
    FastLED.show();
    delay(500);
    leds[6] = CRGB(50, 255, 20);
    FastLED.show();

}

Here's what I'm stuck on: The code is supposed to detect voltage on the A0 pin, ad trigger my test function to make the LEDs glow sequentially. My current issue is that it works, but only once. If I remove the voltage and retry it, the function does not execute. I'm not really sure what I am doing wrong. My end goal is to create code for sequential taillights for my car, this is just a proof of concept. Basically what I want to happen is for the Arduino to detect the voltage from the car's signal and create a sequential animation. I know for that to work I can't use the delay function, but I cat figure out how to make a timer that I can reset to zero. So any help with that issue would also be appreciated. Sorry if this is worded poorly or a simple solution, I am new to arduino.

273K
  • 29,503
  • 10
  • 41
  • 64
  • 2
    What `if()` statement? There isn't a single one in the posted code. – Schol-R-LEA Jul 25 '22 at 01:00
  • Shoot sorry that While loop is supposed to be an if statement. My bad. – Mohamad Saab Jul 25 '22 at 01:01
  • As a side-note, your `TEST` function will be more compact and maintainable if you place those colors in an array and then put the modify/show/delay logic in a loop. – paddy Jul 25 '22 at 01:02
  • Did you know you can [edit] your own question, and fix typos or minor mistakes? – Sam Varshavchik Jul 25 '22 at 01:04
  • Is `loop` a standardized function in Arduino / FastLED? If not, how are you invoking it? – paddy Jul 25 '22 at 01:07
  • Well, it would be a pretty weird cut/paste error that transformed "if" to a "while" in the ***middle*** of cut/pasted text. It's now been determined that the initial version of the shown code was not real code, but made-up code. It's unclear how can anyone be assured that the shown code is now the real code, instead of made-up code, and they won't be wasting their time trying to figure out a non-existent problem in made-up code. – Sam Varshavchik Jul 25 '22 at 01:08
  • @paddy it is a standard thing in arduino ide – Mohamad Saab Jul 25 '22 at 01:09
  • @SamVarshavchik Thanks for pointing out the edit feature, never used stack exchange before. I also changed my if statement to a while loop to see if that solved my issue, when that didn't work I came here but forgot to change it back. Sorry for the misunderstandig. – Mohamad Saab Jul 25 '22 at 01:11
  • Strip back some of the code. You mention something about `delay` being a problem. So, modify `TEST` to do something basic, such as every time it's called, it cycles the color of LED 0 through R -> G -> B -> R -> ... Don't invoke delay or anything else. Use this to prove that TEST is not being re-entered when re-applying the voltage. If it _is_ being re-entered, continue probing forward. If not, probe backward. – paddy Jul 25 '22 at 01:14
  • Well, taking all the information into consideration, there seems to be a very obvious debugging step to take: remove the entire `if` statement (just call `TEST()`). Based on all the provided information it is expected that the LEDs should now be glowing sequentially. If they are, this indicates a problem reading the pin state. If they're not, then it is not true that `loop` gets repeatedly called as part of the "standard thing in arduino ide", or the code crashes. Depending on the results of this experiment, it should be possible to figure out what the next step should be. – Sam Varshavchik Jul 25 '22 at 01:15
  • And since you "never used stackexchange before", before posting your first question did you take the [tour], read the [help], and understand Stackoverflow's requirements for a [mre]? – Sam Varshavchik Jul 25 '22 at 01:17
  • @SamVarshavchik After doing what you said and running the function, It does not loop, so I think I misunderstood the way that function works. The way I understood it is basically that the `void setup()` is code that is executed once the Arduino starts and not again, and the `void loop()` is constantly looping. I'm not quite sure what I mixed up with because that seems to be the consensus online as well. – Mohamad Saab Jul 25 '22 at 01:23
  • @SamVarshavchik I did look at the help ceter, couldn't find anythig quite similar to my issue, and I'm pretty sure my code is a minimal reprex, but if it is not sorry. – Mohamad Saab Jul 25 '22 at 01:25
  • @paddy Sorry I should have saved that issue for a separate post, I meant that the delay was the next issue I wanted to solve, I currently am trying to figure out why I can't get my if statement to run twice. Basically what should happen is I can input voltage to the analog pin, and it will execute the function. But since it's supposed to be a turn signal, the voltage will constantly be removed and readded, so I need the conditional to be able to work more than once. – Mohamad Saab Jul 25 '22 at 01:29
  • Is it a requirement to call `FastLED.show()` at least once from the `loop` function? And maybe a delay? Consult the docs. Looking at the [Basic Usage](https://github.com/FastLED/FastLED/wiki/Basic-usage) wiki, I see no examples that result in falling straight through this function like you are doing when A0 is low. – paddy Jul 25 '22 at 01:35
  • [This arduino reference page](https://www.arduino.cc/reference/en/language/structure/sketch/loop/) indicates your understanding of loop() is correct. Have to look at stuff in your code. – Avi Berger Jul 25 '22 at 01:35
  • @paddy It turns out that I was correct. My issue was that I never told the LEDs to turn off once the sequence was complete. Because of this, The code was running but since the LEDs were already on I could not tell they were cycling through. Thank you much. – Mohamad Saab Jul 25 '22 at 01:53

1 Answers1

0

According to the rule of writing code, the description of the called function should be higher than the place of its call. I mean that the TEST() function should be between setup() and loop()

You need to turn off the LEDs after they have been turned on, this can be achieved with specific function from the FastLED library or as follows:

void loop() {
    for(size_t i = 0; i != 7; ++i) {
        leds[i] = CRGB(0, 0, 0);
    }    

    // your code
}

It is not necessary to compare the digital result with the HIGH signal value. If the value from the sensor is HIGH, then this is automatically converted to 1, which is true in the programming language and the condition is met. If the signal is LOW, then 0 is false and the condition is not met.

You can simply write it like this:

if (digitalRead(A0)) {
    TEST ();
}

Also, make sure what signal the sensor produces.

I see that in the pinMode(pin, mode) function you have connected the sensor to an analog pin that has a range of values ​​from 0 to 1023, but in the condition you are reading a digital signal with values ​​of 0 or 1.

You use the work mode in INPUT_PULLUP mode in pinMode(), it works in reverse. For example, if the button is pressed, then it gives a LOW signal, and if it is not pressed, then a HIGH one. If you need ordinary logic, then you should use the INPUT mode. But it all depends on your connection, and you can not get hung up on this.

It is desirable to connect digital sensors to digital pins, and analog sensors to analog ones. Moreover, if you have an analog sensor, then you do NOT need to write a pinMode().

Thus, if your analog signal outputs a value of 0, then it will be LOW for digital, and all other values ​​are converted to a HIGH signal. It would be more correct to read the analog signal with the analogRead() function.

Voltage-free analog signals can produce some kind of signal, so it's worth taking this into account in your program. It is necessary to measure a certain threshold after which your function should work, for example, if in a calm state the signal is not higher than 20-30, then you can write the condition as follows:

if (analogRead(A0) > 50) {
    TEST ();
}

Also note that you have delays in the function and during this time the voltage may drop, and the condition will not work the next time.

You can use a boolean variable that will capture the voltage and convert the value to true or false. Or you can use a counter.

Code:

#include <FastLED.h>

#define LED_PIN     7
#define NUM_LEDS    20

CRGB leds[NUM_LEDS];

void setup() {
    // if you have an analog sensor, then you do NOT need to write a pinMode()
    pinMode(A0, INPUT_PULLUP);    // maybe INPUT_PULLUP -> INPUT 
    FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
}

void TEST() {
    leds[0] = CRGB(255, 0, 0);
    FastLED.show();
    delay(500);
    leds[1] = CRGB(0, 255, 0);
    FastLED.show();
    delay(500);
    leds[2] = CRGB(0, 0, 255);
    FastLED.show();
    delay(500);
    leds[3] = CRGB(150, 0, 255);
    FastLED.show();
    delay(500);
    leds[4] = CRGB(255, 200, 20);
    FastLED.show();
    delay(500);
    leds[5] = CRGB(85, 60, 180);
    FastLED.show();
    delay(500);
    leds[6] = CRGB(50, 255, 20);
    FastLED.show();
}

void loop() {
    // turn off the LEDs
    for(size_t i = 0; i != 7; ++i) {
        leds[i] = CRGB(0, 0, 0);
    }  

    // make sure what signal the sensor produces
    if (analogRead(A0)) {
        TEST ();
    }
}