I know that this is the wrong site because I have a chipkit max32 but I need help with general programming code. I am really new to programming and I need lots of help. This code that I have is borrowed some several different codes that I have found trying to read an RC receiver’s output. I have two versions of this code, one uses pulseIn() to read the signal, the other uses an interrupt. I tried to use interrupts, but from what I read they don’t yet work with the ide. I am trying to read the first three channels of the receiver and the issue that I am having is that the it is almost hit and miss when using the pulseIn() command. I use the serial monitor to view the output of one of the pulseIn’s and it will read it the first time through the sketch but after that it seams random when it reads it. I know the receiver works because I was able to read it well using an arduino duemilanove before I shorted out the power side of the ardunio. The normal output that I was able to read with the arduino was between 1-2 ms, but the random readings that I am getting withe the max32 is between 3.5 -4.5 ms. I’m really sorry if I have greatly confused you, or by how messy the code looks. I am learning as I go. Any help you can give is very much appreciated.
unsigned long counter = 0;
unsigned long Channel1Value;
unsigned long Channel2Value;
unsigned long lastgood1;
unsigned long lastgood2;
unsigned long InitialSteer;
unsigned long InitialThrottle;
unsigned int MotorLimit;
int Channel3Value;
boolean Right;
//motor stuff
int Steer;
int Thrust;
int RightMotor;
int LeftMotor;
int Chan1 = 9;
int Chan2 = 10;
int Chan3 = 11;
int ledpin = 13;
#include <SoftwareSerial.h>
#define rxPin 3 // pin 3 connects to SMC TX (not used in this example)
#define txPin 4 // pin 4 connects to SMC RX
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
/**************************************************************
* Subroutine to exit saft start;
***************************************************************/
void exitSafeStartBoth()
{
mySerial.print(0x83, BYTE);
}
/**************************************************************
* Subroutine to control right motor= motorRight(speed, direction);
***************************************************************/
void setMotorSpeedleft(int speedleft)
{
if (speedleft < 0)
{
mySerial.print(0xAA, BYTE);
mySerial.print(0xd, BYTE);
mySerial.print(0x6, BYTE); // motor reverse command
speedleft = -speedleft; // make speed positive
}
else
{
mySerial.print(0xAA, BYTE);
mySerial.print(0xd, BYTE);
mySerial.print(0x5, BYTE); // motor forward command
}
mySerial.print((unsigned char)(speedleft & 0x1F), BYTE);
mySerial.print((unsigned char)(speedleft >> 5), BYTE);
}
/**************************************************************
* Subroutine to control left motor
***************************************************************/
void setMotorSpeedright(int speedright)
{
if (speedright < 0)
{
mySerial.print(0xAA, BYTE);
mySerial.print(0xa, BYTE);
mySerial.print(0x06, BYTE); // motor reverse command
speedright = -speedright; // make speed positive
}
else
{
mySerial.print(0xAA, BYTE);
mySerial.print(0xa, BYTE);
mySerial.print(0x05, BYTE); // motor forward command
}
mySerial.print((unsigned char)(speedright & 0x1F), BYTE);
mySerial.print((unsigned char)(speedright >> 5), BYTE);
}
void setup()
{
Serial.begin(19200);
Serial.println("Ready");
pinMode (Chan1, INPUT); // connect Rx channel 1 to PD3, which is labled "D3" on the Arduino board
pinMode (Chan2, INPUT); // connect Rx channel 2 to PD4, which is labled "D4" on the Arduino board
pinMode (Chan3, INPUT); // connect Rx channel 3 to PD5, which is labled "D5" on the Arduino board
pinMode (13, OUTPUT) ;
InitialSteer = pulseIn (Chan1, HIGH); //read RC channel 1
lastgood1 = InitialSteer;
InitialThrottle = pulseIn (Chan2, HIGH); //read RC channel 2
lastgood2 = InitialThrottle;
}
void loop()
{
if (InitialSteer < 100 ||InitialThrottle < 100 || Channel3Value < 100)
{
mySerial.print(0xE0, BYTE);
digitalWrite(ledpin, LOW);
}
else {
digitalWrite(ledpin, HIGH);
// counter++; // this just increments a counter for benchmarking the impact of the pulseIn's on CPU performance
Channel3Value = pulseIn(Chan3, HIGH, 20000); // read RC channel 3
MotorLimit = map(Channel3Value, 1100, 1840, 0, 3200); // map to 0-3200
constrain (MotorLimit, 0, 3200);
Channel1Value = pulseIn (Chan1, HIGH, 20000); //read RC channel 1
if (Channel1Value == 0) {
Channel1Value = lastgood1;
}
else {
lastgood1 = Channel1Value;
}
if (Channel1Value < InitialSteer) {
Right = false;
}
else {
Right = true;
}
if (Right) {
Steer = Channel1Value - InitialSteer;
}
else {
Steer = InitialSteer - Channel1Value;
}
Steer = map(Channel1Value, 1100, 1840, -MotorLimit, MotorLimit); // convert to -3200 to 3200
constrain (Steer, -MotorLimit, MotorLimit); //just in case
Channel2Value = pulseIn (Chan2, HIGH, 20000); //read RC channel 2
Thrust = Channel2Value - InitialThrottle;
Thrust = map(Channel1Value, 1100, 1840, -MotorLimit, MotorLimit); // convert to 0-100 range
constrain (Thrust, -MotorLimit, MotorLimit); //just in case
Serial.print (Channel1Value); // if you turn on your serial monitor you can see the readings.
Serial.print (" ");
Serial.print (Channel2Value);
Serial.println("");
// Serial.println (counter); uncomment if you're benchmarking
Serial.print (Steer); // displays values on the serial monitor if you want to see them
Serial.print (" ");
Serial.print (Thrust);
Serial.println("");
Serial.print("Initial Throttle: ");
Serial.print (InitialThrottle);
Serial.println("");
Serial.print("Channel 2: ");
Serial.print (Channel2Value);
Serial.println("");
Serial.print ("Initial Steer: ");
Serial.print (InitialSteer);
Serial.println("");
Serial.print ("Channel 1: ");
Serial.print (Channel1Value);
Serial.println("");
// Here we combine the direction and thrust into commands for the two motors. We never turn the motors backwards,
// because that would mess up the operation of the vertical vectoring thrusters.
if (Right == true) // turn right
{
RightMotor = Thrust - Steer; // reduce power to the motor in the direction you want to go
// if (RightMotor < 0) RightMotor = 0; //mike changed this so that they will run backwards
LeftMotor = Thrust + Steer; // increase power to the motor opposite the direction you want to go
if (LeftMotor > 3200) LeftMotor = 3200;
}
if (Right == false) //turn left
{
LeftMotor = Thrust - Steer; // reduce power to the motor in the direction you want to go
// if (LeftMotor < 0) LeftMotor = 0; //mike changed this so that they will run backwards
RightMotor = Thrust + Steer; // increase power to the motor opposite the direction you want to go
if (RightMotor > 3200) RightMotor = 3200;
}
setMotorSpeedleft(LeftMotor);
setMotorSpeedright(RightMotor);
delay(50);
}
}
I changed the code to this to remove anything i thought might be affecting the return.
unsigned long Channel1Value;
unsigned long Channel2Value;
long Channel3Value;
unsigned long InitialSteer;
unsigned long InitialThrottle;
int Chan1 = 9;
int Chan2 = 10;
int Chan3 = 11;
void setup()
{
Serial.begin(19200);
pinMode (Chan1, INPUT); // connect Rx channel 1 to PD3, which is labled "D3" on the Arduino board
pinMode (Chan2, INPUT); // connect Rx channel 2 to PD4, which is labled "D4" on the Arduino board
pinMode (Chan3, INPUT); // connect Rx channel 3 to PD5, which is labled "D5" on the Arduino board
InitialSteer = pulseIn (Chan1, HIGH); //read RC channel 1
InitialThrottle = pulseIn (Chan2, HIGH); //read RC channel 2
}
void loop()
{
Channel3Value = pulseIn(Chan3, HIGH, 20000); // read RC channel 3
Channel1Value = pulseIn (Chan1, HIGH, 20000); //read RC channel 1
Channel2Value = pulseIn (Chan2, HIGH, 20000); //read RC channel 2
delay(50);
}
but it still gives random readings.