Using 0 - 5V for a digital latching switch

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);
 
}

Hi Jeremy,

Could you please describe what type of joystick you are using and how it is interfaced with the arduino? Maybe a photo or to would be helpful as well? cheers.

The joystick has a 10K pot, at the moment for testing I am just using a 10K Pot.
It is just a 0 - 5V signal with 2.5 v when the joystick is centred. or 0 -1023

In my if statement when the joystick is returning a value less than Zmax (which is a static variable set at 400) it should make Aux1Sig HIGH.

If I replace the Aux1Sig with another int that is physically connected and defined as an output pin then it does change the state of the int.

The if part of the code is working, I just cant seem to make the "Do part change state of a variable to HIGH unless it is connected to an output.

Thanks for your interest and hopefully you can see what I am doing wrong. I did leave a fair bit of the program the so others can see the setup, but basically it is the If statement, “DO” part that is in question.

Thanks,

Jeremy

Hi Michael,

I forgot to mention, but you can probably see from the code…

I am using A4 (Analog input 4 for the z Axis input of the joystick.

Hey Jeremy,

I’m curious, particularly interested in lines 183 and 184

  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 

Could you try replacing this digital write with a serial output just to state that the Aux1Sig was set to high, it’d bad practice to use this kind of debugging process, but usually a quick throw together solution is the best way to go. If the line doesn’t trigger, then we’ve got some kind of an issue with the integer comparison between sensorValueZ and Zmax at this point, which we can walk back through the code to find where the issues are occuring. Please let us know if you’re able to resolve the issue, as the answer could be useful for another user of this forum in the future.

Bryce
Core Electronics | Support

I suspect that should be aux1OutPin, not aux1Sig.

Hi Bryce,
Thanks for your input. I had already done that and it works.

I am new to arduino programming. Until now I had done more ladder type PLC and a bit with Siemens step7.

In PLC language the “Aux1Sig” would be a memory space that you call and make either High or Low.

I am using the Aux1Sig further in the code to toggle an output on and off. I was using the Aux1Sig as if it were a digital input.

Regards,

Jeremy

Hi Bryce,

My apologies, I answered your suggestion incorrectly.

I have not tried replacing the digital write with serial output. I mixed up an answer from someone else.

I had replaced “Aux1Sig” with an actual digital Pin output and that worked when the pot signal was turned down less than Zmax (400) so I know the IF part is working. I can also see the value of “sensorValueZ” change with the pot turning in the Serial Monitor.

I am not sure about serial output, I have not done that yet. My next few hours tonight learning about that.

Thanks again.

Regards,

Jeremy

Hi Jeff,

I was trying to write to a Memory Marker for lack off a better word. I need to use this value further down in my code. I just don’t know how to do what I want. If i put in the …OutPin it does work. I just want other things to happen or be compared before I write to the output.

I know this is not very clear but I am learning slowly on some of the terminology.

Thanks for your patience.

Jeremy

Hi Jeff,

As suggested I replaced the Aux1Sig with Aus1SigoutPin.

I mapped it to Pin 21 (Which does not exist as far as I know) and now it works.

This leads to my next question, what are the repercussions of mapping to pins that don’t exist?

Thanks for your help.

Regards,

Jeremy

You can write to a ’ memory marker’ but you won’t use digitalWrite to do it - your ‘memory marker’ is a variable and you will just set the variable to a value in an assignment statement, such as

aux2Sig = LOW;

Then you can use the value of aux2Sig anywhere in your code, including as an argument to digitalWrite().

Hi All,

Thanks for the help I have received regarding this. I now have it working with the same code I had before, only difference I can see is that I now have the IF statement reading the z Axis pot doing its logic before the logic is called up in the following lines.

It was strange how if I wrote directly to an output it was ok, otherwise it did not work.

Just for interest I have attached the snippet of code that latches when the joystick would be moved left and right.

There is also the code for latching a PB switch.

Credit to David A. Mellis who I have copied code from for the Latching of a momentary switch.

Regards,

Jeremy

/*
 * 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
int Aux1outPin = 6;       //  the output pin to turn on Aux 1

int Aux1Sig = HIGH;        //  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


// Aux 2
int Aux2outPin = 12;       //  the output pin to turn on Aux 1

int Aux2Sig = HIGH;        //  the z axis signal for Aux 1 to switch  Left Twist)
int Aux2state = LOW;      // the current state of the Aux 1 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


// 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.



//  Static Variables

#define Zmax 300          // Z Axis, the maximum value before movement happens when joystick is centered (Twist)
#define Zmin 700          // 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(Aux1outPin, OUTPUT);
  pinMode(Aux2outPin, OUTPUT);
}

void loop()
{  

 
  // Auxilary Output Enable Conditions (Z axis Twist)

  sensorValueZ = analogRead(ZAxisinPin);                        //  Read Position of Z Axis

  if ( sensorValueZ <=Zmax ) { (Aux1Sig = HIGH);}  // If twisted to the left Aux 1 Pin High.  Used as a digital input for Aux 1 toggle On / Off 
  else {(Aux1Sig = LOW);}                                                        //  Otherwise Low 

  if ( sensorValueZ >=Zmin ) { (Aux2Sig = HIGH);}  // If twisted to the left Aux 2 Pin High.  Used as a digital input for Aux 1 toggle On / Off 
  else {(Aux2Sig = LOW);}                                                        //  Otherwise Low 
 
// 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
   {
  
  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;
   }



//  Spotlight Toggle On / Off
 {
  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;
  
  }





  // print the results to the Serial Monitor:
  //Serial.print("Spot Light = ");
  //Serial.print(Spotstate);
  Serial.print("Z Axis Raw Value = ");
  Serial.print(sensorValueZ); 

  Serial.print("\t Aux 1 Signal Out Pin = ");
  Serial.print(Aux1Sig);

  Serial.print("\t Aux 2 Signal Out Pin = ");
  Serial.print(Aux2Sig);
  
  Serial.print("\t Aux State = ");
  Serial.print(Aux1state);

  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);
}

Hi Jeff,

Thanks for that.

Funnly enough I have just done what you said. In my last post I said I did not know what I did differently.

You have just spelt it out for me, now I remember removing the “digitalWrite” to see what would happen and of course it works.

Now I am satisfied as to where and why I went wrong.

Many thanks for you all to take the time and help out.

regards,

Jeremy

1 Like

Hi Jeff and Jeremy,

Excellent, glad to hear that the project is now correctly functioning! If you have any other questions please add them here and we’ll be here to help.

Bryce
Core Electronics | Support