Automotive Flasher with current sensor

Attached is a simple project for a flasher for a motorcycle. It does verify but something was wrong with declaring some of the variables. I made it work but Im not sure I got them in the right place.

I would welcome any comments.

https://create.arduino.cc/editor/ppuxley/9a5d0549-a2da-4e28-8835-0e3b2acab5ae/preview

Thanks for looking

"something was wrong with declaring some of the variables" ...can you be more specific?

void setup() {
  // initialize Digital pin 12 as an output:
pinMode(12, OUTPUT);
  // initialize Analog pin 2 as an input:
pinMode(A5, INPUT);

}

void loop() {
  int flasherState;
  const long interval = 1000;           // interval at which to blink (milliseconds)
flasherState = analogRead(A5);

  
  if (flasherState > 562 ) 
  
  {
 delay (1000);
 digitalWrite (12,HIGH); 
 delay (interval);
 digitalWrite (12,LOW);
  } 
   
  else
  {
    digitalWrite (12,LOW);
  }
}

flasherState and interval were coming up as " not declared " when I had them in the set up section, but were ok when I moved them into the loop section. It verifies now so I guess it's ok, then?

ppuxley: flasherState and interval were coming up as " not declared " when I had them in the set up section, but were ok when I moved them into the loop section. It verifies now so I guess it's ok, then?

Variables that are declared in a function, like setup() or loop() are only visible inside that function. So a variable declared in setup() won't be visible to the code in loop().

If you need both functions to see those variables, declaring them as global (so putting them at the top of the code, above setup(), for example) is one way to do that. For example:

#define KFLSH_THRESHOLD     562
#define KINTERVAL_A         1000ul
#define KINTERVAL_B         1000ul

const byte pinOut = 12;
const byte pinIn = A5;

int flasherState;

void setup() 
{
    // initialize Digital pin 12 as an output:
    pinMode(pinOut, OUTPUT);
    // initialize Analog pin 2 as an input:
    pinMode(pinIn, INPUT);
    flasherState = analogRead(A5); 
    
}//setup

void loop() 
{    
    flasherState = analogRead(pinIn); 
    if( flasherState > KFLSH_THRESHOLD )
    {
        delay (KINTERVAL_A);
        digitalWrite (pinOut,HIGH);
        delay (KINTERVAL_B);
        digitalWrite (pinOut,LOW);
        
    }//if
    else
    {
        digitalWrite (pinOut,LOW);
        
    }//else
    
}//loop

Look up "variable scope" here: https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/

ppuxley: flasherState and interval were coming up as " not declared " when I had them in the set up section, but were ok when I moved them into the loop section. It verifies now so I guess it's ok, then?

The compiler can only spot syntactical errors (deviations from the syntax of the C language). It can't "understand" your code. So you can't ever depend on it to validate your code.

Thanks all. Will build and test in next couple of days

Thanks all. Will build and test in next couple of days

Scope it out.

Project built today on the bread board and working well with a pro mini. I had the interval calculation incorrect to obtain the requisite 60 to 120 flashes per minute.
Next will be to try with a smaller board, I’m thinking of a Digispark so that with a Songle relay the whole thing can be packaged up quite small.

The Digispark has an onboard 500mA 5v regulator. The relay coil draws about 80mA, the sensor only about 4mA, so should be able to cope.
Also I think I can feed it directly with 12v and do away with the external 5V regulator.
They say
Power via USB or External Source - 5v or 7-35v (12v or less recommended, automatic selection)
On-board 500ma 5V Regulator
Any problems with that?