# Coding Question

Hello,

I am writing a program for two HO scale locomotives that follow eachother on a single loop of track. I’ve placed photo cells on the track to keep the engines apart. If one engine gets too close to the other engine, I plan on stopping it at a photo cell. Each locomotive has a serial motor driver that communicates with an Ardunio wirlessly using Xbees. I am using two separate Arduinos so I can maintain independent control over each locomotive.

Explanation of Coding:

The idea of the coding is to read a value from a photo cell using the readmux(command). Because I have a total of 8 photo cells, I decided to use a multiplexer chip to reduce 8 analog digital pins into one. I have four digital output pins that control which channel to select and rad. I am using an array to call upon the separate channels of the mux.

If the value of say photo cell A0 is greater than A0 min (train present), than I go through a while loop and jump down to compare(). I tell the program to take a reading from the next photo cell up the line(track). In this example, it would be A1. If A1 (indicates there is a different train there) and A0 are high, the program writes a serial command to stop the first train. Other wise, it tells the train to keep on trucking.

After it is finished going through the while loop, it goes back to the top and waits until another sensor is high.

I have not yet measured the min and max values for each photo cell. The min is defined as when a train is not over the sensor. Max is when a train is over the sensor.

I’m not sure if want I’m wanting to do is feasible. If you have any suggestions, please let me know.

Coding:

``````int s0=3;
int s1=4;
int s2=5;
int s3=6;
int analogA0=0
int A0min=//min bit value of photocell
int A0max=//max bit value of photocell
int A1min=
int A2max=
int A2min=
int A3min=
int A3max=
int A4min=
int A4max=
int A5min=
int A5max=
int A6min=
int A6max=
int A7min=
int A7max=
int A8min=
int A8max=

void setup() {
Serial.begin(115200);//baud rate required by serial motor driver
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);

pinMode(s2, OUTPUT);

pinMode(s4, OUTPUT);

pinMode(analogA0=0, INPUT);

digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);

Serial.write('1');
Serial.write('f');
Serial.write('8');
Serial.write('\r');

}

void loop(){

while (val0 >= A0min){ //when train is passing over sensor A0, branch to compare to next sensor
compare();
}

while (val1 >= A1min){ //when train is passing over sensor A1, branch to compare to next sensor
compare1();
}

while (val2 >= A2min){ //when train is passing over sensor A2, branch to compare to next sensor
compare2();
}

while (val3 >= A3min){ //when train is passing over sensor A3, branch to compare to next sensor
compare3();
}

while (val4 >= A4min){ //when train is passing over sensor A4, branch to compare to next sensor
compare4();
}

while (val5 >= A5min){ //when train is passing over sensor A5, branch to compare to next sensor
compare5();
}

while (val6 >= A6min){ //when train is passing over sensor A6, branch to compare to next sensor
compare6();
}

while (val7 >= A7min){ //when train is passing over sensor A7, branch to compare to next sensor
compare7();
}

while (val8 >= A8min){ //when train is passing over sensor A8, branch to compare to next sensor
compare8();
}

int controlpin []={s0,s1,s2,s3}

int muxChannel[16][controlpin]={//mux breakout board arrar
{0,0,0,0}, //sensor 0
{1,0,0,0}, //sensor 1
{0,1,0,0}, //sensor 2
{1,1,0,0}, //sensor 3
{0,0,1,0}, //sensor 4
{1,0,1,0}, //sensor 5
{0,1,1,0}, //sensor 6
{1,1,1,0}, //sensor 7
{0,0,0,1}, //sensor 8
{1,0,0,1}, //sensor 9
{0,1,0,1}, //sensor 10
{1,1,0,1}, //sensor 11
{0,0,1,1}, //sensor 12
{1,0,1,1}, //sensor 13
{0,1,1,1}, //sensor 14
{1,1,1,1}  //sensof 15
};
for(int i = 0; i  < 4; i ++){//cycle through each of the four pins a0,a1,a2,a3
digitalWrite(controlPin[i], muxChannel[channel][i]);
}

void compare (){
if (val1 > A1min && val0 > A0min){//if sensor 1 and 0 are high, stop train
Serial.write('0');
Serial.write('\r');
delay(3000);
}
if else(val1<A1min && val0 >A0min){//if sensor 1 is low and sensor 0 is high, run train
Serial.write('8');
Serial.write('\r');
delay(3000);
}
}

void compare1 (){
if (val2 > A2min && val1 > A1min){//if sensor 2 and 1 are high, stop train
Serial.write('0');
Serial.write('\r');

}
if else(val2<A2min && val1 >A1min){//if sensor 2 is low and sensor 1 is high, run train
Serial.write('8');
Serial.write('\r');

}
}

void compare2 (){
if (val3 > A3min && val2 > A2min){//if sensor 3 and 2 are high, stop train
Serial.write('0');
Serial.write('\r');
}
if else(val3<A3min && val2 >A2min){//if sensor 3 is low and sensor 2 is high, run train
Serial.write('8');
Serial.write('\r');
}
}

void compare3 (){
if (val4 > A4min && val3 > A3min){//if sensor 4 and 3 are high, stop train
Serial.write('0');
Serial.write('\r');
}
if else(val4<A4min && val3 >A3min){//if sensor 4 is low and sensor 3 is high, run train
Serial.write('8');
Serial.write('\r');
}
}

void compare4 (){
if (val5 > A5min && val4 > A4min){//if sensor 5 and 4 are high, stop train
Serial.write('0');
Serial.write('\r');
}
if else(val5<A5min && val4 >A4min){//if sensor 5 is low and sensor 4 is high, run train
Serial.write('8');
Serial.write('\r');
}
}

void compare5 (){
if (val6 > A6min && val5 > A5min){//if sensor 6 and 05are high, stop train
Serial.write('0');
Serial.write('\r');
}
if else(val6<A6min && val5 >A5min){//if sensor 6 is low and sensor 5 is high, run train
Serial.write('8');
Serial.write('\r');
}
}

void compare6 (){
if (val7 > A7min && val6 > A6min){//if sensor 7 and 6 are high, stop train
Serial.write('0');
Serial.write('\r');
}
if else(val7<A7min && val6 >A6min){//if sensor 7 is low and sensor 6 is high, run train
Serial.write('8');
Serial.write('\r');
}
}

void compare7 (){
if (val8 > A8min && val7 > A7min){//if sensor 8 and 7 are high, stop train
Serial.write('0');
Serial.write('\r');
}
if else(val8<A8min && val7 >A7min){//if sensor 8 is low and sensor 7 is high, run train
Serial.write('8');
Serial.write('\r');
}
}

void compare8 (){
if (val0 > A0min && val8 > A8min){//if sensor  and 0 are high, stop train
Serial.write('0');
Serial.write('\r');
}
if else(val0<A0min && val8 >A8min){//if sensor 1 is low and sensor 0 is high, run train
Serial.write('8');
Serial.write('\r');
}
}
``````

Moderator edit: If you put code in a code box, you don’t get smileys and italics in your code. AWOL

reduce 8 analog digital pins into one.

8 what? There are analog pins and there are digital pins. If you noticed, they are on opposite sides of the board. There are no analog digital pins down the middle.

If you have any suggestions, please let me know.

First, when you post code, use the # button before pasting the code in. It puts the code in a nice scrollable box, and prevents the forum software from interpreting things like [ i ] as the start of italics.

Second, don't post ANY code that you haven't bothered to compile. If the issue is that it won't compile, and you don't understand why, say so. But don't post stuff that hasn't a snowballs chance of compiling.

I have not yet measured the min and max values for each photo cell. The min is defined as when a train is not over the sensor. Max is when a train is over the sensor.

Why not? This is one of the first things you should have done. Put in some hardware. Write a sketch to test JUST that hardware. When you know that the hardware is functioning, and is readable/usable, then incorporate that in the main sketch.

I am using an array to call upon the separate channels of the mux.

Why aren't you using arrays to hold the min and max photo sensor values?

You don't need 8 nearly identical compareN() functions. You need one with some arguments.