force-resistance-arduino's People
force-resistance-arduino's Issues
Converting to 16x2 and Uno
I have altered the code to work with a 16x2 and Uno. Just a few questions.
- What is the polling for? Is this for screen timeouts?
- How does the calibration work? I assume trial and error - changing the value of maxV to match 100 resistance?
- What are button A, B and C used for?
- When printing I got "=" after each word - except it was four lines on top of each other not two. Do you know why? I ended up adding a blank string after to avoid this.
Thanks so much. I plan to make this a christmas gift for my wife. My code is below:
// config
#include "config.h"
//LCD Library
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool displayOn = false;
int fsrLevel = 0;
int fsrNormalized = 0;
int fsrLast = 0;
int fsrReadings = fsrMaxReadings;
int fsrNoChange = 0;
int sleepPolls = 0;
void print(const char *line1, const char *line2);
void setup() {
Serial.begin(115200);
Serial.println("");
Serial.println("Booting...");
activate();
pinMode(BUTTON_A, INPUT_PULLUP);
pinMode(BUTTON_B, INPUT_PULLUP);
pinMode(BUTTON_C, INPUT_PULLUP);
}
void loop() {
if(!digitalRead(BUTTON_A)) calibrate();
if(!digitalRead(BUTTON_B)) displayToggle();
if(!digitalRead(BUTTON_C)) reduceReadingInterval();
getResistance();
if (displayOn) {
if (fsrNoChange > sleepPolls) displayToggle();
} else if (fsrNormalized > 80) displayToggle();
}
void getResistance() {
int fsrTotal = 0;
for(int i = 1; i <= fsrReadings; i++) {
fsrTotal += analogRead(fsrPin);
delay(pollingInterval);
}
fsrLevel = floor(fsrTotal / fsrReadings);
Serial.print("Analog reading = ");
Serial.println(fsrLevel);
fsrNormalized = floor(100 * fsrLevel / maxV);
if (fsrNormalized > 100) fsrNormalized = 100;
String s = String(fsrNormalized);
print("Resistance", s.c_str());
int increment = fsrNormalized == fsrLast ? 1 : 0;
fsrNoChange += increment;
fsrLast = fsrNormalized;
}
void setSleepPolls() {
int pollMS = (pollingInterval * fsrReadings);
sleepPolls = 1000 * secondsTillSleep / pollMS;
fsrNoChange = 0;
Serial.print("Sleep Polls: ");
Serial.println(sleepPolls);
}
void calibrate() {
int minV = analogRead(fsrPin);
printCalibration();
print("Hold Break", "5 seconds");
delay(3000);
maxV = analogRead(fsrPin) - minV;
printCalibration();
}
void reduceReadingInterval() {
if (fsrReadings > 1) {
fsrReadings -= 1;
} else {
fsrReadings = fsrMaxReadings;
}
setSleepPolls();
printInterval();
}
int centerText(const char *buf) {
int16_t x1, y1;
uint16_t w, h;
return 0;
}
void printAt(String text, int X, int Y) {
text = " " + text + " ";
lcd.setCursor(X, Y);
lcd.println(text);
lcd.display();
}
void print(const char *line1, const char *line2 = "") {
if (displayOn) {
lcd.clear();
int x = centerText(line1);
printAt(line1, 0, 0);
x = centerText(line2);
printAt(line2, 5, 1);
}
}
void printCalibration() {
String s = "max: " + String(maxV);
print("Calibrate", s.c_str());
delay(2000);
}
void printInterval() {
String s = "-- " + String(fsrReadings) + " --";
print("Polling", s.c_str());
delay(2000);
}
void activate() {
lcd.begin();
lcd.backlight();
displayOn = true;
Serial.println("Screen on");
print("DIYPELOTON");
delay(200);
printCalibration();
setSleepPolls();
printInterval();
}
void displayToggle() {
if (displayOn) {
activate();
Serial.println("Sleep Activated");
print("Goodbye...");
delay(2000);
lcd.clear();
lcd.display();
lcd.noBacklight();
displayOn = false;
} else {
activate();
}
}
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.