Hello Everyone
I have this sketch from DIY and Digital Railroad to use DIY CT sensors to detect block occupancy on a model railroad. I have built his circuits and they function correctly but I cannot get multiple ones working at the same time on a Nano. Here is the code.
long result; //longer variable storage
int numSamples = 10;//number of readings
int clearcount;//clear test variable
int clearsample = 100;// number of clear samples needed from sensor
int threshold = 550;//ADJUST THIS FOR SENSITIVITY minimum thresold for tripping the sensor
int value;// final read result
void setup(){
Serial.begin(9600);//starts serial monitor
pinMode(7,OUTPUT);//indicator LED for occupancy
}
enum OCCUPANCY
{
ST_OCCUPIED,
ST_CLEAR,
};//Our occupancy states
OCCUPANCY Occupancy = ST_CLEAR;//default state
void loop(){
int valA1;
int result = 0;
for (int i=1; i < numSamples; ++i){
valA1 = analogRead(A0);//reads analog pin
result +=valA1;//adds new reading to variable 'result'
delay(10);//reading every .01 seconds
}
value=result/numSamples;//averages readings
Serial.println(value);//displays average reading
switch (Occupancy)
{
case ST_OCCUPIED:
occupied();
break;//sets up occupied state
case ST_CLEAR:
clear1();
break;//sets up clear state
}
}
void clear1(){
clearcount=0;//resets the clearcount
digitalWrite(7,LOW);//keeps LED off for indicator
if (value < threshold){
Serial.println("Block is occupied");//displays text
Occupancy = ST_OCCUPIED;//switches to occupied
}
}
void occupied(){
digitalWrite(7,HIGH);//turns on LED indicator
if ((value >threshold) && (clearcount<clearsample)){
clearcount++;//adds to clear count
}
if ((value < threshold) && (clearcount<clearsample)){
clearcount=0;//resets the clearcount if a false reading was taken
}
if ((value>threshold) && (clearcount >(clearsample-1))){
Serial.println("Block is Clear");//displays text
Occupancy = ST_CLEAR;//changes to clear
}
}
here is what I have thought would work but obviously I have missed something as it does nothing but signal the number 1 sensor
long result; //longer variable storage
int numSamples = 10;//number of readings
int clearcount;//clear test variable
int clearsample = 100;// number of clear samples needed from sensor
int threshold = 550;//ADJUST THIS FOR SENSITIVITY minimum thresold for tripping the sensor
int value;// final read result
void setup() {
Serial.begin(9600);//starts serial monitor
pinMode(2, OUTPUT); //indicator LED for occupancy
pinMode(3, OUTPUT); //indicator LED for occupancy
pinMode(4, OUTPUT); //indicator LED for occupancy
pinMode(5, OUTPUT); //indicator LED for occupancy
pinMode(6, OUTPUT); //indicator LED for occupancy
}
enum OCCUPANCY
{
ST_OCCUPIED,
ST_CLEAR,
};//Our occupancy states
OCCUPANCY Occupancy = ST_CLEAR;//default state
void loop() {
int valA1;
int valA2;
int valA3;
int valA4;
int valA5;
int result = 0;
for (int i = 1; i < numSamples; ++i) {
valA1 = analogRead(A0);//reads analog pin
valA2 = analogRead(A1);//reads analog pin
valA3 = analogRead(A2);//reads analog pin
valA4 = analogRead(A3);//reads analog pin
valA5 = analogRead(A4);//reads analog pin
result += valA1; //adds new reading to variable 'result'
result += valA2; //adds new reading to variable 'result'
result += valA3; //adds new reading to variable 'result'
result += valA4; //adds new reading to variable 'result'
result += valA5; //adds new reading to variable 'result'
delay(10);//reading every .01 seconds
}
value = result / numSamples; //averages readings
Serial.println(value);//displays average reading
switch (Occupancy)
{
case ST_OCCUPIED:
occupied();
break;//sets up occupied state
case ST_CLEAR:
clear1();
break;//sets up clear state
}
}
void clear1() {
clearcount = 0; //resets the clearcount
digitalWrite(2, LOW); //keeps LED off for indicator
digitalWrite(3, LOW); //keeps LED off for indicator
digitalWrite(4, LOW); //keeps LED off for indicator
digitalWrite(5, LOW); //keeps LED off for indicator
digitalWrite(6, LOW); //keeps LED off for indicator
if (value < threshold) {
Serial.println("Block is occupied");//displays text
Occupancy = ST_OCCUPIED;//switches to occupied
}
}
void occupied() {
digitalWrite(2, HIGH); //turns on LED indicator
digitalWrite(3, HIGH); //turns on LED indicator
digitalWrite(4, HIGH); //turns on LED indicator
digitalWrite(5, HIGH); //turns on LED indicator
digitalWrite(6, HIGH); //turns on LED indicator
if ((value > threshold) && (clearcount < clearsample)) {
clearcount++;//adds to clear count
}
if ((value < threshold) && (clearcount < clearsample)) {
clearcount = 0; //resets the clearcount if a false reading was taken
}
if ((value > threshold) && (clearcount > (clearsample - 1))) {
Serial.println("Block is Clear");//displays text
Occupancy = ST_CLEAR;//changes to clear
}
}
can someone please clue me into what else needs to be modified to get it working
Thanks James