I am writing a code for Arduino. What it should do is:
It has to Monitor 6 analog inputs and if there is any activity on any of them, send (number of active pin + value on its pin) via serial connection.
On the other side of the serial connection, Other program will make decision on this given information.
How best to do it?
One solution might be like this:
int analogPin1 = 1;
int analogPin2 = 2;
int analogPin3 = 3;
int analogPin4 = 4;
int analogPin5 = 5;
int analogPin6 = 6;
int val = 0;
byte sendAnalog=0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
val=analogRead(analogPin1);
if(val>0){
Serial.print("1");
sendAnalog=val*0.24926697;
Serial.print(sendAnalog,BYTE);
}
val=analogRead(analogPin2);
if(val>0){
Serial.print("2");
sendAnalog=val*0.24926697;
Serial.print(sendAnalog,BYTE);
}
val=analogRead(analogPin3);
if(val>0){
Serial.print("3");
sendAnalog=val*0.24926697;
Serial.print(sendAnalog,BYTE);
}
val=analogRead(analogPin4);
if(val>0){
Serial.print("4");
sendAnalog=val*0.24926697;
Serial.print(sendAnalog,BYTE);
}
}
AD converter is storing 10 bits values. Maximum value by AD converter is 1023 ([2^10-1]). Module for serial communication is sending bytes of data, so you need scale 1023 to 255. Equation is (255/1023)*currentAnalogValue (so it is 0.249266*currentAnalogValue). In your computer application, you need inverse equation 1023/255*receivedByte to receive original value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With