Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast analog #27

Merged
merged 4 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ Follow a guide like [fsr-pad-guide](https://github.com/Sereni/fsr-pad-guide) or
## Firmware setup
1. Install [Arduino IDE](https://www.arduino.cc/en/software) (skip this if you're using OSX as it's included in Teensyduino)
1. Install [Teensyduino](https://www.pjrc.com/teensy/td_download.html) and get it connected to your Teensy and able to push firmware via Arduino IDE
1. In Arduinio IDE, set the `Tools` > `USB Type` to `Serial + Keyboard + Mouse + Joystick` (or `Serial + Keyboard + Mouse`)
1. In Arduinio IDE, set the `Tools` > `Board` to your microcontroller (e.g. `Teensy 4.0`)
1. In Arduinio IDE, set the `Tools` > `Port` to select the serial port for the plugged in microcontroller (e.g. `COM5` or `/dev/something`)
1. Copy in the code from [fsr.ino](./fsr.ino)
1. By default, [A0-A3 are the pins](https://forum.pjrc.com/teensy40_pinout1.png) used for the FSR sensors in this software. If you aren't using these pins [alter the SensorState array](fsr.ino#L204-L209)
1. In Arduino IDE, set the `Tools` > `USB Type` to `Serial + Keyboard + Mouse + Joystick` (or `Serial + Keyboard + Mouse`)
1. In Arduino IDE, set the `Tools` > `Board` to your microcontroller (e.g. `Teensy 4.0`)
1. In Arduino IDE, set the `Tools` > `Port` to select the serial port for the plugged in microcontroller (e.g. `COM5` or `/dev/something`)
1. Load [fsr.ino](./fsr.ino) in Arduino IDE.
1. By default, [A0-A3 are the pins](https://forum.pjrc.com/teensy40_pinout1.png) used for the FSR sensors in this software. If you aren't using these pins [alter the SensorState array](./fsr.ino#L437-L442)
1. Push the code to the board

### Testing and using the serial monitor
Expand Down
21 changes: 19 additions & 2 deletions fsr.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
#define CAN_AVERAGE
#endif

#if defined(_SFR_BYTE) && defined(_BV) && defined(ADCSRA)
#define CLEAR_BIT(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define SET_BIT(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif


#ifdef CORE_TEENSY
// Use the Joystick library for Teensy
void ButtonStart() {
Expand Down Expand Up @@ -479,9 +485,11 @@ class SerialProcessor {
// e.g. 3180 (fourth FSR, change threshold to 180)

if (bytes_read < 2 || bytes_read > 5) { return; }

size_t sensor_index = buffer_[0] - '0';
if (sensor_index < 0 || sensor_index >= kNumSensors) { return; }
//this works for chars < '0' because they will
//also be > kNumSensors due to uint underflow.
if (sensor_index >= kNumSensors) { return; }

kSensors[sensor_index].UpdateThreshold(
strtoul(buffer_ + 1, nullptr, 10));
Expand Down Expand Up @@ -534,6 +542,15 @@ void setup() {
// Button numbers should start with 1.
kSensors[i].Init(i + 1);
}

#if defined(CLEAR_BIT) && defined(SET_BIT)
// Set the ADC prescaler to 16 for boards that support it,
// which is a good balance between speed and accuracy.
// More information can be found here: http://www.gammon.com.au/adc
SET_BIT(ADCSRA, ADPS2);
CLEAR_BIT(ADCSRA, ADPS1);
CLEAR_BIT(ADCSRA, ADPS0);
#endif
}

void loop() {
Expand Down