Hi all!
I'm newbie. Sorry for the inconvenience!
My project:
- Hardware diagram:
- Software:
PC: I want to write a program by Visual C++ 2008. I use SerialPort class in MSDN. "Send request to arduino and read sensor value from arduino"
....................
void readAnalog()
{
//request: send "1" to arduino
........... (help me)
if(serial available)
{
//read analog value from arduino
............ (help me)
}
}
void readFrequency()
{
//request: send "2" to arduino
........... (help me)
if(serial available)
{
//read frequency value from arduino
............ (help me)
}
}
.........................
Arduino Nano: read request from PC and send sensor value to PC
#include <FreqMeasure.h>
void setup() {
Serial.begin(57600);
FreqMeasure.begin();
}
double sum=0;
int count=0;
int temp=0;
int request=0;
double frequency=0;
int voltage=0;
void loop() {
if(Serial.available()>0)
{
request = Serial.read();
if(request == 2)
{
//read frequency from sensor
readFreq();
//send "frequency" value to PC
.............(help me)
}
else
{
if(request == 1)
{
voltage = analogRead(A0);
//send "anaglog" value to PC
............. (help me)
}
}
}
}
double readFreq()
{
if (FreqMeasure.available())
{
// average several reading together
sum = sum + FreqMeasure.read();
count = count + 1;
if (count > 30) {
frequency = F_CPU / (sum / count);
sum = 0;
count = 0;
}
}
else
{
temp = temp + 1;
if(temp >3)
{
frequency = 0;
temp = 0;
}
}
return frequency;
}
- If my doing wrong, people fix to help me.
- And if my doing right, please help me complete code in "..........(hepme)"
Thank in advance!