switch to setup for Arduino Boards Manager

This commit is contained in:
Erik Tylek Kettenburg
2015-06-23 12:42:35 -07:00
parent bc55c9bb45
commit 6ca6b114d5
3581 changed files with 93 additions and 51 deletions

View File

@@ -0,0 +1,39 @@
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
// Adapted to SoftRcPulseOut library by RC Navy (http://p.loussouarn.free.fr)
// This sketch can work with ATtiny and Arduino UNO, MEGA, etc...
#include <SoftRcPulseOut.h>
SoftRcPulseOut myservo; // create servo object to control a servo
#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
//Here is the POT_PIN definition for ATtiny, they do NOT need a 'A' prefix for Analogic definition
#define POT_PIN 2 // --analog pin-- (not digital) used to connect the potentiometer
#else
//Here is the POT_PIN definition for Arduino UNO, MEGA, they do need a 'A' prefix for Analogic definition
#define POT_PIN A2 // --analog pin-- (not digital) used to connect the potentiometer
#endif
#define SERVO_PIN 3 // --digital pin-- (not analog) used to connect the servo
#define REFRESH_PERIOD_MS 20
#define NOW 1
int val; // variable to read the value from the analog pin
void setup()
{
myservo.attach(SERVO_PIN); // attaches the servo on pin defined by SERVO_PIN to the servo object
}
void loop()
{
val = analogRead(POT_PIN); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(REFRESH_PERIOD_MS); // waits for the servo to get there
SoftRcPulseOut::refresh(NOW); // generates the servo pulse Now
}

View File

@@ -0,0 +1,60 @@
// This SoftwareServo library example sketch was initially delivered without any comments.
// Below my own comments for SoftRcPulseOut library: by RC Navy (http://p.loussouarn.free.fr)
// Controlling the position of 2 servos using the Arduino built-in hardware UART (Arduino Serial object).
// This sketch do NOT work with an ATtinyX4 and ATtinyX5 since they do not have a built-in harware UART (no Arduino Serial object).
// The command (issued in the Arduino Serial Console or in a Terminal) is:
// S=P with:
// S=A for Servo1 and S=B for Servo2
// P=Position number x 20° (Possible positions are from 0 to 9 which correspond to from 0° to 180°)
// Ex:
// A=7 sets Servo1 at 7 x 20 =140°
// B=3 sets Servo2 at 3 x 20 =60°
#include <SoftRcPulseOut.h>
SoftRcPulseOut servo1;
SoftRcPulseOut servo2;
void setup()
{
pinMode(13,OUTPUT);
servo1.attach(2);
servo1.setMaximumPulse(2200);
servo2.attach(4);
servo2.setMaximumPulse(2200);
Serial.begin(9600);
Serial.print("Ready");
}
void loop()
{
static int value = 0;
static char CurrentServo = 0;
if ( Serial.available()) {
char ch = Serial.read();
switch(ch) {
case 'A':
CurrentServo='A';
digitalWrite(13,LOW);
break;
case 'B':
CurrentServo='B';
digitalWrite(13,HIGH);
break;
case '0' ... '9':
value=(ch-'0')*20;
if (CurrentServo=='A')
{
servo1.write(value);
}
else if (CurrentServo=='B')
{
servo2.write(value);
}
break;
}
}
SoftRcPulseOut::refresh();
}

View File

@@ -0,0 +1,100 @@
/*
This sketch demonstrates how to use <SoftRcPulseIn> library to get RC pulses from a receiver and to use <SoftRcPulseOut> library to drive 2 servos.
The first servo will follow the order, and the second one will have a reverted motion.
Please notice this sketch is fully asynchronous: no blocking functions such as delay() or pulseIn() are used.
Tested on arduino UNO, ATtiny84, ATtiny85 and Digispark rev2 (Model A).
RC Navy 2013
http://p.loussouarn.free.fr
*/
#include <SoftRcPulseIn.h>
#include <SoftRcPulseOut.h>
#include <TinyPinChange.h> /* Needed for <SoftRcPulseIn> library */
#define RX_CHANNEL_PIN 2
#define SERVO1_PIN 3
#define SERVO2_PIN 4
#define LED_PIN 1//1 on Digispark rev2 (Model A), change to pin 0 for Digispark rev1 (Model B), change to 13 for UNO
#define LED_HALF_PERIOD_MS 250
#define PULSE_MAX_PERIOD_MS 30 /* To refresh the servo in case of pulse extinction */
#define NOW 1
#define NEUTRAL_US 1500 /* Default position in case of no pulse at startup */
enum {NORMAL=0, INVERTED, SERVO_NB}; /* Trick: use an enumeration to declare the index of the servos AND the amount of servos */
SoftRcPulseIn RxChannelPulse; /* RxChannelPulse is an objet of SoftRcPulseIn type */
SoftRcPulseOut ServoMotor[SERVO_NB]; /* Table Creation for 2 objets of SoftRcPulseOut type */
/* Possible values to compute a shifting average fin order to smooth the recieved pulse witdh */
#define AVG_WITH_1_VALUE 0
#define AVG_WITH_2_VALUES 1
#define AVG_WITH_4_VALUES 2
#define AVG_WITH_8_VALUES 3
#define AVG_WITH_16_VALUES 4
#define AVERAGE_LEVEL AVG_WITH_4_VALUES /* Choose here the average level among the above listed values */
/* Higher is the average level, more the system is stable (jitter suppression), but lesser is the reaction */
/* Macro for average */
#define AVERAGE(ValueToAverage,LastReceivedValue,AverageLevelInPowerOf2) ValueToAverage=(((ValueToAverage)*((1<<(AverageLevelInPowerOf2))-1)+(LastReceivedValue))/(1<<(AverageLevelInPowerOf2)))
/* Variables */
uint32_t LedStartMs=millis();
uint32_t RxPulseStartMs=millis();
boolean LedState=HIGH;
void setup()
{
#if !defined(__AVR_ATtiny24__) && !defined(__AVR_ATtiny44__) && !defined(__AVR_ATtiny84__) && !defined(__AVR_ATtiny25__) && !defined(__AVR_ATtiny45__) && !defined(__AVR_ATtiny85__) && !defined(__AVR_ATtiny167__)
Serial.begin(9600);
Serial.print("SoftRcPulseIn library V");Serial.print(SoftRcPulseIn::LibTextVersionRevision());Serial.print(" demo"); /* For arduino UNO which has an hardware UART, display the library version in the console */
#endif
RxChannelPulse.attach(RX_CHANNEL_PIN);
ServoMotor[NORMAL].attach(SERVO1_PIN); /* enumeration is used a index for the ServoMotor[] table */
ServoMotor[INVERTED].attach(SERVO2_PIN); /* enumeration is used a index for the ServoMotor[]table */
pinMode(LED_PIN, OUTPUT);
}
void loop()
{
static uint16_t Width_us=NEUTRAL_US; /* Static to keep the value at the next loop */
/* Receiver pulse acquisition and command of 2 servos, one in the direct direction, one in the inverted direction */
if(RxChannelPulse.available())
{
AVERAGE(Width_us,RxChannelPulse.width_us(),AVERAGE_LEVEL);
ServoMotor[NORMAL].write_us(Width_us); /* Direct Signal */
ServoMotor[INVERTED].write_us((NEUTRAL_US*2)-Width_us); /* Inverted Signal */
SoftRcPulseOut::refresh(NOW); /* NOW argument (=1) allows to synchronize outgoing pulses with incoming pulses */
RxPulseStartMs=millis(); /* Restart the Chrono for Pulse */
#if !defined(__AVR_ATtiny24__) && !defined(__AVR_ATtiny44__) && !defined(__AVR_ATtiny84__) && !defined(__AVR_ATtiny25__) && !defined(__AVR_ATtiny45__) && !defined(__AVR_ATtiny85__) && !defined(__AVR_ATtiny167__)
Serial.print("Pulse=");Serial.println(Width_us); /* For arduino UNO which has an hardware UART, display the library version in the console */
#endif
}
else
{
/* Check for pulse extinction */
if(millis()-RxPulseStartMs>=PULSE_MAX_PERIOD_MS)
{
/* Refresh the servos with the last known position in order to avoid "flabby" servos */
SoftRcPulseOut::refresh(NOW); /* Immediate refresh of outgoing pulses */
RxPulseStartMs=millis(); /* Restart the Chrono for Pulse */
}
}
/* Blink LED Management */
if(millis()-LedStartMs>=LED_HALF_PERIOD_MS)
{
digitalWrite(LED_PIN, LedState);
LedState=!LedState; /* At the next loop, if the half period is elapsed, the LED state will be inverted */
LedStartMs=millis(); /* Restart the Chrono for the LED */
}
}

View File

@@ -0,0 +1,39 @@
// Sweep
// by BARRAGAN <http://barraganstudio.com>
// Adapted to SoftRcPulseOut library by RC Navy (http://p.loussouarn.free.fr)
// This sketch can work with ATtiny and Arduino UNO, MEGA, etc...
// This example code is in the public domain.
#include <SoftRcPulseOut.h>
SoftRcPulseOut myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
#define SERVO_PIN 3
#define REFRESH_PERIOD_MS 20
#define NOW 1
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(SERVO_PIN); // attaches the servo on pin defined by SERVO_PIN to the servo object
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(REFRESH_PERIOD_MS); // waits 20ms for refresh period
SoftRcPulseOut::refresh(NOW); // generates the servo pulse Now
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(REFRESH_PERIOD_MS); // waits 20ms for for refresh period
SoftRcPulseOut::refresh(NOW); // generates the servo pulse Now
}
}

View File

@@ -0,0 +1,37 @@
// SweepNoDelay
// by RC Navy (http://p.loussouarn.free.fr/arduino/arduino.html>)
// This sketch can work with ATtiny and Arduino UNO, MEGA, etc...
// This example code is in the public domain.
#include <SoftRcPulseOut.h>
SoftRcPulseOut myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
#define SERVO_PIN 0
#define UP_DIRECTION +1
#define DOWN_DIRECTION -1
#define POS_MIN 0
#define POS_MAX 180
int pos = POS_MIN; // variable to store the servo position
int step = UP_DIRECTION;
void setup()
{
myservo.attach(SERVO_PIN); // attaches the servo on pin defined by SERVO_PIN to the servo object
myservo.write(pos);
}
void loop()
{
if (SoftRcPulseOut::refresh()) // refresh() returns 1 every 20ms (after pulse update)
{
// We arrive here every 20ms
pos += step;
if(pos >= POS_MAX) step = DOWN_DIRECTION; //180 degrees reached -> Change direction
if(pos <= POS_MIN) step = UP_DIRECTION; // 0 degrees reached -> Change direction
myservo.write(pos);
}
}

View File

@@ -0,0 +1,73 @@
// This sketch demonstrates how to command 2 servos through the USB of the Digispark.
// It uses:
// - <SoftRcPulseOut> library to easily generates the RC pulses for the servos.
// - <DigiUSB> library to communicate with the PC
// By RC Navy (http://p.loussouarn.free.fr)
// The command (issued in the DigiUSB Monitor or the digiterm) is:
// S=P with:
// S=A for ServoA and S=B for ServoB
// P=Position number x 20° (Possible positions are from 0 to 9 which correspond to from 0° to 180°)
// Ex:
// A=7 sets Servo1 at 7 x 20 =140°
// B=3 sets Servo2 at 3 x 20 =60°
// Once the servo selected, just type the value between 0 and 9
// Please, note this sketch is derived from the SerialServo example of <SoftwareServo> library.
#include <DigiUSB.h>
#include <SoftRcPulseOut.h>
#define LED_PIN 1 /* Builtin Led on Rev2 ModelA Digispark */
#define SERVO_A_PIN 2
/* /!\ Do not use Pin 3 (used by USB) /!\ */
/* /!\ Do not use Pin 4 (used by USB) /!\ */
#define SERVO_B_PIN 5
SoftRcPulseOut ServoA;
SoftRcPulseOut ServoB;
void setup()
{
pinMode(LED_PIN,OUTPUT);
ServoA.attach(SERVO_A_PIN);
ServoB.attach(SERVO_B_PIN);
DigiUSB.begin();
DigiUSB.println(" Ready");
}
void loop()
{
static int value = 0;
static char CurrentServo = 0;
if ( DigiUSB.available()) {
char ch = DigiUSB.read();
switch(ch) {
case 'A':
CurrentServo='A';
digitalWrite(LED_PIN,LOW);
break;
case 'B':
CurrentServo='B';
digitalWrite(LED_PIN,HIGH);
break;
case '0' ... '9':
value=(ch-'0')*20;
if (CurrentServo=='A')
{
ServoA.write(value);
}
else if (CurrentServo=='B')
{
ServoB.write(value);
}
break;
}
}
DigiUSB.refresh();
SoftRcPulseOut::refresh();
/*
Put here your non-blocking code
*/
}

View File

@@ -0,0 +1,54 @@
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
// Adapted to SoftRcPulseOut library by RC Navy (http://p.loussouarn.free.fr)
// This sketch can work with ATtiny and Arduino UNO, MEGA, etc...
#include <SoftRcPulseOut.h>
SoftRcPulseOut myservo; // create servo object to control a servo
#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
//Here is the POT_PIN definition for ATtiny, they do NOT need a 'A' prefix for Analogic definition
#define POT_PIN 2 // --analog pin-- (not digital) used to connect the potentiometer
#else
//Here is the POT_PIN definition for Arduino UNO, MEGA, they do need a 'A' prefix for Analogic definition
#define POT_PIN A2 // --analog pin-- (not digital) used to connect the potentiometer
#endif
#define SERVO_PIN 3 // --digital pin-- (not analog) used to connect the servo
#define REFRESH_PERIOD_MS 20
#define NOW 1
#define MOY_SUR_1_VALEUR 0
#define MOY_SUR_2_VALEURS 1
#define MOY_SUR_4_VALEURS 2
#define MOY_SUR_8_VALEURS 3
#define MOY_SUR_16_VALEURS 4
#define MOY_SUR_32_VALEURS 5
#define TAUX_DE_MOYENNAGE MOY_SUR_4_VALEURS /* Choisir ici le taux de moyennage parmi les valeurs precedentes possibles listees ci-dessus */
/* Plus le taux est élevé, plus le système est stable (diminution de la gigue), mais moins il est réactif */
#define MOYENNE(Valeur_A_Moyenner,DerniereValeurRecue,TauxDeMoyEnPuissanceDeDeux) Valeur_A_Moyenner=((((Valeur_A_Moyenner)*((1<<(TauxDeMoyEnPuissanceDeDeux))-1)+(DerniereValeurRecue))/(1<<(TauxDeMoyEnPuissanceDeDeux)))+(TauxDeMoyEnPuissanceDeDeux-1))
int val; // variable to read the value from the analog pin
void setup()
{
myservo.attach(SERVO_PIN); // attaches the servo on pin defined by SERVO_PIN to the servo object
}
void loop()
{
static int ValMoyennee;
val = analogRead(POT_PIN); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
MOYENNE(ValMoyennee,val,TAUX_DE_MOYENNAGE);//If there is lots of noise: average with TAUX_DE_MOYENNAGE
myservo.write(ValMoyennee); // sets the servo position according to the scaled value
delay(REFRESH_PERIOD_MS); // waits for the servo to get there
SoftRcPulseOut::refresh(NOW); // generates the servo pulse
}