Nodes connecting in sequence

Hi, im new to the forum and pretty new to arduino. I need 4 nodes to connect in sequence with a unique number assigned to each node, would the skech below achieve this? maybe ive went about it a long roundabout way? anyway its what came to mind, have a look. any constructive critisism welcomed :)

int nodecount=0; int thisnodenumber=0; int nodeconfirm=0; int syncbutton=5;

void setup() { pinMode(syncbutton,INPUT); Serial.begin(9600); }

void loop() { if(digitalRead(syncbutton)==HIGH&&nodecount==0){ nodecount=1; thisnodenumber=1;// assigns this as node 1 when sync button pressed } if(nodecount==1&&nodeconfirm==0){ Serial.write(50); // tells nodes "I am node 1" } if(digitalRead(syncbutton)==HIGH&&Serial.available()==50){ Serial.write(100); // when pressed on 2nd node confirms nodecount=1 } if (Serial.available()==100){ nodeconfirm =1; // message recived by node 1 and node 1 will stop tx } if(digitalRead(syncbutton)==HIGH&&nodecount==1){ nodecount=2; thisnodenumber=2;//assigns node2 } if(nodecount==2&&nodeconfirm==0){ Serial.write(55); // "I am node2" } if(digitalRead(syncbutton)==HIGH&&Serial.available()==55){ Serial.write(105);//confirm nodecount=2 } if(Serial.available()==105){ nodeconfirm=2;//completes confirm cycle } if(digitalRead(syncbutton)==HIGH&&nodecount==2){ nodecount=3; thisnodenumber=3; } if(nodecount==3&&nodeconfirm==0){ Serial.write(60); } if(digitalRead(syncbutton)==HIGH&&Serial.available()==60){ Serial.write(110); } if(Serial.available()==110){ nodeconfirm=3; } if(digitalRead(syncbutton)==HIGH&&nodecount==3){ nodecount=4; thisnodenumber=4; Serial.write(115); //tell all nodes sequence complete & 4 nodes connected. } if(Serial.available()==115){ nodecount=4; nodeconfirm=4; } }

after a quick review of the sketch myself i can see that it will automatically jump to nodecount=4 and thisnodenumber=4 when the sync button is pressed, any sugestions for a way around it?

Revised code by using communication counter>>>

int nodecount=0;
int thisnodenumber=0;
int nodeconfirm=0;
int comcounter=0;
int syncbutton=5;

void setup()
{
    pinMode(syncbutton,INPUT);
    Serial.begin(9600);
}

void loop()
{
    if(digitalRead(syncbutton)==HIGH&&nodecount==0){
        nodecount=1;
        thisnodenumber=1;// assigns this as node 1 when sync button pressed
    }
    if(nodecount==1&&nodeconfirm==0){
        Serial.write(50);  // tells nodes "I am node 1"
    }
    if(digitalRead(syncbutton)==HIGH&&Serial.available()==50){
        Serial.write(100);
        comcounter++;  // when pressed on 2nd node confirms nodecount=1
    }
    if (Serial.available()==100){
        nodeconfirm =1;  // message recived by node 1 and node 1 will stop tx
    }
    if(digitalRead(syncbutton)==HIGH&&nodecount==1&&comcounter==1){
        nodecount=2;
        thisnodenumber=2;//assigns node2
    }
    if(nodecount==2&&nodeconfirm==0){
        Serial.write(55); // "I am node2"
    }
    if(digitalRead(syncbutton)==HIGH&&Serial.available()==55){
        Serial.write(105);
        comcounter++;//confirm nodecount=2
    }
    if(Serial.available()==105){
        nodeconfirm=2;//completes confirm cycle
    }
    if(digitalRead(syncbutton)==HIGH&&nodecount==2&&comcounter==2){
        nodecount=3;
        thisnodenumber=3;
    }
    if(nodecount==3&&nodeconfirm==0){
        Serial.write(60);
    }
    if(digitalRead(syncbutton)==HIGH&&Serial.available()==60){
        Serial.write(110);
        comcounter++;
    }
    if(Serial.available()==110){
        nodeconfirm=3;
    }
    if(digitalRead(syncbutton)==HIGH&&nodecount==3&&comcounter==3){
        nodecount=4;
        thisnodenumber=4;
        Serial.write(115); //tell all nodes sequence complete & 4 nodes connected.
    }
    if(Serial.available()==115){
        nodecount=4;
        nodeconfirm=4;
    }
}

I don’t see any sign of connecting to anything in your code.

Serial.available() tells you how many bytes have been received - and the max is 64 before the input buffer overflows and data is lost. Have a look at Serial Input Basics - simple reliable ways to receive data.

You need to give a better description of what you want to achieve. For example the description in your Original Post does not mention serial input at all, but it seems to be important.

Can you use a FOR loop such as

for (byte n = 0; n < numNodes; n++) {
   // do stuff
}

You may get some ideas from Planning and Implementing a Program

…R

connections still have to be mage within the code, just experimenting to see what might work. as stated im sorta new to this. i thought serial was one byte with a value of xx?the 4 arduion modules will be connected via nrf24

The Serial library has several functions - you need to read the reference pages. RTFM

You have not provided the better description I requested.

...R

i understand noe the serial.available function. working on adjusting code. the idea behind my thinking is equiptment that has not been automated yet anywhere. when switched on they need to sync each with a unique identifier (1,2,3,4) they need to perform tasks in sync and relay with each other and exchange info like position from linear transceivers etc. needs to be wireless communication ie nrf24

thanks ross

So you rely on the first machine in the assembly line being turned on first? What if the power drops out for a fraction of a second and they all come back on in random order? Then the third machine may think it is first and parts get dropped on the floor.

If the Arduino code is identical on every one (good idea) then you can use the EEPROM to store the location. That way unit #1 always knows it is #1 even when you upload a new version of the program.

Find one of the simple examples for EEPROM that writes a value. Make it write 1, 2, 3 or 4 to location zero on the EEPROM and run that once on each Arduino. Then upload your code which reads that EEPROM location and works out what position it is in.

For future maintainability, it is best to include a small menu system that responds to Serial input and allows you to change the EEPROM values within the main program.

rossc: needs to be wireless communication ie nrf24

Have you looked at TMRh20's Network and Mesh libraries. They may do what you want out of the box. However if it was my project I would be looking for a simpler solution.

And I have to confess I still don't understand what you are hoping to achieve.

If you are writing code for several Arduinos it would be very easy to have a constant (perhaps myID) with a different value in each program. I do that for identifying different trains for my radio controlled system.

...R

the idea is that the assembly line sometimes would need 4, 6 or 8 modules/nodes. the operator could then select nodes at random and position them randomly then sync them up, instead of having to find module 1, position that in its correct position, find node 2 and position etc.etc. and also that production of the actual modules would be simpler if every module had the same program running instead of having to produce 8 different modules. i know it would be much simpler to code it into eeprom but thats not easier for the operator.

OK, so long as you have an operation that works that way, make the computers support that instead of the other way around.

whats the easiest method of achieving what im trying to achieve

There is no easy method. You have to try to think your way around it. What happens if the operator switches on two machines and runs them for a while, then turns on a third one? What should the group of Arduinos do? You need to think of all possible ways things might go wrong and then write the code to cover that.

I think you're on the right track with the NRF24 units.

ok thought that thanks

rossc:
the idea is that the assembly line sometimes would need 4, 6 or 8 modules/nodes. the operator could then select nodes at random and position them randomly then sync them up,

I’m trying to visualize this.

Suppose there are 8 objects A=1, B=2, C=3, D=4, E=5, F=6, G=7, H=8

Do you mean that the operator should be able to physically place 4 of them in the order (for example) BAEG but the objects themselves would still know that their IDs are 2, 1 ,5 ,7?

Is there any situation in which (for example) object G would NOT have the ID of 7?

Sorry if this seems tedious, but the devil (and the solution) is in the detail.

…R

think i’ll take this one back to the drawing board. sounds like a few dip switches inside a cover would be so much simpler

rossc: think i'll take this one back to the drawing board. sounds like a few dip switches inside a cover would be so much simpler

Sounds like an idea worth trying.

The alternative is to have a global constant with a value that is unique to each device and which is part of the uploaded program. If it does not need to be changed while in use, that may be the simplest overall.

I suggest you put some code in setup() to print out the ID value - whether it comes from the DIP switch or from a constant.

...R