Im trying to test out these two motors I have where I need to call a function from the loop. I only want to call this function once so I made a quick little if statement to have the program only run once. However, it seems that when I run this file, the Variable I have set doesn't continue getting incremented. The code will tell more.
int i = 0;
void loop()
{
// Serial.print("=====");Serial.print(i);Serial.println("=====");
if(i == 1)
{
Serial.print("+++");Serial.print(i);Serial.print("+++");
DeclinationMotor(DEC_freq, NULL, HIGH);
}
i++;
}
The behavior is really odd in that if i just run that code above, the variable i will not get incremented and become stuck at 1. However if I were to un-comment the other serial print line above the if statement, i would be incremented properly and that function will be executed only once.
For reference, the function DeclinationMotor is
float DeclinationMotor(float freqspeed, float angularDistance, byte direction)
{
Serial.print(freqspeed); Serial.print("DEfS "); Serial.print(angularDistance); Serial.print("DEaD "); Serial.println(direction);
int finalLocation = 0;
if(freqspeed == NULL && angularDistance != NULL)
freqspeed = DEC_freq;
if(angularDistance != NULL)
{
if(direction == HIGH)
finalLocation = DEC_degrees_from_home + angularDistance;
else
finalLocation = DEC_degrees_from_home - angularDistance;
}
SetPinFrequency(DEC_STEP, freqspeed);
pwmWrite(DEC_STEP, 128);
digitalWrite(DEC_DIR, direction);
digitalWrite(DEC_running, HIGH);
return finalLocation;
}
Full Code Here
//-----------------------------------------------------------------------------
#include <PWM.h>
void analogJoyStick();
void serialEvent();
void meridianFlipT();
float RightAscensionMotor(float freqspeed, float angularDistance, byte direction);
float DeclinationMotor(float freqspeed, float angularDistance, byte direction);
void StopRAMotor();
void StopDECMotor();
// --------------Todo--------------
// Turn the Variables into motor actions for serial event
// Write program that helps figure out the encoders degrees for its
// angle crap. You might just need to count exactly how many
// times the revolution goes around manually
// set up isrs for optical sensors and joystick
// create stop motors function
//
#define RA_sensor 2
#define DEC_sensor 3
// Sensor limit LEDs
#define RA_limit 27
#define DEC_limit 29
// Telescope Position Status LEDs
#define home_pos 35
#define tracking 37
// Joystick
#define xPin A0 // X axis
#define yPin A1 // Y axis
#define JS_button 26 // Button
// Motor 1
#define RA_STEP 10 // Step for Right Ascension
#define RA_DIR 12 // Direction for Right Ascension
#define RA_EN 32 // Enable motor disconnection
#define RA_OUT 36 // Motor fault output
int RA_freq = 800; // Frequency for normal movement
int RA_freq_tracking = 400; // Frequency for tracking objects
int RA_freq_slow = 600; // Frequency for when close to desired position
volatile byte RA_track_DIR = LOW; // Used to store last direction of RA motor
// Encoder 1
#define RA_A 18
#define RA_B 19
#define RA_I 22
long int RA_Encoder_Count = 0; // Positive value means net position is in B direction
int RA_Complete_Rotations = 0; // Positive value means net complete rotation is in B direction
int RA_degrees_from_home = 0; // Used to keep track of how far from home position
// Motor 2
#define DEC_STEP 11 // Step for Declination
#define DEC_DIR 13 // Direction for Declination
#define DEC_EN 34 // Enable motor disconnection
#define DEC_OUT 38 // Motor fault output
int DEC_freq = 800; // Frequency for normal movement
int DEC_freq_slow = 600; // Frequency when close to desired position
volatile byte DEC_track_DIR = LOW; // Used to store last direction of DEC motor
// Encoder 2
#define DEC_A 20
#define DEC_B 21
#define DEC_I 24
long int DEC_Encoder_Count = 0; // Positive value means net position is in B direction
int DEC_Complete_Rotations = 0; // Positive value means net complete rotation is in B direction
int DEC_degrees_from_home = 0; // Used to keep track of how far from home position
// Communication with ATmega328
#define COMM 39
#define RA_running 23
#define DEC_running 25
int i = 0;
void setup()
{
// Sensor Inputs
pinMode(RA_sensor, INPUT);
pinMode(DEC_sensor, INPUT);
// Sensor Fault LEDs
pinMode(RA_limit, OUTPUT);
pinMode(DEC_limit, OUTPUT);
// Telescope Position Status LEDs
pinMode(home_pos, OUTPUT);
pinMode(tracking, OUTPUT);
// Interrupts for Sensors
// attachInterrupt(digitalPinToInterrupt(RA_sensor), RA_sensor_trip, CHANGE);
// attachInterrupt(digitalPinToInterrupt(DEC_sensor), DEC_sensor_trip, CHANGE);
// Right Ascension Motor Inputs and Outputs
pinMode(RA_STEP, OUTPUT);
pinMode(RA_DIR, OUTPUT);
pinMode(RA_OUT, INPUT);
// attachInterrupt(digitalPinToInterrupt(RA_OUT), RA_motor_fault, CHANGE);
// Right Ascension Encoder Inputs
pinMode(RA_A, INPUT);
// attachInterrupt(digitalPinToInterrupt(RA_A), RA_Enc, RISING);
pinMode(RA_B, INPUT);
pinMode(RA_I, INPUT);
// Declination Motor Inputs and Outputs
pinMode(DEC_STEP, OUTPUT);
pinMode(DEC_DIR, OUTPUT);
pinMode(DEC_OUT, INPUT);
// attachInterrupt(digitalPinToInterrupt(DEC_OUT), DEC_motor_fault, CHANGE);
// Declination Encoder Inputs
pinMode(DEC_A, INPUT);
// attachInterrupt(digitalPinToInterrupt(DEC_A), DEC_Enc, RISING);
pinMode(DEC_B, INPUT);
pinMode(DEC_I, INPUT);
// Joystick inputs
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(JS_button, INPUT);
// Communication with ATmega328
pinMode(COMM, OUTPUT);
pinMode(RA_running, OUTPUT);
pinMode(DEC_running, OUTPUT);
// Constant signal to ATmega328 to confirm ATmega2560 is operational
digitalWrite(COMM, HIGH);
Serial.begin(9600);
InitTimersSafe();
}
void loop()
{
// Serial.print("=====");Serial.print(i);Serial.println("=====");
if(i == 1)
{
Serial.print("+++");Serial.print(i);Serial.print("+++");
DeclinationMotor(RA_freq, NULL, HIGH);
}
i++;
}
void analogJoyStick()
{
int NumbX, NumbY;
while(digitalRead(JS_button) == LOW)
{
NumbX = analogRead(xPin);
NumbY = analogRead(yPin);
// Here we go to to move the motor and the numbers have been created with
// small numerical buffers to allow deadzones. It was made to be linear response
// to the users input.
if(NumbX > 530)
RightAscensionMotor((RA_freq * (NumbX - 512)/512), NULL, HIGH);
else if(NumbX < 500)
RightAscensionMotor((RA_freq * (NumbX - 512)/512), NULL, LOW);
else
StopRAMotor();
if(NumbY > 530)
DeclinationMotor(800, NULL, HIGH);
else if(NumbY < 500)
DeclinationMotor(800, NULL, LOW);
else
StopDECMotor();
delay(50);
}
// Track();
}
// this thing seems to be appended onto the end of the loop() so the immediate intersection must be
void serialEvent()
{
char ident = Serial.read(); // first character will be the identifying character
if(ident == 'R')
{
int number1 = Serial.parseInt(); // these variables should become global afterwards
Serial.read();
int number2 = Serial.parseInt();
// you don't know what origin is yet so..
}
else if(ident == 'M')
{
int raAngle = Serial.parseInt();
Serial.read(); // These serial reads are for the separaters in the transmitted string
int raDirection = Serial.parseInt();
Serial.read();
int dAngle = Serial.parseInt();
Serial.read();
int dDirection = Serial.parseInt();
RightAscensionMotor(RA_freq, raAngle, raDirection);
DeclinationMotor(DEC_freq, dAngle, dDirection);
}
// Serial.readbytes()
Serial.read(); // this is to read the newline character out of the buffer.
}
// This needs to be called if
// if(Osensor1 || Osensor2)
// meridianFlip();
void meridianFlipT()
{
float finalLocationRA, finalLocationD;
byte RACheck = LOW; byte DCheck = LOW;
Serial.println("F1"); // newline is already incorporated
// Right so, RA is normal yea whatever, but Declination is made this way to allow the counter weight
// to always be pointed away from the tripod. For the latitude is 0 at the pole.
finalLocationRA = RightAscensionMotor(RA_freq, 180, !RA_track_DIR);
finalLocationD = DeclinationMotor(DEC_freq, ((180 - DEC_degrees_from_home) * 2), !DEC_track_DIR);
// This is going to check to see if both the motors have turned to their respective locations theres
// a little buffer period that is inputted to just to see if the motor gets there. We can replaced this later on
while(RACheck == LOW || DCheck == LOW)
{
if(RACheck == LOW && (finalLocationRA + 2 > RA_degrees_from_home && finalLocationRA - 2 < RA_degrees_from_home))
{
digitalWrite(RA_running, LOW);
RACheck = HIGH;
}
if(DCheck == LOW && (finalLocationD + 2 > DEC_degrees_from_home && finalLocationD - 2 < DEC_degrees_from_home))
{
digitalWrite(DEC_running, LOW);
DCheck = HIGH;
}
delay(10);
}
//Track(); // Resume Tracking
Serial.println("F0");
}
// These two are just functions to move the motors with. Might have a
// return to tell that its finished or something
float RightAscensionMotor(float freqspeed, float angularDistance, byte direction)
{
Serial.print(freqspeed); Serial.print("RAfS "); Serial.print(angularDistance); Serial.print("RAaD "); Serial.println(direction);
int finalLocation = 0;
if(freqspeed == NULL && angularDistance != NULL)
freqspeed = RA_freq;
if(angularDistance != NULL)
{
if(direction == HIGH)
finalLocation = RA_degrees_from_home + angularDistance;
else
finalLocation = RA_degrees_from_home - angularDistance;
}
SetPinFrequency(RA_STEP, freqspeed); // Look at SetPinFrequencySafe()
pwmWrite(RA_STEP, 128);
digitalWrite(RA_DIR, direction);
digitalWrite(RA_running, HIGH); // I think this just turns it on?
return finalLocation;
}
float DeclinationMotor(float freqspeed, float angularDistance, byte direction)
{
Serial.print(freqspeed); Serial.print("DEfS "); Serial.print(angularDistance); Serial.print("DEaD "); Serial.println(direction);
int finalLocation = 0;
if(freqspeed == NULL && angularDistance != NULL)
freqspeed = DEC_freq;
if(angularDistance != NULL)
{
if(direction == HIGH)
finalLocation = DEC_degrees_from_home + angularDistance;
else
finalLocation = DEC_degrees_from_home - angularDistance;
}
SetPinFrequency(DEC_STEP, freqspeed);
pwmWrite(DEC_STEP, 128);
digitalWrite(DEC_DIR, direction);
digitalWrite(DEC_running, HIGH);
return finalLocation;
}
void StopRAMotor()
{
Serial.println("-RAMotor Stopped-");
digitalWrite(RA_STEP, LOW);
digitalWrite(RA_running, LOW);
}
void StopDECMotor()
{
Serial.println("-DECMotor Stopped-");
digitalWrite(DEC_STEP, LOW);
digitalWrite(DEC_running, LOW);
}
setup()
instead. It is made for things, that only should run once – chrisl Aug 28 '19 at 19:36