Creating Libraries

Ok so I have some code that I want to put into a library so that it will make using my code alot simpler and quicker. I read the tourtorial on how to create a library but I got confused and just need a little more detail so I can fully understand it. Any help will be much appreciated!

Pheeney09:
I read the tourtorial on how to create a library but I got confused and just need a little more detail so I can fully understand it. Any help will be much appreciated!

I assume you read: http://www.arduino.cc/en/Hacking/LibraryTutorial

Now, I guess to get more help you really need to be specific what you don’t understand, using that tutorial as baseline!

ok so my first question is what does the header file do or what is it there for? but the first confusing part what is in red. Is that just assigning the variable (Morse) to the input pin?

class Morse { public: Morse(int pin); void dot(); void dash(); private: int _pin; };

and my second question is what kind of functions or variables go into the public and private sections? and then how many classes can you have in one header file?

In short:

The .h file describes the interface of the class The .cpp file describes the implementation

Morse (int pin);

is the constructor of the Morse class. A constructor makes an new instance of a Morse object. To do this it needs to know to which pin (for a digitalRead/Write) it shoudl connect, therefor this pinnumber is given as a parameter to the constructer. IN the implementation of the constructor the code will make a local copy of this pinnumber to be used if that instance of a Morse object is asked to "beep"

ok I understand a little bit better. but im still not sure on somethings, ill post my code so if someone can help me get started im sure itll click then. All i really want the library to do is control my 7segment display driver to display numbers only…which my code does flawlessly. Im hoping this will shorten my sketch coding and i’ll have it to use on other sketches which will save me time. if anyone can help this would be greatly appreciated!

const int numReadings = 40;

int data = 10; //data input on MC14489
int enable = 12; //enalbe latch on MC14489, active low
int clock = 11; //clock pulse
int blank = 0x01; //hex code to set display to bright
int byte1 = 0; //Frist of 3 bytes to send
int byte2 = 0;
int byte3 = 0;
int nibdp = B1010 ; //decimal point control
int nib1 = 0; // digit 5 - not used
int nib2 = 0; // digit 4 - not used for gas, oil, and temp
int nib3 = 0; // digit 3 1000’s
int nib4 = 0; // digit 2 100’s
int nib5 = 0; // digit 1 1’s
int reading = 0;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputpin = A0; // fuel sender input
int value = 0; // variable to store reading
int bl = 9; // Blue LED PWM Always on unless reading drops below 20
int rl = 6; // Red LED PWM on when reading drops below 20
int brightness = 0; // controls the PWM number for LEDS

void setup() { //set digital pins to outputs

pinMode(data, OUTPUT); //data output
pinMode(enable, OUTPUT); // enable output
pinMode(clock, OUTPUT); // clock pulse output

Serial.begin(9600);

for (int thisReading = 0; thisReading < numReadings; thisReading++) // used for the smoothing algorithm
readings[thisReading] = 0;

}

void loop() {

nib1 = 0; // had to declare all digits zero or else the display chip would give odd outputs
nib2 = 0;
nib3 = 0;
nib4 = 0;
nib5 = 0;

total = total - readings[index];

// read from the sensor:

readings[index] = analogRead(inputpin);

// add the reading to the total:

total= total + readings[index];

// advance to the next position in the array:

index = index + 1;

// if we’re at the end of the array…

if (index >= numReadings)

// …wrap around to the beginning:

index = 0;

// calculate the average:

average = total / numReadings;

if(average <= 879){
delay(100);
}
else if(average >= 880){
delay(2000);

}

reading = map(average, 880, 1016, 0, 10); //remap reading so that it goes from 0 to 100

reading = constrain(reading, 0, 10); //constrain reading so it will never go above 100 or below 0

Serial.println(reading, DEC);

brightness = map(reading, 2, 10, 0, 255); //

if(reading >= 2 ){
analogWrite(bl, brightness);
analogWrite(rl, 0);
}

if(reading <= 2){
brightness = map(reading, 2, 0, 5, 255);
analogWrite(rl, brightness);
analogWrite(bl, 0 );
}

if(reading >= 1000){ //the next 4 if statements take the reading and break it up to there indvidual
value = reading / 1000; // Base ten places I.E…thousands, hundreds, tens, ones.
reading = reading - (value * 1000);
nib2 = value;
}

if(reading >= 100){
value = reading / 100;
reading = reading - (value * 100);
nib3 = value;
}

if(reading >= 10){
value = reading / 10;
reading = reading - (value * 10);
nib4 = value;
}

if(reading >= 1){
value = reading;
reading = reading - value;
nib5 = value;
}

byte3 = nib4 << 4 | nib5; //this asigns bytes 1 to 3 to the respectable order to give the right display output
byte2 = nib2 << 4 | nib3;
byte1 = nibdp << 4 | nib1;

digitalWrite(enable, LOW); //send low to enable to allow data to shiftout
shiftOut(data, clock, MSBFIRST, blank); //set display to bright
digitalWrite(enable, HIGH); //end connection

delay(100);

digitalWrite(enable, LOW);

shiftOut(data, clock, MSBFIRST, byte1); // sends out the value read from the fuel sender one byte at a time
shiftOut(data, clock, MSBFIRST, byte2);
shiftOut(data, clock, MSBFIRST, byte3);

digitalWrite(enable, HIGH);
}

PLease use [ code] tags to post code (you probably used the IDE option which contains a bug)

Use CTRL-T too before posting, and dont use so many empty lines, makes code less readable in fact.

OK, enough of that

The way you calculate averages is wrong for the first 39 elements as you consequently divide by 40 instead of by the nr of read samples.

A rewrite of loop()

int divider = 0;  // to get correct average
void loop()
{
  // all digits zero to prevent odd outputs
  nib1 = 0;               
  nib2 = 0;
  nib3 = 0;
  nib4 = 0;
  nib5 = 0;
  
  // read sample into array and update its total
  total = total - readings[index];
  readings[index] = analogRead(inputpin);
  total = total + readings[index]; 

  // update index and divider and calculate average
  index++;
  divider = max(divider, index);  
  average = total / divider;
  if (index >= numReadings) index = 0;             

  Serial.print("AVG: ");
  Serial.println(average)
  
  // wait depending on average
  if (average <= 879) delay(100);
  else delay(2000);
}

ok sorry bout the posting code thing, I didnt know, but is that why if I put anything bigger than 40 as my sample number I get weird numbers? and I'll try this new code and see what it does.