Problem getting input sent to JMRI

I am having some trouble using the ArduinoCMRI library and getting input sent to JMRI. I believe I have the sketch mostly working but I can not get it to activate a sensor on the JMRI side. I have verified that I can do that with the input and output example sketch so I know that JMRI is setup correctly. I am guessing it is something simple on my sketch that is not communicating correctly. The sketch is supposed to read the value of an LED and when it goes below a certain value it triggers the other LED to come on. I ultimately want the arduino to trigger a JMRI sensor to be active instead of the LED coming on. Here is the sketch:

#include <CMRI.h>

#define READ A0    // add other analog ports as needed
//#define READ A1
#define LED 12
#define IN 11
int basis = 0;
int val = 0;

CMRI cmri;

void setup() {
  Serial.begin(9600, SERIAL_8N2);
  { pinMode(LED, OUTPUT); }
  //Serial.begin(57600);
}

void loop() {
  cmri.process();
  int sens = readLED(50);
  basis = sens -50;                 // adjust this value for room light setting sensitivity - now it will react if the LED is 50 lower than the setting above
  for(int y = 0; y < 1000; y++) {    // after every 1000 tests the program will reset the led to cope with changing light
    sens = readLED(50);
    Serial.println(sens);
    if (sens < basis) {               // testing is the led was in the dark
       digitalWrite(LED, HIGH);
       //Serial.println(bitRead(PORTB, 4));
       val = bitRead(PORTB,4);
       Serial.println(val);
       cmri.set_bit(0, val);
    }
    else { 
       digitalWrite(LED, LOW);
       //Serial.println(bitRead(PORTB, 4));
       val = bitRead(PORTB,4);
       Serial.println(val);
       cmri.set_bit(0, val);
    }
  }
}

int readLED(int number) {            // Read analog value n times and avarage over those n times
  int totaal = 0;
  for(int x = 0; x < number; x++) {
    totaal += analogRead(READ);
    delay(1);      // delay sets output delay
  }
  return totaal/number;
}

Thanks in advance for any help!

Chris

Can you describe how that CMRI library is intended to work ?
Do you have a link to its documentation ?

...R

Here is the link to the developer's site for the library.

Hi digiext

The sketch is supposed to read the value of an LED and when it goes below a certain value it triggers the other LED to come on.

What is the program doing at present?

Are you getting sensible value for sens and basis? Is the right branch of the if statement being executed?

One potential problem ...

int totaal = 0;
...
totaal += analogRead(READ);

Averaging 50 readings could lead to totaal overflowing.

Regards

Ray

Ray,
I am getting correct readings and I can watch the value of val change from 0 to 1. It seems that the CMRI part is not sending that value correctly. I think the logic before that is working correctly.

Chris

Thanks, Chris.

Since you have this for loop within loop() ...

for(int y = 0; y < 1000; y++) {

... maybe you need to move cmri.process(); inside the for loop as well. Otherwise, it could be that your CMRI commands are not actually being actioned.

Alternatively, replace the for loop with code that increments a counter each time round loop() and resets the reading (and the counter) when it gets to 1000.

ADDED The library web page has this ...

The main loop is where the magic happens. cmri.process() receives data from the PC and processes it automatically, responding to the PC if required, and updating it's internal state ready for us to read the data back out.

Ray,
I want to say thank you very much because by moving the cmri.process into the For loop the problem has been solved!

Chris