use of increment

My code is connected to pin 8 for the increment command. But instead of increment by 1, it increments by 4. Is there something special with the pin 8 or my code is wrong?

Thank you for the help!

Without seeing your code it's hard to know. See How to use the Forum

A diagram showing how your switch is connected would also be a good idea. See this Simple Image Guide

...R

These are my codes:

From Transmitter:

int SW1 = 8;

void loop(void){

    if (digitalRead(SW1) == HIGH){
  msg[0] = 11;
  radio.write(msg, 1);
}

From receiver:

void getdata() {    
  if(radio.available()){           
     radio.read(msg, 1);
     
    lcd.setCursor(3,1);
    lcd.print(a);
    
  if (msg[0] == 11) {
    a++;
     delay(1000);   
  }

You're transmitting all the time pin 8 is HIGH.

Try transmitting only when pin 8 becomes HIGH.

See the state change example in the IDE.

If you're asked to post code, make sure you post ALL of the code.

and remember your button might bounce too...

Here are my codes:

FOR TRANSMITTER:

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

int msg[1];

#define CE_PIN   9
#define CSN_PIN 10
                                     
const byte Address[5] = {'R','x','A','A','A'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

int button_1 = 8;

int button_2 = 7;

int button_3 = 4;

int button_4 = 2;

int led = 3;

void setup(){
  radio.begin();                     .
  radio.openWritingPipe(Address);        //sets the address of the receiver to which the program will send data.

  pinMode(led, OUTPUT);
}

void loop(){

    if (digitalRead(button_1) == HIGH){
  msg[0] = 01;
  radio.write(msg, 1);
}

  if (digitalRead(button_2) == HIGH){
  msg[0] = 02;
  radio.write(msg, 1);
}

  if (digitalRead(button_3) == HIGH){
  msg[0] = 03;
  radio.write(msg, 1);
}

  if (digitalRead(button_4) == HIGH){
  msg[0] = 04;
  radio.write(msg, 1);           

  digitalWrite(led, HIGH);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
  digitalWrite(led, LOW); 
}

}

FOR RECEIVER:

#include <SPI.h>      //the communication interface with the modem
#include <RF24.h>   
#include <nRF24L01.h>

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 6, 5, 4, 3, 2);

#define CE_PIN   9
#define CSN_PIN 10

const byte thisAddress[5] = {'R','x','A','A','A'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

int msg[1];

int a = 0;
int b = 0;

void setup(){
  lcd.begin(20, 4);  
  lcd.setCursor(0,0);
  lcd.print("Now serving");
  lcd.setCursor(12,0);
  lcd.print("Counter");
  
   
  radio.begin();                    
  radio.openReadingPipe(1, thisAddress);  
  radio.startListening();          
   
   }

void loop() {    
  if(radio.available()){           
     radio.read(msg, 1);
     
    lcd.setCursor(3,1);
    lcd.print(a);
    
  if (msg[0] == 01) {
    a++;
     delay(1000);   
  }
    
  if (msg[0] == 02){      
    lcd.setCursor(15,1);
    lcd.print('1');
     delay(200);
  }

  if (msg[0] == 03){
    lcd.setCursor(15,1);
    lcd.print('2');
     delay(200);
  }
 
  if (msg[0] == 04){
    a=0;
    lcd.setCursor(15,1);
    lcd.print(" ");
     delay(200);
  }

  if (a == 20){
    lcd.setCursor(3,1);   
    lcd.print(b);
    delay(200);
  }
 }
}

RESULTS:
*LCD displays correctly.
*The value of "a" increments by 4 (which should be only 1).
*The code to display "2" doesn't work.
*Do not reset when button_4 is pressed.

please pay attention to comments... if you don’t understand them, ask about them, or there is no point in trying to give you any advice

=> Read again #3... your arduino is faster than you releasing your button
=> You did not comment on #4 (the bouncing) either...

how are your button wired? Have a cap ? and I assume they are not INPUT_PULLUP (not declared so), so do you have a resistor and going to where?

Also:

  • declaring an array of one int is the same as just declaring an int, would make sense to use just one int
  • you are asking to send just one byte although your array of int has 2 bytes. Will become an issue if you had larger data to send.
  • the notation 01 02 etc for your data to send is an octal-literal notation - probably not what you have in mind (and will bite you if you were to write 09 or 011)