/*how the standard 50 Hz 1,0 milliseconds to 2,0 milliseconds
upper: RC-control-signal controls a servo*/
#include <Servo.h>
Servo ESC;
int Speed;
void setup(){
ESC.attach(9,1000,2000);
}
void loop(){
Speed = analogRead(A0);
Speed = map(Speed, 0, 1023, 0, 180);
ESC.write(Speed);
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
#include <Servo.h>
byte servoPin = 9; // signal pin for the ESC.
byte potentiometerPin = A0; // analog input pin for the potentiometer.
Servo servo;
void setup() {
servo.attach(servoPin);
servo.writeMicroseconds(1500); // send "stop" signal to ESC. Also necessary to arm the ESC.
delay(7000); // delay to allow the ESC to recognize the stopped signal.
}
void loop() {
int potVal = analogRead(potentiometerPin); // read input from potentiometer.
int pwmVal = map(potVal,0, 1023, 1100, 1900); // maps potentiometer values to PWM value.
servo.writeMicroseconds(pwmVal); // Send signal to ESC.
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// ref: https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/
// forum on Arduino PWM: https://forum.arduino.cc/index.php?topic=354160.0
// Schematic: https://content.arduino.cc/assets/UNO-TH_Rev3e_sch.pdf
// Find min and max compare register values to get 1 ms to 2 ms pulse
// Using 16-bit timer with prescale of 8
// Register value should be number of clock cycles before reset - 1 b/c we count up from 0
int OCR1B_min = 1250; //+1=2000 clock cycles to get 1 ms pulse width (off)
int OCR1B_max = 4750; // to get 2 ms pulse width (max speed)
void setup() {
Serial.begin(9600);
// Set timer output pin data direction
// From ATMEGA328/P datasheet pgs 13 and 14 and the UNO schematic
// OC1B -> PB2 -> MCU Pin 16 -> Board Pin 10
// pg 167: Actual OC1x value will only be visible if data direction for the port pin is set as output (DDR_OC1x)
pinMode(10, OUTPUT);
// Create variables to store the values to be written to the TIM1 registers
// Control registers are 8 bits (char)
char TCCR1A_pre = 0x00;
char TCCR1B_pre = 0x00;
// Set OC1B to non-inverting mode
// pg 167: INCORRECT INFO: Non-inverted PWM output can be generated by writing the COM1x[1:0] to 0x3
// pg 174-175: looks like it is actually 0x2 in register description
// Below code was tested working
TCCR1A_pre |= _BV(COM1B1);
// Set waveform generation mode
// pg 165: counter is incremented until counter value matches value in OCR1A (WGM1[3:0]=0xF)
// pg 175: WGM1[3:2] bits found in TCCR1B register, WGM1[1:0] found in TCCR1A register
TCCR1B_pre |= _BV(WGM13) | _BV(WGM12);
TCCR1A_pre |= _BV(WGM11) | _BV(WGM10);
// PORTC |= (_BV(0) | _BV(2) | _BV(7)); // Set bits 0,2,7
// Select the prescaled clock to use. See Excel worksheet "PWMCalcs" to see justification.
TCCR1B_pre |= _BV(CS11);
// Write control registers
TCCR1A = TCCR1A_pre;
TCCR1B = TCCR1B_pre;
// Write output compare registers (2 bytes)
// pg 167: f_OCnxPWM = f_CLK_IO / (N*(1+TOP))
OCR1A = 39999; // To get 50 Hz frequency
OCR1B = OCR1B_min; // start at 1 ms pulse width
}
void loop() {
// Read analog input
// Voltages are between 0-5V on an UNO per https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/
int potVal = analogRead(A0);
Serial.println(potVal);
// Map the input to the timer output
// analogRead() values go from 0 to 1023. We want to change the register from min to max specified above
// pg 166: OCR1A is double bufferred, updated when TCNT1 matches TOP
OCR1B = map(potVal, 0, 1023, OCR1B_min, OCR1B_max);
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
/*
Try this test sketch with the Servo library to see how your
ESC responds to different settings, type a speed (1000 - 2000)
in the top of serial monitor and hit [ENTER], start at 1500
and work your way toward 1000 50 micros at a time, then toward
2000.
*/
#include <Servo.h>
Servo esc;
void setup() {
// initialize serial:
Serial.begin(9600); //set serial monitor baud rate to match
esc.writeMicroseconds(1500);
esc.attach(9,1000,2000);
prntIt();
}
void loop() {
// if there's any serial available, read it:
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
int speed = Serial.parseInt();
speed = constrain(speed, 1000, 2000);
esc.writeMicroseconds(speed);
prntIt();
}
}
void prntIt()
{
Serial.print("microseconds = ");
Serial.println(speed);
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
const uint8_t pinRC1 = 9; //OC1A output
const uint8_t pinRC2 = 10; //OC1B output
void setup()
{
TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11); //COM1x clear on match, set on bottom
TCCR1B = _BV(WGM13) | _BV(WGM12)| _BV(CS11); //WGM=14, /8 prescaler
ICR1 = 39999; //20mS period for timer 1
OCR1A = 2999; //1.5mS pulse on pin 9
OCR1B = 3999; //2mS pulse on pin 10
pinMode( pinRC1, OUTPUT );
pinMode( pinRC2, OUTPUT );
}//setup
void loop()
{
}//loop
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////