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