4

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?

3
  • Also, the code should be capable of listening to serial port for "messages" that tell what pin should be HIGH or LOW. In other words, listen to serial port and write to serial port. How can the two tasks work simultaneously? Commented Jan 24, 2012 at 23:07
  • I think you've already covered that in this other question: stackoverflow.com/questions/8996675/… haven't you? Commented Jan 25, 2012 at 12:45
  • yes, and have not got any reply yet Commented Jan 25, 2012 at 14:48

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

Communication between Arduino and another device (I'm assuming a PC here) is simplest (and easiest to debug) if you use a text-based interface. For example:

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

void loop() {
    for (int i = 0; i < 6; i++) {
        Serial.print(i);
        Serial.print(":");
        Serial.println(analogRead(i));
    }
}

This will continuously read values from the analog ports and send them (as text) like this:

0:456
1:26
2:0
3:1023
4:321
5:1010

Ports are numbered from 0 to 5, then a colon (:) character is sent as a delimiter, then the analog value (from 0 to 1023).

This will repeat indefinitely, but you mention only sending data if there is activity. If you need something more sophisticated please expand on what you consider 'activity' (for example maybe you only want to send data if the analog value has changed since it was last read?).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.