Здравствуйте. Помогите пожалуйста подправить код. Нужно вместо кнопок встроить потенциометр. Не справился, знаний не хватает. Буду весьма признателен за помошь. Спасибо.
/*********** Powerwheels Sabertooth (Altonate) Version777 *********************
NOTE: Fun revision. Identical to Powerwheels Sabertooth
but revised to use code suggestions of Arduino
Forum guru, Alto777.
Tested on equipment described below, working well
as expected.
!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!
!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!
!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!
THIS CODE WILL ALLOW OVERRIDING OF KID'S RIDE ON TOYS
SUCH AS THOSE MADE BY POWERWHEELS AND PEG PEREGO
IT IS UNTESTED CODE IN ACTUAL RIDE ON WITH RIDER.
USE AT YOUR OWN RISK!
DO NOT, I MEAN DO NOT USE ON RIDE ON TOY WITH ANY
RIDER UNTIL IT HAS BEEN CALIBRATED AND THOROUGHLY
TESTED TO THE SATISFACTION OF A COMPETENT ADULT
CARE PROVIDER FOR THE RIDER IF THE RIDER IS NOT AN
ADULT.
THIS CODE CONTAINS NO EMERGENCY OVERRIDE AND THE TOY
SHOULD ONLY BE OPERATED WITHIN THE REACH OF A
COMPETENT ADULT CARE PROVIDER WHERE A MECHANICALLY
OPERATED EMERGENCY STOP OVERRIDE DEVICE HAS BEEN INSTALLED,
EFFECTIVELY REMOVING ALL BATTERY POWER TO THE MOTORS.
END OF IMPORTANT NOTE
--------- INSTRUCTIONS TO BUILDER ------------
Requires changes to code depending on your hardware
if using two position button type pedal, activate
the line
pinMode(sensorPin, INPUT_PULLUP);
by uncommenting it.
if using typical potentiometer, leave that line
commented out.
Also, in function void checkSpeedPot(){}
have a read through and there's some stuff to set there
Defaults to testing (printing data to PC over Serial)
and assumes pushbutton type "all-or-nothing"
throttle input. These can be adjusted according to
the notes above and the comments found throughout
this sketch.
Also, select the instance of Sabertooth class that suits you
(default is for Arduino Mega or other with Serial1 UART)
The circuit:
- potentiomenter to analog input 0
OR if you use a switch type pedal
- switch pin A0 to GND
- forward switch to D3 to GND
- reverse switch to D4 to GND
- LED on digital pin 13 to GND
- view values in Serial monitor, 9600 baud
- Arduino TX to Sabertooth S1 (Uno) if selected
- Arduino RX to Sabertooth S2 (Uno) if selected
DEFAULT:
- Arduino TX1 to Sabertooth S1 (Mega)
- Arduino RX1 to Sabertooth S2 (Mega)
Tested with 2 x Razor E300 scooter brushed motors @12V, no load
and seems to be working as expected.
Test motor specs:
MY1016-B 24V 350W 2750 RPM electric scooter motor
Sabertooth DIP switch settings for this Packetized Serial Mode:
- 1: D for Packetized Serial address 128
- 2: D
- 3: D for LiPo, U for all other chemistries (battery cutoff switch)
- 4: U for 9600 baud, reverse this for 19200 baud: baud select pin 1 U for autobaud
- 5: U for 9600 baud, reverse this for 19200 baud: baud select pin 2
- 6: U for Standard Simplified Serial
I mean, I'm pretty sure. Google "Sabertooth DIP switch wizard" to be sure.
by Hallowed31
2024-07-27
most excellent code revisions
by Alto777
2024-07-28
Library copyright Dimension Engineering, 2012
"Copyright (c) 2012 Dimension Engineering LLC
See license.txt for license details."
*/
#include <Sabertooth.h>
// add a library
#include <ezButton.h>
/* choose your version, normal or Mega. Use one only.
(or roll your own changing 2nd argument in the Sabertooth instance) */
// use line below for Uno Serial on pins 0 and 1
//Sabertooth ST = Sabertooth(128); // address 128
// use line below for Mega, Leonardo, any with a Serial1
Sabertooth ST = Sabertooth(128); // address 128, port Serial1
const int maximum = 1023; // throttle max as calibrated with analogRead();
const int minimum = 0; // throttle min as calibrated with analogRead();
const byte speedPot = A0;
const byte heartbeatLED = 13;
const byte backwardSwitch = 5;
const byte forwardSwitch = 4;
// make some buttons
ezButton aForwardButton(forwardSwitch);
ezButton aBackwardButton(backwardSwitch);
enum { REVERSE = -1,
NEUTRAL = 0,
FORWARD = 1
};
int directionFlag = NEUTRAL;
int speed = 0;
int m1 = 0; // signal to motor 1
int m2 = 0; // signal to motor 2
unsigned long heartbeatTime;
unsigned long checkSSpeedPotTime;
void setup() {
Serial.begin(9600);
SabertoothTXPinSerial.begin(9600);
Sabertooth::autobaud(SabertoothTXPinSerial);
// the line below is for a button type pedal
// comment this out if using potentiometer
pinMode(speedPot, INPUT_PULLUP);
pinMode(forwardSwitch, INPUT_PULLUP);
pinMode(backwardSwitch, INPUT_PULLUP);
pinMode(heartbeatLED, OUTPUT);
// set the debounce time
aForwardButton.setDebounceTime(25);
aBackwardButton.setDebounceTime(25);
Serial.println("Powerwheels Sabertooth ver.777");
delay(1000); // a second to read sketch name
}
void loop() {
checkSwitches();
if (millis() - heartbeatTime >= 500ul) { //500ms
heartbeatTime = millis();
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
if (millis() - checkSSpeedPotTime >= 100ul) { //100ms
checkSSpeedPotTime = millis();
checkSpeedPot();
}
/************************************************
Other non blocking code goes here.
That means no while(), no delay();
*************************************************/
}
void checkSwitches() {
aForwardButton.loop();
aBackwardButton.loop();
// forward switch
if (aForwardButton.isPressed()) directionFlag = FORWARD;
if (aForwardButton.isReleased()) directionFlag = NEUTRAL;
// backward switch
if (aBackwardButton.isPressed()) directionFlag = REVERSE;
if (aBackwardButton.isReleased()) directionFlag = NEUTRAL;
}
void checkSpeedPot() {
speed = analogRead(speedPot);
// uncomment for testing, comment out for final install
printRawData();
/* IMPORTANT - use only ONE of these speed = map(etc) functions at at time */
// for potentiometer type throttle
// speed = (map(speed, minimum, maximum, 0, 1023)) / 8; //for speed 0 to ±127
// for pushbutton type throttle, map is reversed because INPUT_PULLUP
speed = (map(speed, minimum, maximum, 1023, 0)) / 8; //for speed 0 to ±127
// uncomment for testing, comment out for final install
printMappedData();
speed = constrain(speed, 0, 127); //for speed 0 to ±127
speed = directionFlag * speed;
m1 = speed;
m2 = speed;
// uncomment for testing, comment out for final install
printFinalDriveSignal();
ST.motor(1, m1); // motor 1, speed cast to m1
ST.motor(2, m2); // notor 2, speed cast to m2
}
void printRawData() {
Serial.print("Throttle Input: ");
Serial.print(speed);
Serial.print(" ");
}
void printMappedData() {
Serial.print("Throttle Mapped To: ");
Serial.print(speed);
Serial.print(" ");
}
void printFinalDriveSignal() {
Serial.print(" Final Drive Signal M1: ");
Serial.print(m1);
Serial.print(" Final Drive Signal M2: ");
Serial.println(m2);
Serial.println();
}