Getting the code to work

This entry is part 2 of 2 in the series Project 10Four

A big part of the 10Four project is detecting a button press. This, I had been told, would be trivial using an Arduino Leonardo. That was true.

It took me all of 20 minutes and a bit of light googling to parse together what I needed. Here’s the code I use.

#include "Keyboard.h"   // This loads the library that allows the Arduino to mimic itself as a standard USB keyboard. 

const int BUTTON_PIN = 7;

int lastState = LOW;
int currentState;

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Keyboard.begin();   // The keyboard library is loaded into memory. 
}

void loop() {
  // read the state of the switch/button:
  currentState = digitalRead(BUTTON_PIN);

  if(lastState == HIGH && currentState == LOW) { // button is pressed
    pressedTime = millis();
    Keyboard.press(KEY_LEFT_SHIFT);
    Keyboard.press(KEY_TAB);
    Keyboard.press('S');S
  }
  else if(lastState == LOW && currentState == HIGH) { // button is released
    Keyboard.releaseAll();   // I instruct the keyboard library to release all keys, ending the PTT
  }

  // save the the last state
  lastState = currentState;
}

Next up is hijacking the CB handset I have. I have already disassembled it and started poking around. I didn’t know these things come with a speaker built in, and we’re not going to use that, so it has to go.

I did however find a wiring diagram that I think is going to be helpful. I am hoping I can wire everything up to run via the existing cable which has four leads in it. Should be doable as long as the button and the microphone can share a ground wire.

Anyhoo. I am cautiously optimistic about this project.