Coding for sensing current value while to and fro movement of piezo actuator.

  1. I am using a piezo actuator and controlling its to and fro movement along z axis for a machining.

  2. A current sensor is in used to sense the current while the time of machining.

  3. In downward motion, at the time of machining if the current sensor sense current more than 1.8A, then the actuator automatically will move upward.

I have used this code, but some problem is there in the coding thats why if condition, current sensor and backward motion is not working.

I need your for the right coding

#define VIN A0 // define the Arduino pin A0 as voltage input (V in)

const float VCC = 5.0;
float sensitivity = 0.185;
const float QOV = 0.5 * VCC; // set quiescent Output voltage of 0.5V
float voltage; // internal variable for voltage
float spark_current =1.8; // set the current which below that value, doesn’t matter. Or set 0.5
//int led_pin = 11;
int led_pin1 = 10; //Initializing LED Pin

int i;
int j;

void setup()
{
Serial.begin(115200);
pinMode(led_pin1, OUTPUT); //Declaring LED pin as output
Serial.println(“ACS712 Current Sensor”);
}

void loop()
{
float voltage_raw = (5.0 / 1023.0)* analogRead(VIN); // Read the voltage from sensor
voltage = voltage_raw - QOV + 0.012 ; // 0.000 is a value to make voltage zero when there is no current
float current = voltage / sensitivity;
Serial.println(current,3); // print the current with 2 decimal places
delay(100);

if
(current < spark_current )
{
for(int i=0; i<255; i++) //Fading the LED
{
//analogWrite(led_pin, i);
analogWrite(led_pin1, i);
delay(100);
}
}
else
{
for(int i=255; i>0; i–)
{
//analogWrite(led_pin, i);
analogWrite(led_pin1, i);
delay(100);
}
}
}

I need your for the right coding…

Hi arjita,
Please read through the General Guidance and How to use the Forum sticky post at the top of this forum. It covers a lot of information about how to use this forum.
Pay attention to the part on code tags. You can edit your post to put your code inside of code tags.

This make it much easier for us to help you.

Also, auto format your code. This indents the code in a way that makes it easier to see the block and lines up the curly brackets. Again, making it easier for us to help you.

Now I need help understanding your problem. you calculate a current and print it out.
Is it printing out values that you expect?

Then you test that current value against a spark_current value of 1.8.
If the current is less, you walk your actuator from o to 255.
It the current is more, you walk your actuator fro 255 to zero.
Then you repeat.

Is that what is happening?
Is that what you want?

edit: also, many forum members, myself included, avoid personal messages for helping with code problems. Keeping the conversation here on the forum makes the history of the work available to future searches.

yes exactly that I want. Sir, can you please help me out

I think I am trying to help you.
But you appear to have glossed over my many questions and simple repeated a request for help.
Let me try again in smaller pieces.

This link to How to use this Forum details how t post code using code tags.
You can edit your first post and put code tags around your code.
Can you please do that?

Are you trying to implement a feedback loop to control the actuator position based on the current,
in order to keep the current constant at 1.8A?