Want to add a code to control the servo motor when the constant DC reaches 20 volts

hi ,
This is a code for measuring a constant voltage(DC VOLTMETER) using a voltage divider
After dividing the voltage, the two ends were connected to ground and the second to analog A0
The code displays the voltage on the serial monitor
I want to add a code to control the movement of the servo motor if the voltage(DC) is less than 20 volts and the return of the motor to its original position if the voltage is more than 20 volts.
Can you help me add the correct code for the servo motor? I tried but failed؟
And this is the code

const float arduinoVCC = 5.01;//Your Arduino voltage
unsigned long ValueR1 = 5070;
unsigned long ValueR2 = 7500;
double Voltage_Source = 12;
const int alanogPin = A0;//the pin connecting the voltage. 
const int inputResolution =1023;//works with most Arduino boards
const float average_of = 500;//Average of 500 readings

float voltage;
void setup() {
  Serial.begin(9600);
  Serial.println("hi: Reading Any voltage with Arduino");
  delay(500);

}

void loop() {
  //islamway.com ARDVC-01 Measure any voltage with Arduino
  readVoltage();

  Serial.print("Vin: ");
  //Serial.print(voltage);
  //Serial.print("V Avg: ");
  Serial.print(getVoltageAverage());
  Serial.println("V");
  //delay(100);
  //islamway.com ARDVC-01 Measure any voltage with Arduino
}//loop end


/*
 * @brief calculates the input voltage and updates the variable "voltage"
 * @param none
 * @return does not return anything
 * www.islamway.com code cairo, egypt
 */
void readVoltage(){
    //islamway.com ARDVC-01 Measure any voltage with Arduino
    //islamway.com http://islamway.com/L/?id=62
    int A0Value = analogRead(alanogPin);
    float voltage_sensed = A0Value * (arduinoVCC / (float)inputResolution); 
//  Serial.print("voltage_sensed:");
//  Serial.print(voltage_sensed);       
  voltage = voltage_sensed * ( 1 + ( (float) ValueR2 /  (float) ValueR1) );
  //islamway.com ARDVC-01 Measure any voltage with Arduino  
}//readVoltage()

/*
 * @brief calculates the average of input voltage and updates the variable "voltage"
 * @param none
 * @return retuns average of "average_of" iteration of voltage
 * www.islamway.com code cairo, egypt
 */
float getVoltageAverage(){
  //islamway.com ARDVC-01 Measure any voltage with Arduino
    //This code is explained at my at  http://islamway.com/L/?id=62  
    float voltage_temp_average=0;
    for(int i=0; i < average_of; i++)
    {
       readVoltage();
       voltage_temp_average +=voltage;
    }
      
  return voltage_temp_average / average_of;
    //islamway ARDVC-01 Measure any voltage with Arduino
}//getVoltageAverage

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}


What do those functions do?

Where is the servo code?

I want to add a code so that the command is given to the servo motor to move 180 degrees if the voltage reading is less than 20 volts, and I want the servo to return to its original position if the voltage reading is more than 20 volts

I did not put the code for controlling the servo because I do not know it and I need your help in adding it

Oh.

How to Control Servo Motors with Arduino (3 Examples).

Well review the tutor and then give it your best shot and when you get stuck let us know.

This is the latest modification and it didn't work unfortunately

const float arduinoVCC = 5.01;//Your Arduino voltage
unsigned long ValueR1 = 5070;
unsigned long ValueR2 = 94300;
double Voltage_Source = 12;
const int alanogPin = A0;//the pin connecting the voltage. 
const int inputResolution =1023;//works with most Arduino boards
const float average_of = 500;//Average of 500 readings
#include "Servo.h"  // include the servo library

 Servo servo1;       // creates an instance of the servo object to control a servo
 int posServo1 = 0;
 int servoPin = 9;       // Control pin for servo motor
float voltage;
void setup() {
  Serial.begin(9600);
  Serial.println("ISLAMWAY: Reading Any voltage with Arduino");
  delay(500);
servo1.attach(servoPin);  // attaches the servo on pin 4 to the servo object
 Serial.begin(9600);
 servo1.write(45);
 delay(2000);  

}

void loop() {
  //ISLAMWAY.com ARDVC-01 Measure any voltage with Arduino
  readVoltage();

  Serial.print("VOLT");
  //Serial.print(voltage);
  //Serial.print("V Avg: ");
  Serial.print(getVoltageAverage());
  Serial.println("V");
  //delay(100);
  //ISLAMWAY.com ARDVC-01 Measure any voltage with Arduino
analogValue = getVoltageAverage;                 // read the analog input (value between 0 and 1023)
 Serial.println(analogValue);
 if(analogValue==0)
   {
   posServo1=0;
   }
 else if(analogValue>=511)
   {
   posServo1+=1;
   }
 else 
   {
   posServo1-=1;
   }
 posServo1=constrain(posServo1,0,180);  
 servo1.write(posServo1);     // write the new mapped analog value to set the position of the servo
 delay(50);                   // waits for the servo to get there  
}//loop end


/*
 * @brief calculates the input voltage and updates the variable "voltage"
 * @param none
 * @return does not return anything
   */
void readVoltage(){
    //ISLAMWAY.com ARDVC-01 Measure any voltage with Arduino

    int A0Value = analogRead(alanogPin);
    float voltage_sensed = A0Value * (arduinoVCC / (float)inputResolution); 
//  Serial.print("voltage_sensed:");
//  Serial.print(voltage_sensed);       
  voltage = voltage_sensed * ( 1 + ( (float) ValueR2 /  (float) ValueR1) );
  //ISLAMWAY.com ARDVC-01 Measure any voltage with Arduino  
}//readVoltage()

/*
 * @brief calculates the average of input voltage and updates the variable "voltage"
 * @param none
 * @return retuns average of "average_of" iteration of voltage
  */
float getVoltageAverage(){
  
    float voltage_temp_average=0;
    for(int i=0; i < average_of; i++)
    {
       readVoltage();
       voltage_temp_average +=voltage;
    }
      
  return voltage_temp_average / average_of;

What does that mean?

Post a schematic.

Post an image of your setup.

Why not post code that actually complies?

Strike 2.

The code works very well in measuring the voltage, but I want to add an option to rotate the Arduino when the voltage reaches a certain amount

Work on getting a servo example to work.

BTW the Arduino is NOT a power supply for motors, use an external power supply for motors.

Servo stuff

Power externally.

power distribution blocks.



What does "doesn't work" mean? As an example of "it doesn't work" is a meaningless description to describing a problem. I want help with getting the PRU_1 to work with PyPin on a BeagleBoneBlack, it doesn't work. Now does "it doesn't work" explain the issue?

// pseudo code


switch ((int) voltage)
{
case 0 ... 19:
  servo.write(Close);
  break;
case 20 ... 39:
  servo.write(Open);
  break;
}

I will connect the external voltage source to power the Arduino and I will be grateful if you write me the complete final code so that I don't get confused

.can any one help me

All i need code to measure input dc voltage about 30 volt ..and if the input volt less than 20 volt then the servo move 180 degree ...and if the input voltage more than 20volt then servo return to 0 degree

This is not a question about getting the IDE to work but your problem with motors.
Therefore I have moved your post here.

Have you got a schematic?
Your ver key description of how your resistors are connected sounds wrong.

All i need code to measure input dc voltage about 30 volt ..and if the input volt less than 20 volt then the servo move 180 degree ...and if the input voltage more than 20volt then servo return to 0 degree
'''
In need the code and i will wireing all connection
'''

Use a voltage divider to reduce the input voltage to a safe level for the Arduino. You can find tutorials on line.

Cross post flagged.

The forum is not a place where a somebody writing code on demand.
We can help you, but you have to write code yourself.
Or you can hire a programmer in Paid consultancy section of the forum.

I did it but not working

const float arduinoVCC = 5.01;//Your Arduino voltage
unsigned long ValueR1 = 5070;
unsigned long ValueR2 = 94300;
double Voltage_Source = 12;
const int alanogPin = A0;//the pin connecting the voltage. 
const int inputResolution =1023;//works with most Arduino boards
const float average_of = 500;//Average of 500 readings
#include "Servo.h"  // include the servo library

 Servo servo1;       // creates an instance of the servo object to control a servo
 int posServo1 = 0;
 int servoPin = 9;       // Control pin for servo motor
float voltage;
void setup() {
  Serial.begin(9600);
  Serial.println("ISLAMWAY: Reading Any voltage with Arduino");
  delay(500);
servo1.attach(servoPin);  // attaches the servo on pin 4 to the servo object
 Serial.begin(9600);
 servo1.write(45);
 delay(2000);  

}

void loop() {
  //ISLAMWAY.com ARDVC-01 Measure any voltage with Arduino
  readVoltage();

  Serial.print("VOLT");
  //Serial.print(voltage);
  //Serial.print("V Avg: ");
  Serial.print(getVoltageAverage());
  Serial.println("V");
  //delay(100);
  //ISLAMWAY.com ARDVC-01 Measure any voltage with Arduino
analogValue = getVoltageAverage;                 // read the analog input (value between 0 and 1023)
 Serial.println(analogValue);
 if(analogValue==0)
   {
   posServo1=0;
   }
 else if(analogValue>=511)
   {
   posServo1+=1;
   }
 else 
   {
   posServo1-=1;
   }
 posServo1=constrain(posServo1,0,180);  
 servo1.write(posServo1);     // write the new mapped analog value to set the position of the servo
 delay(50);                   // waits for the servo to get there  
}//loop end


/*
 * @brief calculates the input voltage and updates the variable "voltage"
 * @param none
 * @return does not return anything
   */
void readVoltage(){
    //ISLAMWAY.com ARDVC-01 Measure any voltage with Arduino

    int A0Value = analogRead(alanogPin);
    float voltage_sensed = A0Value * (arduinoVCC / (float)inputResolution); 
//  Serial.print("voltage_sensed:");
//  Serial.print(voltage_sensed);       
  voltage = voltage_sensed * ( 1 + ( (float) ValueR2 /  (float) ValueR1) );
  //ISLAMWAY.com ARDVC-01 Measure any voltage with Arduino  
}//readVoltage()

/*
 * @brief calculates the average of input voltage and updates the variable "voltage"
 * @param none
 * @return retuns average of "average_of" iteration of voltage
  */
float getVoltageAverage(){
  
    float voltage_temp_average=0;
    for(int i=0; i < average_of; i++)
    {
       readVoltage();
       voltage_temp_average +=voltage;
    }
      
  return voltage_temp_average / average_of;

Can you help me