Plasma cutter - controlling height above cut workpiece - need help with code

Hello everyone!

I got DTHC as my class project, class teacher thought ARDUINO might be suitable... So now, I'm struggling with code, and came here for help. This is part of program, which is responsible for incrementing and decrementing value for height above cutted workpiece. I'm sorry if I'm missing something, I'm new to programming...

To the problem - buttonUp should increment variable buttonHeight and buttonDown should decrement it. It's working, but with one exception - when it increments to 10 or to 1, it jumps for 2, instead of one. So it shows something like this:

height above workpiece = 1mm height above workpiece = 3mm //button up pushed once, have no idea why it's doing this height above workpiece = 2mm //button down pushed once, working normally ... ... //in the meantime, it's working as it should - button up increments, button down decrements... ... height above workpiece = 10mm height above workpiece = 8mm //button down pushed once height above workpiece = 9mm //button up pushed once

BTW, sorry for my English, I'm Czech and still learning... Thank you all, I will update on progress. :-)

Here goes the code:

/*DTHC - Digital Torch Height Control
  Řízení výšky plasmového hořáku pomocí procesoru ATMega328
  a vývojové desky ARDUINO
  David Straus MEA4, 2011
*/

const int voltagePin = A0;             
const int hallPin = A1;                 

const int ledUp = 11;                   
const int ledOk = 10;                  
const int ledDown = 8;                 
const int ledTest = 13;

const int buttonUp = 2;                 
const int buttonDown = 3;               

int buttonHeight = 0;                 

int buttonUpState = 0;                 
int buttonDownState = 0;              

int lastButtonUpState = LOW;             
int lastButtonDownState = LOW;          

int voltageSensor = 0;                  
int voltageValue = 0;                   

int hallSensor = 0;                  
int hallValue = 0;                   




void setup() 
{
  pinMode(ledUp, OUTPUT);               
  pinMode(ledOk, OUTPUT);
  pinMode(ledDown, OUTPUT);
  
  pinMode(buttonUp, INPUT);            
  pinMode(buttonDown, INPUT);
  
  Serial.begin(9600);
}

void loop() 
{
  hallSensor = analogRead(hallPin);                          
  hallValue = map(hallSensor, 0, 1023, 0, 4);               
  voltageSensor = analogRead(voltagePin);                   
  voltageValue = map(voltageSensor, 0, 1023, 0, 10);         
  
  buttonUpState = digitalRead(buttonUp);                     
  buttonDownState = digitalRead(buttonDown);              
  

  buttonHeight = constrain(buttonHeight, 2, 9);
  


  
  if(hallValue >= 2)                                         
 {
               

     if(buttonUpState != lastButtonUpState)                 
       {
          if(buttonUpState == HIGH)                          
            {
              ++buttonHeight;
                Serial.print("Height above cut workpiece is: ");
                Serial.print(buttonHeight);
                Serial.println(" mm.");
            }
       }
      lastButtonUpState = buttonUpState;                       
      

     
     if(buttonDownState != lastButtonDownState)            
       {
          if(buttonDownState == HIGH)                         
            {
              --buttonHeight;
                Serial.print("Height above cut workpiece is: ");
                Serial.print(buttonHeight);
                Serial.println(" mm.");
            }
       }
      lastButtonDownState = buttonDownState;                     
  }

  else                                                      
  {

     if(buttonUpState != lastButtonUpState)                 
        {
          if(buttonUpState == HIGH)                          
          {
            ++buttonHeight;
            Serial.print("Height above cut workpiece is: ");
            Serial.print(buttonHeight);
            Serial.println(" mm.");
          }
        }
     lastButtonUpState = buttonUpState;                       
     
     if(buttonDownState != lastButtonDownState)                 
        {
          if(buttonDownState == HIGH)                         
          {
            --buttonHeight;
            Serial.print("Vyska nad obrobkem nastavena na: ");
            Serial.print(buttonHeight);
            Serial.println(" mm.");
          }
        }
     lastButtonDownState = buttonDownState;                    
    
  }  
}

Sorry, native English speaker, but "digital torch height control" is about as nonsensical as it gets

Well, torch height control could be analog or digital. ARDUINO is pretty much digital - how should I call it?

http://www.candcnc.com/MP3000-DTHC.htm

They call it DTHC. It is DTHC.

No, sorry, still pretty much nonsensical. Have you posted this in your native language forum?

My native language is Czech. Do you see Czech forum here? No.

All right, I could call it "thing, that controls height of plasma torch cutter above workpiece, using binary values instead of analog ones", but why should I do that...

No need to apologies for your English, but just recognize that random abbreviations may not mean a great deal to other people here. There was no mention of any potentially lethal technologies in your original post. In UK English, a torch is simply a flashlight, with no connotations of high temperatures or cutting of materials.

It's just CNC controlled machine, that cuts things. I had no idea, that "DTHC" makes no sense (even big CNC companies use this name) and I just wanted to know, why is my code not working as it should... :(

Right, in UK english it should be "Digital Arc Height Control". Can you help me now, please?

Changed title, it shouldn't be confusing anymore...

I'm a long way from an Arduino, but the code you posted doesn't even compile (I suspect you know this), so what are you asking?

If it makes you feel any better I knew immediately what you meant by "Digital Torch Height Control". Don't mind the English guy -- he's been known to store car jacks in his boot of all places. Must be hard to walk like that...

I think your bug is primarily in the placement of your constrain() call. You're constrain()ing the value between 2 to 9, then you increment or decrement it, then you print it. That's why it starts at 2, you can decrement it to 1, print it, it gets re-constrained() to 2, increments to 3...

Chagrin: Thanks, that made me feel better. :)

Code now compiles... I will try replacing constrain(), if it won't help, I'll be back. Thanks. :)

Imagine saying you’d stowed your jack in your trunk when you’re not even an elephant.
Your code really didn’t compile when you posted it, so changing it after people pointed this out makes the thread hard to follow.
If you want help, stick to the plot.

There are no calls in the code to turn on the internal pullup resistors. This implies that you have external pullup or pulldown resistors. Do you? How ARE the switches wired?

You are not debouncing the buttons, except through the use of serial print statements. Not a good idea to rely on Serial.print() taking time.

You have code to increment or decrement the count when the hallValue is greater than 1, and apparently identical code for when hallValue is less than 2.

Why does the up/down stuff depend on hallValue?

Chagrin: Thanks, that helped me a lot, instead of constrain(), I used min() and max(), it's now working as it should! :)

PaulS: Yes, I'm using external 10k pullup, mostly because i didn't even knew, that ARDUINO have internal pullups...

Bouncing don't seem to be a problem, but I'll debounce buttons eventually...

And hallValue is value from Hall sensor - it's detecting if torch/arc is lit or not, but I still need to set height above workpiece, even if it's off.

Thanks for interest and help!

I had no problem understanding you were using a plasma cutting torch, and you are far from a beginner at English. As you said you should debounce the buttons, just because they work today does not mean they will not start making extra steps tomorrow.

And hallValue is value from Hall sensor - it's detecting if torch/arc is lit or not, but I still need to set height above workpiece, even if it's off.

That does not really make too much sense. If you are going to move the torch up or down whether or not it is lit then you should remove the redundant code.