Hi, I am new to this arduino thing and was wondering if anyone would be able to give me some insight as to how i might be able to combine these two codes to control a motor attached to an esc with a potentiometer. I don’t expect anyone to completely combine the code to my liking but I would be very thankful to anyone who could give advice on this.
Here is the ESC code:
caution, this code sweeps the motor up to maximum speed !
// make sure the motor is mounted securily before running.
#include <Servo.h>
Servo myservo;
void arm(){
// arm the speed controller, modify as necessary for your ESC
setSpeed(0);
delay(1000); //delay 1 second, some speed controllers may need longer
}
void setSpeed(int speed){
// speed is from 0 to 100 where 0 is off and 100 is maximum speed
//the following maps speed values of 0-100 to angles from 0-180,
// some speed controllers may need different values, see the ESC instructions
int angle = map(speed, 0, 100, 0, 180);
myservo.write(angle);
}
void setup()
{
myservo.attach(9);
arm();
}
void loop()
{
int speed;
// sweep up from 0 to to maximum speed in 20 seconds
for(speed = 0; speed <= 100; speed += 5) {
setSpeed(speed);
delay(1000);
}
// sweep back down to 0 speed.
for(speed = 95; speed > 0; speed -= 5) {
setSpeed(speed);
delay(1000);
}
setSpeed(0);
delay(5000); // stop the motor for 5 seconds
}
And here is the potentiometer code (just reads and displays the input on the serial monitor) :
int analogPin = 0; // potentiometer wiper (middle terminal) connected to analog pin 0
// outside leads to ground and +5V
int val = 0; // variable to store the value read
void setup()
{
Serial.begin(9600); // setup serial
}
void loop()
{
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}
No attempt was made to compile or test the following -
// potentiometer wiper (middle terminal) connected to analog pin 0
// outside leads to ground and +5V
#include <Servo.h>
// --- constants
const uint8_t pinPotentiometer = 0;
const uint8_t pinServo = 9;
const unsigned long ONE_SECOND = 1000UL;
const unsigned long FIVE_SECONDS = 5UL * ONE_SECOND;
// -- variables
Servo servo;
// --- subroutines
void reportPot()
{
int val;
val = analogRead(pinPotentiometer);
Serial.println(val);
}
void setSpeed(int speed)
{
// speed is from 0 to 100 where 0 is off and 100 is maximum speed
// the following maps speed values of 0-100 to angles from 0-180,
// some speed controllers may need different values, see the ESC instructions
int angle;
angle = map(speed, 0, 100, 0, 180);
servo.write(angle);
}
void arm()
{
// arm the speed controller, modify as necessary for your ESC
setSpeed(0);
// some speed controllers may need longer
delay(ONE_SECOND);
}
// --- required 'setup' and 'loop'
void loop()
{
// read a potentiometer and display the result to the console
reportPot();
// sweep up from 0 to to maximum speed in 20 seconds
for ( int speed = 0; speed <= 100; speed += 5 )
{
setSpeed(speed);
delay(ONE_SECOND);
}
// sweep back down to 0 speed.
for ( int speed = 95; speed > 0; speed -= 5 )
{
setSpeed(speed);
delay(ONE_SECOND);
}
// stop the motor for a time
setSpeed(0);
delay(FIVE_SECONDS);
}
void setup()
{
// initialize the serial port for debugging output to console
Serial.begin(9600);
servo.attach(pinServo);
arm();
}
or No, as one is taken care of by the analogRead(),
and the other by the servo.attach()?
I believe you can keep constants in flash while hard-coded indexes use RAM.
But it’s cycles slower through flash. Printing bytes to serial from flash, the text ‘flows’.
If you’ll look at the ‘analogRead’ examples you’ll note none of them do a ‘setMode’ for the analog pins so it appears during whatever setups up the board before it gets to ‘setup’ or ‘loop’ does it for you. So the answer is NO you do NOT need to do so yourself.
Next if you’ll look at the source for the examples you’ll see the same thing there as well. Or looking at the Servo library source for ‘attach’ you’ll note it does a ‘setMode’ to ‘OUTPUT’ when called using the pin number passed in.
or No, as one is taken care of by the analogRead(),
and the other by the servo.attach()?
I believe you can keep constants in flash while hard-coded indexes use RAM.
But it's cycles slower through flash. Printing bytes to serial from flash, the text 'flows'.
While I'm not entirely sure what you're try to say I do believe it has nothing to do with the quoted code.
So they are already only stored in and used from flash even if not declared PROGMEM?
I run 0022; thought I read here last year that constants not made PROGMEM all loaded to RAM at startup, even hard-coded numbers like index limits and pin numbers had 1 copy in RAM.
I’m going to punt (for now) and suggest you read the following (yes the whole page) and come back and we can discuss it if you still don’t get what’s happening and why.
Alright, not sure with what this constant stuff has to do with anything but can anyone tell me what I'd need to learn to go about writing this code so that the speed of the motor varies with the values that the petentiometer inputs? Thanks
The constants are readable, unmodifiable (at run time) synonyms for numerical values. They allow for a more human readable code than hardcoded numbers often seen in source code. Which would you rather see in source code.
digitalWrite(13, HIGH);
or
digitalWrite(pinLED_ON_BOARD, LED_ON);
me I’d much rather read the second.
You can specify both ‘pinLED_ON_BOARD’ and ‘LED_ON’ specifying the following previous to there use.
I was just looking at the Arduino~Knob page and was wondering, even though a servo uses PPM and an ESC uses PWM, can the loop of this code be used for both?