Code works in Arduino IDE but not PlatformIO

I’m trying to go through a few books to learn Arduino programming and really like PlatformIO IDE but I keep running into issues with code that won’t work in PlatformIO but will work in the regular Arduino IDE. It’s a simple program that flashes the built in LED, when I build it i get an “error: ‘flash’ was not declared in this scope”.

Here’s the code:
// sketch 4-02
#include "Arduino.h"
int ledPin = 13;
int delayPeriod = 250;

void setup()
{
pinMode(ledPin, OUTPUT);
}

void loop()
{
flash(20, delayPeriod);
delay(3000);
}

void flash(int numFlashes, int d)
{
for (int i = 0; i < numFlashes; i ++)
{
digitalWrite(ledPin, HIGH);
delay(d);
digitalWrite(ledPin, LOW);
delay(d);
}
}

Am I missing something super obvious? Thanks in advance for any help!

You need to declare flash() before you can use it, this is called “forward declaration”. Just add a line void flash(int numFlashes, int d); before loop() and then you can put the complete function anywhere in the code. Adruino IDE does some magic when converting ino to C++ but with PIO you have to write normal C++.

1 Like

did you name the sketch .ino or .cpp ?

That worked! Thank you so much!