I got it to work nicely on a Mega, but the mega would freeze at times. I need to make it work on a Due because of all the other tasks I am gonna lay on it.
When I compile, I get:
/Users/mitchanderson/Documents/Arduino/libraries/PinChangeInterrupt/src/PinChangeInterrupt.h:30:20: fatal error: avr/io.h: No such file or directory
#include <avr/io.h>
I could not find this file. Please help, I think it has to do with the pin change interrupt. Beyond me...
Here is my sketch
/*
Arduino Mega: 10, 11, 12, 13, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64),
A11 (65), A12 (66), A13 (67), A14 (68), A15 (69)
These are the pins for Measuring remote PWM with the remote.
*/
//Include the necessary libraries.
#include <PulseInput.h>
#include <Average.h>
#include <Wire.h>
volatile unsigned int ELEV_VAL, AILE_VAL, THRO_VAL, RUDD_VAL, GEAR_VAL, MIX_VAL, AUX2_VAL;
Average<float> AVG_MarchForceR(200);
Average<float> AVG_MarchForceL(200);
//Propulsion Motor section
int EN_A = 2; // enable output motor A -
int MarchFwdR = 3; // Pin for right motor Forward move PWM INVERTED-// it means that 0 is max speed and 255 no current
int DIS_A = 4;
int MarchBckR = 11; // Pin for motor BACK move PWM
int EN_B = 8; //Left motor
int MarchFwdL = 6 ; // Pin for right motor Forward move PWM // has been moved by jumper
int DIS_B = 7;
int MarchBckL = 12; // Pin for right motor BACK move PWM
bool EveryOther=false;
// Ribbon Bus pins
const int TurnRight = 24;
const int TurnLeft = 25;
const int DancePin = 26;
const int OnBk = 27; //switch on Back Relay 2 Button 2 Green
const int Program2 = 28;
const int OnFwd = 29; //switch on forward both tracks at the same speed (it has switch on PCB for testing.)Relay 1 button 1 -Blue wire
const int PixyPin = 30;
const int SleepPin = 31;
const int NodYesPin = 32;
const int ISeeYouPin = 33;
char ToSend[22];// = "Hello"; //String data
// Communication to the Balance processor
const int StaticPin = 14; // When WallE is not moving is high and moving is LOW.
///const int MoveBckPin =20; // PushBackpin, Goes high is wally is stationary and is pushed back.
///const int MoveFwdPin =19; // PushFwdpin, Goes high is wally is stationary and is pushed back.
// Left motor is Yaw of the cannon
int MaxSpeed_Lim = 150;
int SteerDivider = 20;
char Mode = 'P'; // P for park D drive, R for reverse r for turn right and l for turn leftx2
char Mode_Prev = 'x'; // P for park D drive, R for reverse r for turn right and l for turn left
int MarchReading;
int MarchForceR;
int MarchForceL;
int SteerReading; // left - right reading on the joystick // has been moved by jumper
int SteerForce;
int ThePitch;
int MarchReadingAbsolute;
int SteerReadingAbsolute;
int PitchReading;
int i; // integer for timer
unsigned long SpeedTimer = 0;
unsigned long AvgTimer = 0;
long encoderRValue = 0; // 73 pulses (CHANGE MODE)= full track cover =80cm
long encoderLValue = 0; // 73 pulses (CHANGE MODE)= full track cover =80cm
long encoderRValue_Prev = 0; // 73 pulses (CHANGE MODE)= full track cover =80cm
long encoderLValue_Prev = 0; // 73 pulses (CHANGE MODE)= full track cover =80cm
float Indicated_Speed_R = 0; // Speed in Cm per sec
float Indicated_Speed_L = 0; // Speed in Cm per sec
void(* resetFunc) (void) = 0; // must stay here
void setup() {
Serial.begin(115200); // Speed must stay at 115200 Not lower or higher to work with the USB shield
Serial3.begin(115200); // second serial that receives ThePitch from The tilt arduino
Serial.println (Serial3.availableForWrite());
Serial.print(F("WALLE_TRACKPOWER_11"));
digitalWrite (EN_B, 1); // writing a High will enable the motors
digitalWrite (EN_A, 1);
pinMode(24, INPUT_PULLUP); // all ribbon inputs are on digital pullup
pinMode(25, INPUT_PULLUP);
pinMode(26, INPUT_PULLUP);
pinMode(27, INPUT_PULLUP);
pinMode(28, INPUT_PULLUP);
pinMode(29, INPUT_PULLUP);
pinMode(30, INPUT_PULLUP);
pinMode(31, INPUT_PULLUP);
pinMode(32, INPUT_PULLUP);
pinMode(33, OUTPUT);
pinMode(StaticPin, OUTPUT);
// pinMode(MoveFwdPin,INPUT_PULLUP);
// pinMode(MoveBckPin,INPUT_PULLUP);
attachPulseInput(69, ELEV_VAL); // A15 PIN NO ON MEGA
attachPulseInput(68, AILE_VAL); // A14
attachPulseInput(67, THRO_VAL); // A13
attachPulseInput(66, RUDD_VAL); // A12
attachPulseInput(65, GEAR_VAL); // A11
attachPulseInput(64, MIX_VAL); // A10
// attachPulseInput(63, AUX2_VAL); // A9
delay (2000);
if (THRO_VAL == 0) {
Serial.println(F(" NO RADIO"));
//while(1); //halt
delay (2000);
resetFunc();
}
pinMode(19, INPUT);
attachInterrupt(digitalPinToInterrupt(19), countR, CHANGE);//interrupt
pinMode(18, INPUT);
attachInterrupt(digitalPinToInterrupt(18), countL, CHANGE);//interrupt
}
void loop()
{
/*
int RightVal = digitalRead (TurnRight); // Pin 24
int LeftVal = digitalRead (TurnLeft); // Pin 25
int DanceVal = digitalRead (DancePin); // Pin 26
int BackVal = digitalRead (OnBk); // Pin 27
int Prog2Val = digitalRead (Program2); // Pin 28
int ForeVal = digitalRead (OnFwd); //Pin 29
int PixyVal = digitalRead (PixyPin); // Pin 30
int SleepVal = digitalRead (SleepPin); // Pin 31
int NodYesVal = digitalRead (NodYesPin); // Pin 32
// int MoveFwdVal= digitalRead (MoveFwdPin);
// int MoveBckVal= digitalRead (MoveBckPin);
*/
// determine Mode
if (Mode != Mode_Prev)
{ Serial.println ("MODE: " + String(Mode));
Mode_Prev = Mode;
}
if (millis() >= SpeedTimer + 500) // calculating speed
{ SpeedTimer = millis();
long StartCountR = encoderRValue - encoderRValue_Prev;
Indicated_Speed_R = StartCountR * 2;
long StartCountL = encoderLValue - encoderLValue_Prev;
Indicated_Speed_L = StartCountL * 2;
// char ToSend[];
//itoa(THRO_VAL, ToSend, 4);
//Serial.println ("Speed_R= " + String (Indicated_Speed_R) + " Speed_L= " + String (Indicated_Speed_L) + " CM/SEC");
// sprintf_P(ToSend, (PGM_P)F("%04d, %04d, %02d"), THRO_VAL, RUDD_VAL, '\n');
encoderRValue_Prev = encoderRValue;
encoderLValue_Prev = encoderLValue;
// Serial.println ("LEFT_ENC= " + String(encoderLValue));
// Serial.println ("Right ENC= " + String(encoderRValue));
}
//TurnLeftFunction();
MarchReading = ELEV_VAL - 1500;
SteerReading = AILE_VAL - 1500;
// left - right reading on the joystick // has been moved by jumper
//Serial.println (String(AUX1_VAL)+ " "+ String(GEAR_VAL));
//Serial.println (String(MarchReading)+ " "+ String(SteerReading));
//Serial.println (String(ELEV_VAL)+ " "+ String(AILE_VAL)+" "+ String(THRO_VAL)+ " "+ String(RUDD_VAL)+" GEAR:"+ String(GEAR_VAL)+ " MIX:"+ String(MIX_VAL)); //+ " AUX2:"+ String(AUX2_VAL));
//delay (10);
// Serial.println (SteerReading);
MarchForceR = (265 - abs(MarchReading / 4.00));
MarchForceR = MarchForceR - (SteerReading / SteerDivider);
MarchForceR = constrain(MarchForceR, MaxSpeed_Lim, 255);
MarchForceL = (265 - abs(MarchReading / 4.00));
MarchForceL = MarchForceL + (SteerReading / SteerDivider);
MarchForceL = constrain(MarchForceL, MaxSpeed_Lim, 255);
if (millis() >= AvgTimer + 5)
{ AvgTimer = millis();
AVG_MarchForceR.push(MarchForceR);
AVG_MarchForceL.push(MarchForceL);
EveryOther= !EveryOther;
if (EveryOther)
{ sprintf_P(ToSend, PSTR("%04d, %04d, %02d\n"), THRO_VAL, RUDD_VAL, '\n');
Serial.println (ToSend);
Serial3.write (ToSend);
}
//Serial.println (String(MarchReading)+ " "+ String(SteerReading));
if (AVG_MarchForceR.mean() != 255)
{ //Serial.print(MarchForceR);
//Serial.print(" ");
//Serial.println(AVG_MarchForceR.mean());
//Serial.println (String(ELEV_VAL)+ " "+ String(AILE_VAL)+" "+ String(THRO_VAL)+ " "+ String(RUDD_VAL)+" GEAR:"+ String(GEAR_VAL)+ " MIX:"+ String(MIX_VAL));
}
}
if (MarchReading > 20 and SteerReading < abs(20) and Mode == 'P') // drive FWD
{ Mode = 'D';
}
else if (MarchReading < -20 and SteerReading < abs(20) and Mode == 'P') // REVERSE
{ Mode = 'R';
}
else if (MarchReading >= -20 and MarchReading <= 20 and SteerReading > 20 and Mode == 'P')// Turn LEFT
{ Mode = 'l';
}
else if (MarchReading >= -20 and MarchReading <= 20 and SteerReading < -20 and Mode == 'P')// Static turn Right
{ Mode = 'r';
}
else if ( abs (MarchReading) <= 20 and abs(SteerReading) < 25 and Indicated_Speed_R == 0 and Indicated_Speed_L == 0) //
{ Mode = 'P'; //PARK
}
//Serial.println (encoderRValue); // String(encoderValue));
if (Mode == 'D') //marching Forward
{
analogWrite (MarchFwdR, AVG_MarchForceR.mean()); // - (SteerReading / SteerDivider)); //PWM is written Here In reverse (255 is no speed, and 0 is max speed)
analogWrite (MarchFwdL, AVG_MarchForceL.mean()); // + (SteerReading / SteerDivider)); //PWM is written Here In reverse (255 is no speed, and 0 is max speed)
}
//
else if (Mode == 'R') // negative numbers for marching back
{
analogWrite (MarchBckR, AVG_MarchForceR.mean()); //- (SteerReading / SteerDivider)); //PWM is written Here In reverse (255 is no speed, and 0 is max speed)
analogWrite (MarchBckL, AVG_MarchForceL.mean()); // + (SteerReading / SteerDivider)); //PWM is written Here In reverse (255 is no speed, and 0 is max speed)
//Serial.println(MarchForceR);
}
////////////STATIC TURNS
else if (Mode == 'l')// Static turn right
{ digitalWrite (StaticPin, LOW);
MarchForceR = 265 - (SteerReading / 6);
MarchForceR = constrain(MarchForceR, MaxSpeed_Lim, 255);
//Serial.println (MarchForceR);
analogWrite (MarchFwdR, MarchForceR); //PWM is written Here In reverse (255 is no speed, and 0 is max speed)
analogWrite (MarchBckL, MarchForceR); //PWM is written Here In reverse (255 is no speed, and 0 is max speed)
}
else if (Mode == 'r')// Static turn left
{ digitalWrite (StaticPin, LOW);
MarchForceR = 265 - abs(SteerReading / 6);
MarchForceR = constrain(MarchForceR, MaxSpeed_Lim, 255);
//Serial.println (MarchForceR);
analogWrite (MarchFwdL, MarchForceR); //PWM is written Here In reverse (255 is no speed, and 0 is max speed)
analogWrite (MarchBckR, MarchForceR); //PWM is written Here In reverse (255 is no speed, and 0 is max speed)
}
else if (Mode == 'P') {
digitalWrite (StaticPin, HIGH);
EverybodyStop();
}
// Marching ();
} // end loop
void Marching ()
{
}
void PiruetteFunction()
{ delay (500);
for (int fadeValue = 255 ; fadeValue >= 220; fadeValue -= 1) {
analogWrite(MarchFwdL, fadeValue);
analogWrite(MarchBckR, fadeValue);
delay(100);
}
analogWrite(MarchFwdL, 220);
analogWrite(MarchBckR, 220);
delay(2255);
for (int fadeValue = 220 ; fadeValue <= 255; fadeValue += 1) {
analogWrite(MarchFwdL, fadeValue);
analogWrite(MarchBckR, fadeValue);
delay(100);
}
EverybodyStop();
delay(400);
}
void TurnLeftFunction()
{
delay (500);
for (int fadeValue = 255 ; fadeValue >= 220; fadeValue -= 1) {
analogWrite(MarchFwdR, fadeValue);
analogWrite(MarchBckL, fadeValue);
delay(55);
}
analogWrite(MarchFwdR, 220);
analogWrite(MarchBckL, 220);
delay(555);
for (int fadeValue = 220 ; fadeValue <= 255; fadeValue += 1) {
analogWrite(MarchFwdR, fadeValue);
analogWrite(MarchBckL, fadeValue);
delay(55);
}
EverybodyStop();
delay(400);
}
void TurnRightFunction ()
{ delay (500);
for (int fadeValue = 255 ; fadeValue >= 220; fadeValue -= 1) {
analogWrite(MarchFwdL, fadeValue);
analogWrite(MarchBckR, fadeValue);
delay(50);
}
analogWrite(MarchFwdL, 220);
analogWrite(MarchBckR, 220);
delay(55);
for (int fadeValue = 220 ; fadeValue <= 255; fadeValue += 1) {
analogWrite(MarchFwdL, fadeValue);
analogWrite(MarchBckR, fadeValue);
delay(50);
}
EverybodyStop();
delay(400);
}
void DanceFunction ()
{ //Turn Right
for (int fadeValue = 220 ; fadeValue >= 255; fadeValue -= 1) {
analogWrite(MarchFwdL, fadeValue);
analogWrite(MarchBckR, fadeValue);
delay(22);
}
for (int fadeValue = 220 ; fadeValue <= 255; fadeValue += 1) {
analogWrite(MarchFwdL, fadeValue);
analogWrite(MarchBckR, fadeValue);
delay(22);
}
EverybodyStop();
//Turn Left
for (int fadeValue = 220 ; fadeValue >= 255; fadeValue -= 1) {
analogWrite(MarchFwdR, fadeValue);
analogWrite(MarchBckL, fadeValue);
delay(22);
}
for (int fadeValue = 220 ; fadeValue <= 255; fadeValue += 1) {
analogWrite(MarchFwdR, fadeValue);
analogWrite(MarchBckL, fadeValue);
delay(22);
}
EverybodyStop();
//Turn complete Piruette
for (int fadeValue = 220 ; fadeValue >= 255; fadeValue -= 1) {
analogWrite(MarchFwdL, fadeValue);
analogWrite(MarchBckR, fadeValue);
delay(55);
}
analogWrite(MarchFwdL, 220);
analogWrite(MarchBckR, 220);
delay(4000);
for (int fadeValue = 220 ; fadeValue <= 255; fadeValue += 1) {
analogWrite(MarchFwdL, fadeValue);
analogWrite(MarchBckR, fadeValue);
delay(22);
}
EverybodyStop();
// turn Left track only FWD
for (int fadeValue = 210 ; fadeValue >= 255; fadeValue -= 1) {
analogWrite(MarchFwdL, fadeValue);
// analogWrite(MarchBckR, fadeValue);
delay(33);
}
analogWrite(MarchFwdL, 210);
// analogWrite(MarchBckR, 210);
delay(5000);
for (int fadeValue = 210 ; fadeValue <= 255; fadeValue += 1) {
analogWrite(MarchFwdL, fadeValue);
// analogWrite(MarchBckR, fadeValue);
delay(15);
}
EverybodyStop();
// turn Right track only FWD
for (int fadeValue = 210 ; fadeValue >= 255; fadeValue -= 1) {
// analogWrite(MarchFwdL, fadeValue);
analogWrite(MarchFwdR, fadeValue);
delay(33);
}
// analogWrite(MarchFwdL, 210);
analogWrite(MarchFwdR, 210);
delay(4000);
for (int fadeValue = 210 ; fadeValue <= 255; fadeValue += 1) {
// analogWrite(MarchFwdL, fadeValue);
analogWrite(MarchFwdR, fadeValue);
delay(15);
}
EverybodyStop();
}
void StepBackFunction ()
{
for (int fadeValue = 255 ; fadeValue >= 220; fadeValue -= 1) {
analogWrite(MarchBckR, fadeValue);
analogWrite(MarchBckL, fadeValue);
delay(44);
}
for (int fadeValue = 220 ; fadeValue <= 255; fadeValue += 1) {
analogWrite(MarchBckR, fadeValue);
analogWrite(MarchBckL, fadeValue);
delay(44);
}
EverybodyStop();
delay (2900);
}
void StepBackFunctionShort ()
{
for (int fadeValue = 255 ; fadeValue >= 230; fadeValue -= 1) {
analogWrite(MarchBckR, fadeValue);
analogWrite(MarchBckL, fadeValue);
delay(33);
}
for (int fadeValue = 230 ; fadeValue <= 255; fadeValue += 1) {
analogWrite(MarchBckR, fadeValue);
analogWrite(MarchBckL, fadeValue);
delay(33);
}
EverybodyStop();
delay (1000);
}
void StepAheadFunction ()
{
for (int fadeValue = 255 ; fadeValue >= 220; fadeValue -= 1) {
analogWrite(MarchFwdR, fadeValue);
analogWrite(MarchFwdL, fadeValue);
delay(55);
}
analogWrite(MarchFwdR, 220);
analogWrite(MarchFwdL, 220);
delay(300);
for (int fadeValue = 220 ; fadeValue <= 255; fadeValue += 1) {
analogWrite(MarchFwdR, fadeValue);
analogWrite(MarchFwdL, fadeValue);
delay(55);
}
EverybodyStop();
delay (2800);
}
void StaticTurns ()
{ if (SteerReading < -10)
{
analogWrite (MarchFwdR, 200); //PWM is written Here In reverse (255 is no speed, and 0 is max speed)
digitalWrite (MarchBckR, HIGH);
delay (1000);
analogWrite (MarchFwdL, HIGH); //PWM is written Here In reverse (255 is no speed, and 0 is max speed)
digitalWrite (MarchBckL, 200);
}
else if (SteerReading > 10)
{
analogWrite (MarchBckR, 200); //PWM is written Here In reverse (255 is no speed, and 0 is max speed)
digitalWrite (MarchFwdR, HIGH);
analogWrite (MarchBckL, HIGH); //PWM is written Here In reverse (255 is no speed, and 0 is max speed)
digitalWrite (MarchFwdL, 200);
}
else
{
EverybodyStop();
}
}
void EverybodyStop()
{
digitalWrite (MarchFwdR, HIGH); //
digitalWrite (MarchBckR, HIGH);
digitalWrite (MarchFwdL, HIGH); //
digitalWrite (MarchBckL, HIGH);
}
void countR()
{
encoderRValue++;
}
void countL()
{
encoderLValue++;
}