Add potentiometer control and debug messages

This commit is contained in:
Elizabeth Cray 2025-01-19 00:31:51 -05:00
parent dbc07c7a0f
commit 9aa1838a26

View File

@ -18,45 +18,73 @@
/* TODO: /* TODO:
* - Implement sending strings * - Implement sending strings
* - Add OLED Support * - Add OLED Support
* - Add Potentiometer support
*/ */
uint8_t input_dit = 3, input_dah = 6; boolean debug = true;
uint8_t input_dit = 3, input_dah = 6, input_pot = A6;
uint8_t output = 7; uint8_t output = 7;
uint16_t dit = 200, dah = 500; uint16_t dit = 200, dah = 500;
uint8_t debounce = 50;
uint16_t p_tol = 10, p_min = 50, p_max = 2000, p_buf = 0, p_cache = 0;
String serial_buffer = ""; String serial_buffer = "";
char buf; char buf;
void adjust_length(uint16_t);
void set_length(uint16_t, uint16_t); void set_length(uint16_t, uint16_t);
void send(bool); void send(bool);
void process_input(String); void process_input(String);
void log(String);
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
log("Set PinModes\n");
pinMode(input_dit, INPUT_PULLUP); pinMode(input_dit, INPUT_PULLUP);
pinMode(input_dah, INPUT_PULLUP); pinMode(input_dah, INPUT_PULLUP);
pinMode(input_pot, INPUT);
pinMode(output, OUTPUT); pinMode(output, OUTPUT);
digitalWrite(output, HIGH); digitalWrite(output, HIGH);
log("Read EEPROM\n");
// dumbass
pinMode(9, OUTPUT);
digitalWrite(9, LOW);
dit = EEPROM.read(20); dit = EEPROM.read(20);
dah = EEPROM.read(21); dah = EEPROM.read(21);
// debug = EEPROM.read(22);
p_buf = analogRead(input_pot);
adjust_length(p_buf);
log("Setup complete\n");
log("Dit length: ");
log(dit);
log("\n");
log("Dah length: ");
log(dah);
log("\n");
log("Potentiometer: ");
log(p_buf);
log("\n");
} }
void loop() { void loop() {
log(".");
if (digitalRead(input_dah)){ if (digitalRead(input_dah)){
send(true); send(true);
}else if (digitalRead(input_dit)){ }else if (digitalRead(input_dit)){
send(false); send(false);
} }
p_cache = analogRead(input_pot);
if (abs(p_cache - p_buf) > p_tol){
p_buf = p_cache;
if (p_cache < p_min){
p_cache = p_min;
} else if (p_cache > p_max){
p_cache = p_max;
}
adjust_length(p_cache);
}
if (Serial.available() > 0) { if (Serial.available() > 0) {
buf = Serial.read(); buf = Serial.read();
log("SR ");
log(buf);
log("\n");
if ((uint16_t) buf == 13){ if ((uint16_t) buf == 13){
process_input(serial_buffer); process_input(serial_buffer);
serial_buffer = ""; serial_buffer = "";
@ -64,6 +92,13 @@ void loop() {
serial_buffer += buf; serial_buffer += buf;
} }
} }
delay(debounce);
}
void adjust_length(uint8_t adjusted_length){
uint16_t new_dah = map(adjusted_length, 0, 1023, p_min, p_max);
set_length(new_dah/2, new_dah);
} }
void process_input(String input){ void process_input(String input){
@ -71,6 +106,10 @@ void process_input(String input){
// S<string> send string as CW, unavail chars become " " // S<string> send string as CW, unavail chars become " "
// I<int> set did length (ms) // I<int> set did length (ms)
// A<Int> set dah length (ms) // A<Int> set dah length (ms)
// D<0/1> set debug mode
log("Processing input: ");
log(input);
log("\n");
char command = input[0]; char command = input[0];
input.remove(0); input.remove(0);
uint16_t length = 0; uint16_t length = 0;
@ -86,6 +125,10 @@ void process_input(String input){
length = input.toInt(); length = input.toInt();
set_length(dit, length); set_length(dit, length);
break; break;
case 'D':
debug = input.toInt()==1;
EEPROM.write(22, debug);
break;
default: default:
Serial.println('?'); Serial.println('?');
break; break;
@ -93,15 +136,28 @@ void process_input(String input){
} }
void send(bool b) { void send(bool b) {
log("Sending ");
log(b?"dah":"dit");
log("\n");
digitalWrite(output, LOW); digitalWrite(output, LOW);
delay(b?dit:dah); delay(b?dit:dah);
digitalWrite(output, HIGH); digitalWrite(output, HIGH);
} }
void set_length(uint16_t length_dit, uint16_t length_dah) { void set_length(uint16_t length_dit, uint16_t length_dah) {
log("Setting dit length to ");
log(length_dit);
log(" and dah length to ");
log(length_dah);
dit = length_dit; dit = length_dit;
dah = length_dah; dah = length_dah;
EEPROM.write(20, length_dit); EEPROM.write(20, length_dit);
EEPROM.write(21, length_dah); EEPROM.write(21, length_dah);
return; return;
} }
void log(String content){
if (debug){
Serial.print(content);
}
}