Sampling and sorting analog signal c(non invasive current sensor)[SOLVED]

hi
i have grovepi+ , rpi2 and a non invasive current sensor.

i have use the exampel code in /GrovePi/Software/C/grovepi_analog_read.c
it works good but i measure a alternating current so i get a lot of zeros.
what i need help with is to get like highest value for each second.
my c programming skills ist the best but its a lot better reading code.

btw dose anyone know how i do if i want it to read from the a1 port?

Know not much with C but here’s what it would look like in sudo code:
for I in range 0 through 10 { Append sensor reading to list x sleep tenth a second

sort list x highest_value = item one of list x

For info on sortin a list check out this stack overflow topic:

Hi @kristoffer.ljungberg,

To have your code read the highest value every second, you can also try this pseudo code

while(1){

max_value=0;
adata=analogRead(0);
if adata>max
   max=adata
for every 1 sec
{   print(max_value);
     max_value=0;
}
}

To calculate time you can follow some samples given here.

Also to use analog port A1, you will have to change this line to
adata=analogRead(1);

-Shoban

1 Like

ok well i still got a problem. if i write a program i cant make it print the analog input. but if i use the exampel code it dose.
that leavs some questions.
why cant the exampel code be compliled?
why cant i just write #include “GrovePi.h” if i write my program in that folder.
what dose " if (init()==-1)
exit(1); "
do in the exampel code of grovpi_analog_read.c
and what dose ./a out do?

edit:
did not understand the readme file. now i do so this post is unnecessary

Hi Kristoffer,

I’m glad you understood the readme, but in order to help others who might come here, I’ll give a brief explanation of how things are.


Here’s my list of answers:

  1. Every function you call in your program, let’s say analogRead, print and so on, all have to be defined somewhere.
    And in our case, these functions can be imported by using the #include "grovepi.h" directive, that causes the contents of a grovepi.h file to be inserted into the your program.

  2. The code @Shoban posted earlier, is not actual code, but it’s a pseudo code. A pseudo code cannot be compiled, it’s written in natural language and it’s used to describe the functionality of a program. The pseudo code doesn’t deal with the intricacies of a programming language. Think of it as a spoken language.

  3. The instruction if(init() == -1) exit(1); tries to initialize communication between the Raspberry and the GrovePi and if it fails, the function init() returns a -1 code. If -1 code is encountered in the if statement, then it exits the program. Here’s a link to init() function.

  4. When you want to compile your program, you’ll have to cd in your directory, and use the gcc compiler to compile your program. You’ll also have to include the other .c files you’re using (like the grovepi.c) in the program. ./a.out is what your compiler outputs after it finished compiling, and this is your program you want to run. In order to run your compiled program, you have to type in you’re cd'ed terminal the ./a.out command. Also, ./a.out is the default name of a program gcc gives when there’s no name provided.


And a question:

  1. What do you mean by saying you’re not getting the output? Can you provide us with your code? It would be nice if you could paste here the output you’re getting.

Here’s a link to the readme in order to understand what you have to do with gcc compiler.


Thank you

This topic was automatically closed after 24 hours. New replies are no longer allowed.