help wanted...

Hello again,

I'am still trying to operate a sheetmetal deforming machine with a arduino uno.

I have studied a lot samples of codes and posts to learn to understand programming the arduino.

For my machine i use 2 potmeters, 1 proximity switch. 1 Potmeter is for rising time of the hyraulic cilinder. I changed the led for a relay, to oprate a hydraulic valve. So far so good. The machine i make is a simple big hammer operated with a hydraulic cilinder. Hammering metal sheet plates. If the hammer comes down and gives a hit, directly afther the hit it must go up to use the energie of the rebound of the hammer. Therefor i wil use a proxswitch to 'see' when de hammer is down. Potmeter 2 i will use is to adjust the rising time. (when to rise afther the hit) Question, am i thinking in the right direction to create a code for this application???

I tried also the command with delay's but that wasn't acurate enough. In the code i run the valve stays high. Please help me out...

Thanks

/*    12-07-2011 sheetplate machine
    
                  * dropheight potmeter
                  * resttime Potmeter
                  * Start button 1        
                  * switch 3 Auto Sell2
*/

// Variabele verklaring

  int Pot1   ;       // dropheight potmeter
  int Pot2   ;       // resttime potmeter
  int Butt1  ;       // startbutton
  int Sell2  ;       // auto select
  int Sensor ;       // Lower sensor
  long previousMillis = 0;
  
  const int Valve1 = 12  ;      // dig output Valve
  
  int dropheight   ;   // dropheight value
  int resttime     ;   // resttime Value
  int Valveauto    ;   // Auto control
  int Autocontrol  ;   // Set autocontrol

void setup() {
  
  pinMode(Butt1, INPUT);    // declare Butt1 as input
  pinMode(Sell2, INPUT);    // declare Sell2 as input
  pinMode(Valve1, OUTPUT);  // declare Valve1 as output
   }

void loop() {
  
 // Read_Digitale_Input

  Butt1 = digitalRead(2);  // button 1_Dig_in_2
  Sell2 = digitalRead(5);  // select button 2_Dig_in_5
  
  // Read_Analoge_Input
  
  Pot1 = analogRead(A0);  // Read analoge input 0
  dropheight = map(Pot1, 0, 1023, 0, 1200);  // Scale analoge val 0 to 1200 dropheight (mm)
  
  Pot2 = analogRead(A1);
  resttime = map(Pot2, 0, 1023, 0, 1000);  // Scale analoge val 0 to 1000 resttime (ms)
  

  // Automatic_control
  
    if ((Sell2 == 1)) // Check selector switch for auto control
      {
        Autocontrol = 1; // Autocontrol ON
      }
    else{ (Autocontrol = 0); } // Autocontrol OFF
      
      if ((Autocontrol ==1) && (Butt1 == 1)) // Check startbutton state
      {
      
        if (Sensor = 1) // Check lower sensor state
      {
      Valveauto = 1; //Ram weight up
      }
    unsigned long currentMillis = millis(); 

    if(currentMillis - previousMillis > dropheight) // Compare timer to dropheight
      {
        Valveauto = 0; 
      previousMillis = currentMillis; 
      }
    } 
 // Set_output_ state 
   
   if ( Valveauto == 1)
    {
      digitalWrite(Valve1, HIGH); // valve active
    }else{ (digitalWrite(Valve1, LOW)); } // valve off
   

}

Edit: Moderator edited to put code into code box. AWOL

    if (Sensor = 1) // Check

Bingo!

When posting code, please use the # icon on the editor's toolbar. You can go back to your post, click "modify", highlight the code, then click the # icon, then click on "save".

That's quick! and there always a but, i mean what is wrong whit this you post?

if (Sensor = 1) // Check

sorry i am not that far in programming.

Best Regards

you want

if (Sensors == 1)

The way you have it you are assigning the value 1 to the variable sensors instead of performing any kind of boolean evaluation.

piet: That's quick! and there always a but, i mean what is wrong whit this you post?

if (Sensor = 1) // Check

sorry i am not that far in programming.

Best Regards

if (Sensor == 1) // Check

  if ( Valveauto == 1)
    {
      digitalWrite(Valve1, HIGH); // valve active
    }else{ (digitalWrite(Valve1, LOW)); } // valve off

Hint:digitalWrite(Valve1, Valveauto);

a point to note: If statements only execute when the statemet inside the brackets is true. E.G.

if(1){
//this code will execute
}
if(0){
//this will not
}

There is no need to have if(Sensor == 1) - you can write if(sensor) instead. If you want to test for a false statement then you will need an ! :

if(!1){
this will work
}
if(!0){
this will not
}

Onions.

I made the changes in my prog, but for me it's very hard working!!! When i am finished ill let you know.

regards

There is no need to have if(Sensor == 1) - you can write if(sensor) instead. If you want to test for a false statement then you will need an ! :

Perhaps it isn't absolutely needed, but it is certainly clearer to use an explicit comparison.

On the other hand, Sensor never gets assigned a value in OP's code, so it will never be 1.

I'd also suggest, OP, that as a beginner, you should get in the habit of putting every { and } on its own line, and properly indenting the code between them. It is much easier, in my opinion, to see the structure of this code:

if ( Valveauto == 1)
    {
      digitalWrite(Valve1, HIGH); // valve active
    }
    else
    {
      digitalWrite(Valve1, LOW); // valve off
    }

than this code:

if ( Valveauto == 1)
    {
      digitalWrite(Valve1, HIGH); // valve active
    }else{ (digitalWrite(Valve1, LOW)); } // valve off

What's with the extra parentheses in the else block?

Probably nothing. As I wrote in my first post, I'm completely new to the Arduino story. But I see slowly the unknown possibilities. But the programming is totally new to me. As I wrote earlier, I have gone through an incredible amount of forums to read to understand. even react in a forum is new to me. So I'm pleasantly surprised with the help of everyone and grateful.

But my program still does not work ....

just keep studying, I think ...

regards Piet.

But my program still does not work ....

Doesn't work as in doesn't do anything, or doesn't work as in doesn't do exactly what you want?

If you post your current code, with all suggested fixes incorporated, along with a description of what you expect the code to do and what it actually does, we'll be more than happy to help you get it working.

With this code,

description as follows,

pot1 must indicate how long the hammer goes up. the hammer is in free fall back down. pot2 to indicate when the hammer goes up again. The sensor I want a reference point to see if the hammer is low. The point from low to high is very important because the energy I want to use the hammer as soon as possible to go up.

I hope it's a little obvious. it is a hobby project sheet metal to deform

greeting

// Variabele 

  int Pot1   ;       // dropheight potmeter
  int Pot2   ;       // resttime potmeter
  int Butt1  ;       // startbutton
  int Sell2  ;       // auto select
  int Sensor ;       // Lower sensor
  long previousMillis = 0;
  
  const int Valve1 = 13  ;      // dig output Valve
  
  int dropheight   ;   // dropheight value
  int resttime     ;   // resttime Value
  int Valveauto    ;   // Auto control
  int Autocontrol  ;   // Set autocontrol

void setup() {
  
  pinMode(Butt1, INPUT);    // declare Butt1 as input
  pinMode(Sell2, INPUT);    // declare Sell2 as input
  pinMode(Valve1, OUTPUT);  // declare Valve1 as output
   }

void loop() {
  
 // Read_Digitale_Input

  Butt1 = digitalRead(2);  // button 1_Dig_in_2
  Sell2 = digitalRead(5);  // select button 2_Dig_in_5
  Sensor = digitalRead(6);  // sensor
  // Read_Analoge_Input
  
  Pot1 = analogRead(A0);  // Read analoge input 0
  dropheight = map(Pot1, 0, 1023, 0, 1200);  // Scale analoge val 0 to 1200 dropheight (mm)
  
  Pot2 = analogRead(A1);
  resttime = map(Pot2, 0, 1023, 0, 1000);  // Scale analoge val 0 to 1000 resttime (ms)
  

  // Automatic_control
  
    if ((Sell2 == 1)) // Check selector switch for auto control
      {
        Autocontrol = 1; // Autocontrol ON
      }
    else{ (Autocontrol = 0); } // Autocontrol OFF
      
      if ((Autocontrol ==1) && (Butt1 == 1)) // Check startbutton state
      {
      
       if (Sensor == 1) // Check lower sensor state
      {
      Valveauto = 1; //Ram weight up
      }
    unsigned long currentMillis = millis(); 

    if(currentMillis - previousMillis > dropheight) // Compare timer to dropheight
      {
        Valveauto = 0; 
      previousMillis = currentMillis; 
      }
    } 
 // Set_output_ state 
   
   if ( Valveauto == 1)
    {
      digitalWrite(Valve1, Valveauto); // valve active
    }else{ (digitalWrite(Valve1, LOW)); } // valve off
   

}

Edit: Moderator edited to put code into code box. When posting code, please use the # icon on the editor's toolbar. You can go back to your post, click "modify", highlight the code, then click the # icon, then click on "save". Coding Badly.

int Butt1  ;       // startbutton

void setup() {

  pinMode(Butt1, INPUT);    // declare Butt1 as input

You haven't assigned Butt1 a value. You aren't connecting the switch to pin 0 are you?

Ditto for Sel2. Can't have two things on the same pin.

  Butt1 = digitalRead(2);  // button 1_Dig_in_2

So, now you are overwriting the value you didn't store in Butt1...

You should add a Serial.begin() call in setup(), and Serial.print() and Serial.println() statements in loop(), to see what is going on.

There are no digitalWrite() statements in setup() to turn on pull-up resistors. This implies that you have external pull-up or pull-down resistors. The == 1 test (should be == HIGH) implies pull-down resistors. You do have these external resistors, right?

    if ((Sell2 == 1)) // Check selector switch for auto control
      {
        Autocontrol = 1; // Autocontrol ON
      }
    else{ (Autocontrol = 0); } // Autocontrol OFF

In the end, Autocontrol ends up with the same value that is in Sel2. Autocontrol = Sel2; is so much easier (and faster, too).

You've explained now what you want the code to do, but not what it actually does. How is the program not meeting your expectations?