Hi All,
My apologies in advance, have not used a forum before so I am probably oblivious to some of the un-written rules.
I have some code I can’t seem to get working.
I was using some ones existing code to use a Push Button to toggle an output on and off. This works fine.
I now want to use the forth axis (Twist) of a 0 – 5v joystick as a digital switch, one way is Aux1 and the other way is Aux2.
I have substituted modified bits of code in to my new bit of code.
I cant figure out how to make the “Aux1Sig” in the attached code change state, or even set it to HIGH or 1 what ever works…
Could you please let me know what I might be doing wrong.
The attached code is a snippet of the whole project. The rest works ok, just this bit is a problem and I think once you tell me the correct syntax I can apply it to the rest of my code.
Code:
/* This version re-mapped pins to use Pololu Dual VNH5019 Motor Driver Shield
*
* Previous vorking version V4.00
* switch
*
* Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
* press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
* a minimum delay between toggles to debounce the circuit (i.e. to ignore
* noise).
*
* David A. Mellis
* 21 November 2006
*/
// Spot Light
int SpotinPin = 3; // the number of the input pin
int SpotoutPin = 5; // the number of the output pin
int Spotstate = LOW; // the current state of the output pin
int readingSpot; // the current reading from the input pin
int previousSpot = LOW; // the previous reading from the input pin
// Aux 1 & 2
int Aux1outPin = 6; // the output pin to turn on Aux 1
int Aux2outPin = 12; // the output pin to turn on Aux 2
int Aux1Sig = LOW; // the z axis signal for Aux 1 to switch Left Twist)
int Aux1state = LOW; // the current state of the Aux 1 output pin
int readingAux1; // the current reading from the Aux 1 input pin
int previousAux1 = LOW; // the previous reading from the input pin
int Aux2Sig = LOW; // the z axis signal for Aux 2 to switch Right Twist)
int Aux2state = LOW; // the current state of the Aux 2 output pin
int readingAux2; // the current reading from the Aux 1 input pin
int previousAux2 = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 500; // the debounce time for spot light, increase if the output flickers
// Slew Left & Right
int LSlewoutPin = 2; // the output pin to select Slew Left
int RSlewoutPin = 4; // the output pin to select Slew Right
int SlewOutPin = 9; // PWM Value to drive the slew motor
int XAxisinPin = A2; // Analouge input X Axis Pin
int sensorValueX = 512; // variable to store the X value from the joystick. Set at 512 to stop and drift if Analoug input is not connected at startep.
int outputValueX = 0; // PWM Value on start up
int LSlewLimitinPin = 11; // Left Slew Proximity Sensor
int RSlewLimitinPin = 13; // Right Slew Proximity Sensor
int sensorValueLProx = 0; // variable to store the value of Left Proximity
int sensorValueRProx = 0; // variable to store the value of Right Proximity
// Auxilary Inputs Z Axis Left & Right set as Digital Switch
int ZAxisinPin = A4; // Analoug Input Z Axis Pin
int sensorValueZ = 512; // variable to store the Z value from the joystick. Set at 512 to stop and drift if Analoug input is not connected at startep.
// Tilt Up & Down
int YAxisPin = A3; // Analouge input Y Axis Pin
int sensorValueY = 512; // variable to store the Y value from the joystick. Set at 512 to stop and drift if Analoug input is not connected at startep.
int outputValueY = 0; // PWM Value on Start up
int tiltUpPin = 7; // the output pin to enable Tilt Up
int tiltDownPin = 8; // the output pin to enable Tilt Down
int TiltOutPin = 10; // PWM Value to Tilt
// Static Variables
#define Xmax 498 // X Axis, the maximum Value before movement happens when joystick is centered 504
#define Xmin 518 // X Axis, the minimum Value before movement happens when joystick is centered 510
#define Xminpwm 10 // X Axis Minimum PWM Drive value 14
#define Xmaxpwm 95 // X Axis Maximum PWM Drive Value 96
#define Ymax 505 // Y Axis, the maximum Value before movement happens when joystick is centered
#define Ymin 510 // Y Axis, the minimum Value before movement happens when joystick is centered
#define Yminpwm 20 // Y Axis Minimum PWM Drive value
#define Ymaxpwm 95 // Y Axis Maximum PWM Drive Value
#define Zmax 400 // Z Axis, the maximum value before movement happens when joystick is centered (Twist)
#define Zmin 600 // Z Axis, the minimum Value before movement happens when joystick is centered (Twist)
void setup()
{
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(SpotinPin, INPUT_PULLUP);
pinMode(SpotoutPin, OUTPUT);
pinMode(LSlewLimitinPin, INPUT_PULLUP);
pinMode(RSlewLimitinPin, INPUT_PULLUP);
pinMode(tiltUpPin, OUTPUT);
pinMode(tiltDownPin, OUTPUT);
pinMode(LSlewoutPin, OUTPUT);
pinMode(RSlewoutPin, OUTPUT);
pinMode(SlewOutPin, OUTPUT);
pinMode(TiltOutPin, OUTPUT);
pinMode(Aux1outPin, OUTPUT);
pinMode(Aux2outPin, OUTPUT);
TCCR1B = TCCR1B & B11111000 | B00000001; // set timer 1 divisor to 1 for PWM frequency of 31372.55 Hz on pins 9 and 10
}
void loop()
{
{
readingSpot = digitalRead(SpotinPin);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (readingSpot == HIGH && previousSpot == LOW && millis() - time > debounce)
{
if (Spotstate == HIGH)
Spotstate = LOW;
else
Spotstate = HIGH;
time = millis();
}
digitalWrite(SpotoutPin, Spotstate);
previousSpot = readingSpot;
}
// Aux 1 Toggle On / Off
{
readingAux1 = digitalRead(Aux1Sig);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (readingAux1 == HIGH && previousAux1 == LOW && millis() - time > debounce)
{
if (Aux1state == HIGH)
Aux1state = LOW;
else
Aux1state = HIGH;
time = millis();
}
digitalWrite(Aux1outPin, Aux1state);
previousAux1 = readingAux1;
}
// Aux 2 Toggle On / Off (Z axis twist Right)
{
readingAux2 = digitalRead(Aux2Sig);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (readingAux2 == HIGH && previousAux2 == LOW && millis() - time > debounce)
{
if (Aux2state == HIGH)
Aux2state = LOW;
else
Aux2state = HIGH;
time = millis();
}
digitalWrite(Aux2outPin, Aux2state);
previousAux2 = readingAux2;
}
// Auxilary Output Enable Conditions (Z axis Twist)
sensorValueZ = analogRead(ZAxisinPin); // Read Position of Z Axis
if ( sensorValueZ <=Zmax ) {digitalWrite (Aux1Sig,HIGH);} // If twisted to the left Aux 1 Pin High. Used as a digital input for Aux 1 toggle On / Off
else {digitalWrite (Aux1Sig,LOW);} // Otherwise Low
if ( sensorValueZ >=Zmin ) {digitalWrite (Aux2Sig,HIGH);} // If twisted to the Right Aux 2 Pin High. Used as a digital input for Aux 2 toggle On / Off
else {digitalWrite (Aux2Sig,LOW);} // Otherwise Low
digitalWrite(LSlewoutPin, Aux1Sig); // temp code
// Tilt Enable Conditions
sensorValueY = analogRead(YAxisPin);
if (sensorValueY <=Ymax){digitalWrite (tiltUpPin,HIGH);} // If moved back, Tilt Up Pin High
else {digitalWrite (tiltUpPin,LOW);} // otherwise Low
if (sensorValueY >=Ymin){digitalWrite (tiltDownPin,HIGH);} // If moved Forward, Tilt Down Pin High
else {digitalWrite (tiltDownPin,LOW);} // otherwise Low
// X Axis PWM Drive
digitalRead(LSlewoutPin);
digitalRead(RSlewoutPin);
analogRead(SlewOutPin);
// print the results to the Serial Monitor:
//Serial.print("Spot Light = ");
//Serial.print(Spotstate);
Serial.print("Aux 1 State = ");
Serial.print(Aux1state);
Serial.print("\t Z Axis Raw Value = ");
Serial.print(sensorValueZ);
Serial.print("\t Spot State = ");
Serial.println(Spotstate);
// Serial.print("\t Slew Left = ");
// Serial.print(LSlewoutPin);
// Serial.print("\t Slew Right = ");
// Serial.print(RSlewoutPin);
// Serial.print("\t X Axis Output Value = ");
// Serial.print(outputValueX);
// Serial.print("\t Raw Sensor Value X = ");
// Serial.println(sensorValueX);
//Serial.print("\t Tilt Out Pin Value = ");
//Serial.print(TiltOutPin);
// Serial.print("\t Tilt PWM = ");
// Serial.print(outputValueY);
// Serial.print("\t Slew PWM = ");
// Serial.println(outputValueX);
// wait 2 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading:
delay(2);
}