Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Make my own library in arduino IDE

I want to make my code more elegant to get ekstra points from my teacher. So my plan is to use a seperet file for all my functions that I will need to use throughout the program. We are making an arduino which can calculate your BPM of your heart. So I have so far made one function and it's job is to get average of a list. here is my code!:
Code
#include "Helper.cs"

int measurment[10];
int measur = 0;
int reading = 0;
int counter = 0;


void setup() {
  Serial.begin(9600);

}

void loop() {
  while(counter < 10) { // we will get readings from the analogreader and we want to do that 10 times and then start calulating.
      reading = analogRead(0);
      measurment[counter] = reading;
      counter++;

  }
getAverage(measurment[10]); // using average to get to know if this will work or not!
  

}

Now that is so far my main code. This function is made in notepad++ and I want to use it as a library for my code:
Code
#ifndef Helper.cs
#define Helper.cs

#include "Arduino.h"

public int getAverage(int list) {
    int counter = 0;     
    int sum = 0;
    for(int counter;counter < 10;counter++){
        sum += list[counter];
    }
    return sum;
}


#endif

Now will this work or do I need to make the library inside the arduino IDE?
Do I need to make classes for a library to work or is this fine?

Note: I'm just a beginner at coding and please tell me if there is anything wrong with the code.
Where do I begin.

PHP Code

<?php 
public int getAverage(int list) {
    
int counter = 0;     
    
int sum = 0;
    for(
int counter;counter < 10;counter++){
        
sum += list[counter];
    }
    return 
sum;
} 

This won't work. list is an integer, not an array.
counter in the for loop is re-declared but unintitialized. It should be initialized to 0, and really, it should be declared outside the loop depending on the language.

PHP Code

<?php 
int counter
;
for(
counter = 0; counter < 10; counter++){
    
// code here
} 

If you are passing an array, you want to pass the size as well. Also, because the name of the funciton is 'getAverage', I assume you want the average.

PHP Code

<?php 
public int getAverage(int *list, int size) {
    
int counter;     
    
int sum = 0;
    for(
counter = 0;counter < size;counter++){
        
sum += list[counter];
    }
    return 
sum/size;
} 

Call it with:
PHP Code

<?php 
void loop
() {
  
measurement[10];
  
int counter;
  for(
counter = 0; counter < 10; ++counter){
      
reading = analogRead(0);
      
measurment[counter] = reading;
  }
  
int avg = getAverage(measurment, 10);
} 
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
Just a thought as well, rename your files to the appropriate file extensions. Arduinos do not support C#. While technically it doesn't matter since it is just text files you are working with, I'd recommend making them header files or C source files.
So Goodie,
if I then make this a PHP file then call #include"library_getAverage"(<- jusr random name), I will be able to use it in my program?
Then after that I can just keep on adding functions as I go on coding for the arduino project?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[The Brotherhood] by itcorpJul 30, 2026 01:52 AM 4 181 Jul 30, 2026 01:52 AM
Last Post: HerrWinston
[C] by phisnNov 11, 2020 08:44 AM 1 166 Nov 11, 2020 08:44 AM
Last Post: HFDone
[Java] by Nemo2478Oct 28, 2019 01:42 AM 0 176 Oct 28, 2019 01:42 AM
Last Post: Nemo2478