Coding Issue-Vibration sensor to nrf24 to 7 segment display

I am pretty new to this but am trying to figure out the proper way to write this code. I have 3 codes that work individually and I am trying to now make them work over a wireless frequency. I have one code that takes a sw420 vibration sensor and turns on a light at a certain measurement of impact (works on its own). One code that counts up and triggers a relay when a button is pushed (works on its own). And I have one code that works when a button is pushed to turn on a led through and nrf24.

I am trying to now combine all 3 principals into 2 codes (tx and rx) and am having trouble understanding how to tie it all together. I have spend the last few days trying to figure this out, but most of what I’ve been able to learn has been through reverse engineering working codes to understand why they work and then apply them into newer or modified versions of that code.

The objective is as follows;
-one arduino has a vibration sensor with a threshold and sends a reading of HIGH when that is reached over an nrf24.
-the second arduino takes that reading (HIGH) and counts on the 7 segment (0-9) as well as triggering a relay (this will likely be a buzzer and/or an led but I’m just calling it a relay for now).

Application;
I am a long range shooter and at longer ranges, it is very hard to tell if you hit your target. I plan to mount the first module to the back of a steel target with the second unit next to me so that I know if I have hit the target.

I will ultimately be using 2 nrf24s rated for 1000m and will be setting it to high to get the most range possible after I get the coding figured out.

Also, I know you like to see wiring diagrams and can draw one up if need be but I have tested every aspect individually and they work with all of the individual codes, I am 99% sure it is a coding issue.

I can upload the 3 original codes if needed too.

What I’m using;
2 arduino nanos, 2 nrf24s, 2 nrf24 voltage regulators, 7 segment display, 5v relay, and a sw420 vibration sensor.

Thanks

TX_code.ino (877 Bytes)

RX_code.ino (4.43 KB)

@mct89 - your duplicate topic has been deleted as it was in the wrong place as you realised. Rather than starting a new topic it could have been moved here if you has reported it to a moderator

UKHeliBob:
@mct89 - your duplicate topic has been deleted as it was in the wrong place as you realised. Rather than starting a new topic it could have been moved here if you has reported it to a moderator

Thanks, I wasn't aware that was how it worked. I tried to delete it but it wouldn't let me

tx code:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
////////////

RF24 radio(9, 10);

int vs =6; // vibration sensor
int buttonUpState;

const byte address[6] = "00001";
/////////////////
void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  //set range MIN LOW HIGH MAX
  radio.setPALevel(RF24_PA_HIGH);
  radio.setDataRate(RF24_250KBPS);
  radio.stopListening();

  pinMode(vs, INPUT);
}

///////////////////////////////
void loop() {
  long measurement =vibration();
  delay(50);
  Serial.println(measurement);
  
  if (measurement > 50){
    radio.write(buttonUpState, HIGH);
  }
  else{
    radio.write(buttonUpState, LOW); 
  }
    //how often it sends
  delay(1500);
}
 
long vibration(){
  long measurement=pulseIn (vs, HIGH);  //wait for the pin to get HIGH and returns measurement
  return measurement;
}

rx code:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9,10);


const byte address[6] = "00001";

//Initialize the 7 segment pins
int A = 3;
int B = 2;
int C = A2;
int D = A1;
int E = A0;
int F = 4;
int G = 5;

int Relay = 6;


//Initialize the push buttons pins, push buttons states, and the counter
int switchUpPin = 7;///////// not sure I need this any more, obviously the switch should be coming from tx
int counter = 0;
int buttonUpState = 0;
int lastButtonUpState = 0;

void setup() {
  // put your setup code here, to run once:
  
  radio.begin();
  radio.openReadingPipe(0,address);
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);
  radio.startListening();

  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
 

  pinMode (Relay, OUTPUT);
}

void loop() {

  
  if (radio.available()) {
    radio.read(buttonUpState, HIGH); // Read information from the NRF24L01

    /////////////////////////////
 if (buttonUpState != lastButtonUpState) 
  {
    if (buttonUpState == HIGH) 
    {
      //Reset the counter to -1
      if(counter == 9)
      {
        counter = -1;
      }
      //Increase the counter by 1
      counter++;
      //Print the counter to the console and calling the function
      Serial.println(counter);
      changeNumber(counter);
      //Delaying by 250 ms
      delay(250);
       digitalWrite(Relay,HIGH);
      delay(1500);
      digitalWrite(Relay,LOW);
    //time before the button can be pushed again
      delay(2000);
    }
    else
    {
        Serial.println("OFF");
    }
    //Delay to avoid button bouncing
   delay(1000);
  }
  //Calling the function changeNumber with the arg counter

  changeNumber(counter);
  //relay turned off
    digitalWrite(Relay,LOW);
}
}
  
//The function to display the numbers
void changeNumber(int buttonPress)
{
  switch (buttonPress)
  {
    //number 0
    case 0:
      digitalWrite(A, LOW);
      digitalWrite(B, LOW);
      digitalWrite(C, LOW);
      digitalWrite(D, LOW);
      digitalWrite(E, LOW);
      digitalWrite(F, LOW);
      digitalWrite(G, HIGH);
      break;
    //number 1
    case 1:
      digitalWrite(A, HIGH);
      digitalWrite(B, LOW);
      digitalWrite(C, LOW);
      digitalWrite(D, HIGH);
      digitalWrite(E, HIGH);
      digitalWrite(F, HIGH);
      digitalWrite(G, HIGH);
      break;
    //number 2
    case 2:
      digitalWrite(A, LOW);
      digitalWrite(B, LOW);
      digitalWrite(C, HIGH);
      digitalWrite(D, LOW);
      digitalWrite(E, LOW);
      digitalWrite(F, HIGH);
      digitalWrite(G, LOW);
      break;
    //number 3
    case 3:
      digitalWrite(A, LOW);
      digitalWrite(B, LOW);
      digitalWrite(C, LOW);
      digitalWrite(D, LOW);
      digitalWrite(E, HIGH);
      digitalWrite(F, HIGH);
      digitalWrite(G, LOW);
      break;
    //number 4
    case 4:
      digitalWrite(A, HIGH);
      digitalWrite(B, LOW);
      digitalWrite(C, LOW);
      digitalWrite(D, HIGH);
      digitalWrite(E, HIGH);
      digitalWrite(F, LOW);
      digitalWrite(G, LOW);
      break;
    //number 5
    case 5:
      digitalWrite(A, LOW);
      digitalWrite(B, HIGH);
      digitalWrite(C, LOW);
      digitalWrite(D, LOW);
      digitalWrite(E, HIGH);
      digitalWrite(F, LOW);
      digitalWrite(G, LOW);
      break;
    //number 6
    case 6:
      digitalWrite(A, LOW);
      digitalWrite(B, HIGH);
      digitalWrite(C, LOW);
      digitalWrite(D, LOW);
      digitalWrite(E, LOW);
      digitalWrite(F, LOW);
      digitalWrite(G, LOW);
      break;
    //number 7
    case 7:
      digitalWrite(A, LOW);
      digitalWrite(B, LOW);
      digitalWrite(C, LOW);
      digitalWrite(D, HIGH);
      digitalWrite(E, HIGH);
      digitalWrite(F, HIGH);
      digitalWrite(G, HIGH);
      break;
    //number 8
    case 8:
      digitalWrite(A, LOW);
      digitalWrite(B, LOW);
      digitalWrite(C, LOW);
      digitalWrite(D, LOW);
      digitalWrite(E, LOW);
      digitalWrite(F, LOW);
      digitalWrite(G, LOW);
      break;
    //number 9
    case 9:
      digitalWrite(A, LOW);
      digitalWrite(B, LOW);
      digitalWrite(C, LOW);
      digitalWrite(D, HIGH);
      digitalWrite(E, HIGH);
      digitalWrite(F, LOW);
      digitalWrite(G, LOW);
      break;
  }
}