Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitor 6 analog input pins on Arduino

Tags:

arduino

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?

like image 563
user1165574 Avatar asked Sep 19 '25 09:09

user1165574


1 Answers

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.

like image 176
MrD Avatar answered Sep 23 '25 08:09

MrD