helllo , I have a project that I working through & it was pointed out in my code that I will have to change my wireing & code to make it work . MY original code has the keypad in 2,3,4,5,6,7,8,9 , with a interrupt coming in on pin 11 . It was pointed out that I can only have interrupts on pins 2 & 3 . SO heres my question . I spent a evening looking around , reading & watching some youtubes & it looks like I have 3 options .
- Move the keypad up to use pins 4-11 & put my interrupt in pin 2 & my PWM pin in pin 3 .
- Use one of the setups where you only use 2 wires for the keypad by using some resistors .
- Use one of these PCF8574 PCF8574T IO Expansion Board I/O Expander I2C Evaluation Develop Module , heres a link to one . https://www.amazon.com/HiLetgo-PCF8574T-Expansion-Expander-Evaluation/dp/B01ICN5JB6 .
Which one of these 3 would be my best choice ?
The project is to make a keypad operated controller for a MC-2100 Treadmill motor controller . I'll attach my code here in case anyone wants to see where the issue is .
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // replaced #include <Adafruit_LiquidCrystal.h>
LiquidCrystal_I2C lcd(0x27,20,4); // replaced Adafruit_LiquidCrystal lcd(0x27,20,4)
const int pwmPin = 10;
const int tachoPin = 11;
const byte ROW_NUM = 4;
const byte COLUMN_NUM = 4;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6};
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
long rpm = 0;
int maxRpm = 1500;
int targetRpm = 0;
void setup() {
lcd.init(); // replaced lcd.begin(20, 4)
pinMode(pwmPin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(tachoPin), countRpm, RISING);
TCCR1B = TCCR1B & 0b11111000 | 0x01;
lcd.backlight(); // added turn on LCD backlight
lcd.setCursor(0, 0); // added move LCD cursor to column 0 row 0
lcd.print(" Uno Tredmill"); // added display Uno Treadmill on LCD display
delay(2000); // added wait 2 seconds before continuing
}
void countRpm() {
rpm++;
}
void loop() {
char key = keypad.getKey();
if (key) {
int numKey = key - '0';
if (numKey >= 0 && numKey <= 9) {
targetRpm = targetRpm * 10 + numKey;
} else if (key == 'A') {
analogWrite(pwmPin, rpmToDutyCycle(targetRpm));
} else if (key == 'B') {
analogWrite(pwmPin, 0);
targetRpm = 0;
} else if (key == 'C') {
targetRpm++;
analogWrite(pwmPin, rpmToDutyCycle(targetRpm));
} else if (key == 'D') {
targetRpm--;
analogWrite(pwmPin, rpmToDutyCycle(targetRpm));
}
targetRpm = constrain(targetRpm, 0, maxRpm);
}
rpm = 0;
delay(1000);
lcd.setCursor(0, 0);
lcd.print("Target RPM: ");
lcd.print(targetRpm);
lcd.setCursor(0, 1);
lcd.print("Current RPM: ");
lcd.print(rpm * 60);
lcd.setCursor(0, 2);
lcd.print("Duty Cycle: ");
lcd.print(rpmToDutyCycle(targetRpm));
lcd.setCursor(0, 3);
lcd.print("Press A to start, B to stop");
}
int rpmToDutyCycle(int rpm) {
return constrain(map(rpm, 0, maxRpm, 0, 255) * 0.85, 0, 255);
}
//END OF CODE
Thank you
animal12