How would I tell the main code to run a function from a .cpp file

So I'm working on a project and I'm new to the whole "multiple tabs" thing in the IDE and I when I tried to run a digitalWrite from a .cpp file, it wouldn't let me upload my code to the board. I want to know how I could possibly communicate between the two "tabs" to have an even occur when something happens in the .cpp file. if you know how this is done, I would really appreciate it if you would let me know. thanks!
image

To use digitalWrite and the likes, you need to include Arduino.h. Did you do that?

For your function in your cpp file to be known in other files (e.g. your main ino file), you will need to provide the function prototype in that other file, either using a separate h file that youu include or by just putting the prototype directly in the other file.

Welcome to the forum

Normally there is a .h file of the same name as the .cpp file but you have given little or no detail of exactly want you want to do. More details and code (in code tags) please

1 Like

Look up how to make a library in arduino and you should get everything you need.

1 Like

There isn’t really any actual code that I can show, I just have an if statement in my cpp file and I want that if statement to be able to run a digitalWrite in the main code. Could you explain how having a .h of the same name would help me do this? I’m very new to this kind of code. Thanks!

Is it inside a function? If not, it will not work; you can not dump random snippets in a cpp file.

Why use a .cpp file unless you are going to write a library when you could just use a second .ino file in the same folder

See My Post #5 In this Thread for a guide to breaking a project up into multiple .cpp / .h files.

So this is an example of a simple .cpp file

functions.cpp

/*
  test a pin and set pin13 high if the pin is low
  In:
    number of pin to test
 */
void testPin(int pin)
{
  if(digitalRead(pin) == LOW)
  {
    digitalWrite(LED_BUILTIN, HIGH);
  }
}

If you compile it, the compiler will complain about digitalRead and digitalWrite() as it does not know about them; you can solve this by including Arduino.h.

functions.cpp (fixed)

#include <Arduino.h>

/*
  test a pin and set pin13 high if the pin is low
  In:
    number of pin to test
 */
void testPin(int pin)
{
  if(digitalRead(pin) == LOW)
  {
    digitalWrite(LED_BUILTIN, HIGH);
  }
}

The compiler now knows what the reuqired arguments for digitalRead() and digitalWrite() are as that is defined in Arduino.h.

Below is the main sketch that wants to use the testPin() function.
ino file

const uint8_t somePin = 2;

void setup()
{
  pinMode(somePin, INPUT_PULLUP);

}

void loop()
{

  testPin(somePin);

}

Again, the compiler will complain that it does not know about a function (testpin() in this case). You can solve this by adding the function prototype to your code.
ino file (fixed)

// function prototype
void testPin(int pin);

const uint8_t somePin = 2;

void setup()
{
  pinMode(somePin, INPUT_PULLUP);

}

void loop()
{

  testPin(somePin);

}

The more common approach is to have a .h file that accompanies the .cpp file. In that .h file, you can place your prototype and you include the .h file when needed.
functions.h

#ifndef _FUNCTIONS_H_
#define _FUNCTIONS_H_

void testPin(int pin);

#endif

and the ino file

#include "functions.h"

const uint8_t somePin = 2;

void setup()
{
  pinMode(somePin, INPUT_PULLUP);

}

void loop()
{

  testPin(somePin);

}

Regarding the functions.h file

#ifndef _FUNCTIONS_H_
#define _FUNCTIONS_H_

...
...

#endif

The construction is a so-called include guard that prevents errors due to double declarations if you include the file in different files.

Alright this is really helpful. thanks. So all I would have to do to make digitalRead/Write work within a cpp file is to include arduino.h?

I wouldn't know. I basically know nothing about this stuff. what I have currently is what I got from an example project the I'm modifying. if there's a better way to do any of this, let me know.

The beauty of modern computers (that includes Arduino), you can quickly test it.

Are you intending to #include a file in your sketch or put the function prototype in your main .ino file ?

That depends on your definition of "better"

Try this
In the main tab of the sketch

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

void loop()
{
  changeLed();
  delay(1000);
}

In a second .ino file in the same folder, ie a second tab

void changeLed()
{
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}

yes. I understand this but what my real issue is, is that I have a web function that can only be run in a specific cpp script(at least I believe it can only be run in the cpp) and I need to run a function when a certain value is greater than another value. I've probably been explaining this bad the whole time, but what I'm trying to do is run a function from an if statement in a cpp script. I tried the #include "Arduino.h" but it still didn't work and I'm not sure why. I'm able to print to the serial moniter but for some reason I still cant run digitalWrites.

I looked back at it and #include "Arduino.h" was already at the top of the .cpp from the example and it still wasn't working.

That sounds unlikely, but if you insist

The .ino file

void triggered();  //function prototype as we don't have a .h file

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  static byte count = 0;
  delay(10);
  count++;
  if (count == 0)
  {
    triggered();
  }
}

The .cpp file

#include "Arduino.h"  //needed to access the Arduino functions

void triggered()
{
  Serial.println("triggered");
}

this is kind of the opposite of what I want to do. I want to run the function that's in the main ino from the cpp. I modified the code but I'm getting an error with the function in the .cpp file. what am I doing wrong? is there something I need to have before I call the function?

This is the main .ino

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


void triggered()
{
  while (true) {
      digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
      delay(500);
  }
}

and this is the .cpp

#include "Arduino.h"  //needed to access the Arduino functions
void triggered();  //function prototype as we don't have a .h file


triggered();

That call to triggered() is not within a function.
Your .ino file has no loop() function.

Xy problem