Stream Deck - Code
12/11/2022 | Views: 781 | Arduino | by: ELECTRONOOBS      


First we import the Keyboard library. This will mimic a real keyboard and send different keys using the function press, release or write. A normal PC keyboard could have the keys from F1 up to F12 usually. But virtually we have keys up to F24 and we could also use those. We do that so we won’t interfere with any other key. So in the Arduino code I create a serial communication with the Nextion Display. When I receive a 0, meaning that the first button was pressed, I will send through the USB connection the F13 key using the keyboard library. I do the same for the other numbers. Also, when I detect that one of the real push buttons was pressed, I also send a different key from F20 to F24. That's it.





Arduino Stream Deck Code 12/11/2022

//ASCII codes: http://www.asciitable.com
//Tutorial link: https://electronoobs.com/eng_arduino_tut178.php
//https://www.shellhacks.com/arduino-pro-micro-reset-restore-bootloader/
//https://www.shellhacks.com/arduino-pro-micro-board-selection/
//https://www.shellhacks.com/arduino-pro-micro-reset-restore-bootloader/


#include <Arduino.h>
#include <Keyboard.h>
#include <SoftwareSerial.h>

const byte rxPin = 9;
const byte txPin = 10;
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

//Inputs/Outputs
int button_1 = 2;
int button_2 = 3;
int button_3 = 4;
int button_4 = 5;
int button_5 = 6;
int buzzer = 7;


//Used variables
int delay_time = 300;                             //Delay after button press in ms+
bool button_1_state = true;
bool button_2_state = true;
bool button_3_state = true;
bool button_4_state = true;
bool button_5_state = true;
int tone_duration = 50;
int freq = 3000;

void setup() {
  mySerial.begin(9600);             // Initialise serial communication with the TFT screen
  pinMode(button_1, INPUT_PULLUP);  // Make buttons inputs and turn on the pullup resistor
  pinMode(button_2, INPUT_PULLUP);  // Make buttons inputs and turn on the pullup resistor
  pinMode(button_3, INPUT_PULLUP);  // Make buttons inputs and turn on the pullup resistor
  pinMode(button_4, INPUT_PULLUP);  // Make buttons inputs and turn on the pullup resistor
  pinMode(button_5, INPUT_PULLUP);  // Make buttons inputs and turn on the pullup resistor  
  pinMode(buzzer, OUTPUT);  // Make buttons inputs and turn on the pullup resistor
  digitalWrite(buzzer,LOW);
  Keyboard.begin();                 // Initialise keyboard control
}

void loop() {
  
  if(mySerial.available() >0 )                  //If we receive something...
  {
    int Received = mySerial.read();      //Save the received String in the Received variable
    if(Received == 0)                   //If the first character of "Received" is "1"
    {
      Keyboard.write(KEY_F13);                  //We type this keyboard
      tone(buzzer, freq, tone_duration);
      delay(100);      
    }
    
    if(Received == 1)                   //if is a "1"
    {
      Keyboard.write(KEY_F14);                  //We type this other keyboard
      tone(buzzer, freq, tone_duration);
      delay(delay_time);      
    }

    if(Received == 2)                   //if is a "2"
    {
      Keyboard.write(KEY_F15);                  //We type this other keyboard
      tone(buzzer, freq, tone_duration);
      delay(delay_time);      
    }

    if(Received == 3)                   //if is a "3"
    {
      Keyboard.write(KEY_F16);                  //We type this other keyboard
      tone(buzzer, freq, tone_duration);
      delay(delay_time);      
    }

    if(Received == 4)                   //if is a "4"
    {
      Keyboard.write(KEY_F17);                  //We type this other keyboard
      tone(buzzer, freq, tone_duration);
      delay(delay_time);      
    }

    if(Received == 5)                   //if is a "5"
    {
      Keyboard.write(KEY_F18);                  //We type this other keyboard
      tone(buzzer, freq, tone_duration);
      delay(delay_time);      
    }

    if(Received == 6)                   //if is a "6"
    {
      Keyboard.write(KEY_F19);                  //We type this other keyboard
      tone(buzzer, freq, tone_duration);
      delay(delay_time);      
    }

    if(Received == 7)                   //if is a "6"
    {
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_F13);
      tone(buzzer, freq, tone_duration);
      Keyboard.releaseAll();
      delay(delay_time);      
    }
    
    if(Received == 8)                   //if is a "6"
    {
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_F14);
      tone(buzzer, freq, tone_duration);
      Keyboard.releaseAll();
      delay(delay_time);      
    }

    if(Received == 9)                   //if is a "6"
    {
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_F15);
      tone(buzzer, freq, tone_duration);
      Keyboard.releaseAll();
      delay(delay_time);      
    }

    if(Received == 10)                   //if is a "6"
    {
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_F16);
      tone(buzzer, freq, tone_duration);
      Keyboard.releaseAll();
      delay(delay_time);      
    }

    if(Received == 11)                   //if is a "6"
    {
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_F17);
      tone(buzzer, freq, tone_duration);
      Keyboard.releaseAll();
      delay(delay_time);      
    }

    if(Received == 12)                   //if is a "6"
    {
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_F18);
      tone(buzzer, freq, tone_duration);
      Keyboard.releaseAll();
      delay(delay_time);      
    }

    if(Received == 13)                   //if is a "6"
    {
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_F19);
      tone(buzzer, freq, tone_duration);
      Keyboard.releaseAll();
      delay(delay_time);      
    }
    
  }//end of serial read


  ///////////////////////Extra Push Buttons////////////////
  /////////////////////////////////////////////////////////
  //Button 1
  if(!digitalRead(button_1) && button_1_state){
    button_1_state = false;
    Keyboard.write(KEY_F20);      //Send this key
    tone(buzzer, freq, tone_duration);
    delay(delay_time);   
  }
  else if(digitalRead(button_1) && !button_1_state){
    button_1_state = true;     
  }
  
  //Button 2
  if(!digitalRead(button_2) && button_2_state){
    button_2_state = false;
    Keyboard.write(KEY_F21);      //Send this key
    tone(buzzer, freq, tone_duration);
    delay(delay_time);   
  }
  else if(digitalRead(button_2) && !button_2_state){
    button_2_state = true;     
  }
  
  //Button 3
  if(!digitalRead(button_3) && button_3_state){
    button_3_state = false;
    Keyboard.write(KEY_F22);      //Send this key
    tone(buzzer, freq, tone_duration);
    delay(delay_time);   
  }
  else if(digitalRead(button_3) && !button_3_state){
    button_3_state = true;     
  }
  
  //Button 4
  if(!digitalRead(button_4) && button_4_state){
    button_4_state = false;
    Keyboard.write(KEY_F23);      //Send this key
    tone(buzzer, freq, tone_duration);
    delay(delay_time);   
  }
  else if(digitalRead(button_4) && !button_4_state){
    button_4_state = true;     
  }
  
  //Button 5
  if(!digitalRead(button_5) && button_5_state){
    button_5_state = false;
    Keyboard.write(KEY_F24);      //Send this key
    tone(buzzer, freq, tone_duration);
    delay(delay_time);   
  }
  else if(digitalRead(button_5) && !button_5_state){
    button_5_state = true;     
  }
  /////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////

  
  
}//End of void loop







Last tutorials

Arduino Mirror Steering Control
Two Hand Multimeter - Multiple Modes
Filament from PET bottles machine
Washing Machine Universal Motor - Control speed with AC and DC
Arduino + MCP4725 DAC module

ADVERTISERS



>

ADVERTISERS

PCBWAY PCB service





Curso Arduino Online nivel bajo