Arduino Library Confusion

I'm trying to create a library for a group of Arduino's set up in parallel controlled thru a GUI via the serial port. In order to exit from the current Arduino you press an exit button on the GUI. This sends out a letter "z" to the Arduino. When the Arduino receives this letter it puts a 500 millisecond pulse on pin 5. The Control Arduino senses this pulse and disconnects the current Arduino letting the user choose another Arduino. The code works fine but I'd like to put it in a Library rather then typing it out for every Arduino. I've looked up how to create a library but am totally confused.
The .cpp file is for the code? What do I put in the .h file? Here is the code Id like to put in the library.

String data;
void setup() {
  Serial.begin(9600);
  pinMode(5, OUTPUT);
  digitalWrite(5, LOW);
}
void loop()
{
  digitalWrite(5, LOW);
  if (Serial.available() )
  {
    String txt = Serial.readString();
    {
      else if (txt == "z")
      {
        digitalWrite(5, HIGH);
        delay(500);
        digitalWrite(5, LOW);
      }
    }
  }
}

See my Post #5 in this Thread for a basic guide to multi-file projects.

the library would not have a setup() and loop(). this is a sketch.

how would you see that sketch written assuming you had a library?

Ralph Bacon created a couple easy-to-follow videos on creating libraries:

1 Like

@igor61

I suggest you learn following steps:

a) follow the Arduino Example for a first library:

b) the Library specification is a good source to learn about the internals:

c) read and apply the Arduino Style Guide for Creating Libraries

d) and finally the only externals source I would recommend as a starter is Sparkfuns Hot to write a great library

coming back to your example sketch:

First you have to decide "WHAT" you want to put in the library. Start with a clear documentation what functionality you want to have separated in a reusable library.

Is it the serial read?
Is it the pulsing of the pin*)?
Both together? Then you should start and put it in a function first and just call that function.

if you tell more about what you want to achieve, we can give better hints.

p.s.: *) why do you block your code with a delay? why not a non-blocking variant with millis?

Yes, I'd just like to know more about these Arduinos and selecting amongst them and so forth.

What are you up to exactly?

a7

@igor61 has not come back... I guess we don't need to add more questions :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.