Simple keyboard program holding bottom

hi every one
i made a wireless keyboard with NRF
wich have 4 keys F1, F2, left shift and left control
program and arduino works perfectly
but
the problem is , i cant hold bottoms
i mean when i hold bottom it hit rapidly
but what i want is to hold the bottom ( connect ) and when i release the bottom ( disconnect)
the bottom just as a real key board

i beg to help me
i add reciever and transfer codes and schematics of boards

please help me
its like cancer for me

Processing: wireless keyboard.rar...
1
2
Receiver_e5.ino (1.5 KB)
Transfer_e5.ino (1.6 KB)

Reciver code

#include <Keyboard.h>
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
RF24 radio(7,8); // (ce,cs)
RF24Network network(radio);
const uint16_t this_node = 00;
const uint16_t other_node = 03;

unsigned int laser_state=0;
const unsigned long my_interval = 500;
unsigned long last_sent;
unsigned int income=0;

void setup(void)
{
Serial.begin(115200);
pinMode(9,INPUT_PULLUP);
SPI.begin();
radio.begin();
network.begin(90,this_node);
pinMode(4,OUTPUT);
digitalWrite(4,LOW);
}

void loop(void){
digitalWrite(4,LOW);
network.update();

if ( network.available() ) {
RF24NetworkHeader header;
network.read(header,&income,sizeof(income));
if(income==10){
Serial.println("f1");
Keyboard.press(KEY_F1);
income=0;
}
if(income==11){
Serial.println("f2");
Keyboard.press(KEY_F2);
income=0;
}
if(income==12){
Serial.println("ctrl");
Keyboard.press(KEY_LEFT_CTRL);
income=0;
}
if(income==13){
Serial.println("shift");
Keyboard.press(KEY_LEFT_SHIFT);
income=0;
}
Keyboard.releaseAll();
}

unsigned long now = millis();
if ( now - last_sent >= my_interval )
{
// Serial.print("pin ");
// Serial.println(digitalRead(9));
last_sent = now;
laser_state = digitalRead(9);
RF24NetworkHeader header(other_node);
bool ok = network.write(header,&laser_state,sizeof(laser_state));
}
}

and for Transfer



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



RF24 radio(8,7);
RF24Network network(radio);
const uint16_t this_node = 03;
const uint16_t other_node = 00;

unsigned int laser_state=0;
const unsigned long interval = 5;
unsigned long last_sent;

unsigned int b1=1;
unsigned int b2=1;
unsigned int b3=1;
unsigned int b4=1;

unsigned int d1=10;
unsigned int d2=11;
unsigned int d3=12;
unsigned int d4=13;

//TR1
int btn1 = 3;
int btn2 = 4;
int btn3 = 5;
int btn4 = 6;

void setup(void)
{
//  Serial.begin(9600);
  pinMode(btn1,INPUT_PULLUP);
  pinMode(btn2,INPUT_PULLUP);
  pinMode(btn3,INPUT_PULLUP);
  pinMode(btn4,INPUT_PULLUP);
  SPI.begin();
  radio.begin();
  network.begin(90,this_node);
}


void loop(void){
  
  network.update();

  unsigned long now = millis();
  if ( now - last_sent >= interval  )
  {
    last_sent = now;
    b1 = digitalRead(btn1);
    b2 = digitalRead(btn2);
    b3 = digitalRead(btn3);
    b4 = digitalRead(btn4);
    if(b1==0){
//      Serial.println("F1");
      RF24NetworkHeader header(other_node);
      bool ok = network.write(header,&d1,sizeof(d1));
    }
    if(b2==0){
//      Serial.println("F2");
      RF24NetworkHeader header(other_node);
      bool ok = network.write(header,&d2,sizeof(d2));
    }
     if(b3==0){
//      Serial.println("Ctrl");
      RF24NetworkHeader header(other_node);
      bool ok = network.write(header,&d3,sizeof(d3));
    }
    if(b4==0){
//      Serial.println("Shift");
      RF24NetworkHeader header(other_node);
      bool ok = network.write(header,&d4,sizeof(d4));
    }
  }
}

Please modify your post so those who want to help you do not have to download anything.
You can post your code directly here using the code block </> in your reply editor window.

thanks i will do it right now

Awesome, now people can help you better.

I can see a tiny flaw in the way you are using millis().

This creates inconsistent timing as now could have been delayed by other processes. Instead add your timing interval to last_sent to avoid a time creep. While that should be changed I do not believe that it is your problem.

There are 4 reading that you can get off of a button based on its two states of on or off:

  • just became on
  • still on
  • just became off
  • still off

If you record the states of the button like:

bool button_state = digitalRead(button_pin) == LOW; 

Then you can compare it to a current state variable to determine if it maintaining a state or has just changed to that state. Given that you can repeat commands when a key is pressed and held.

1 Like

thank you for your kindness and your help
but i have no knowledge of programming
i just can upload codes in Arduino
its hard for me to ask but
can you please do corrections to my codes and send them for me
???
:pensive:

thank you
i fix it with another way
but
thank you very muck

There's no real incentive to do that.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.