You are on page 1of 8

Change the width of the Pulse according to the

change power

‫االسم\حسن عالوي عبد‬

Group –B-
We will generate a wave that depends on the change in the
power produced by the voltage change if the power increases, we will
make sure the voltage increases. We will reduce the pulse width, but if
the voltage is reduced we will increase the pulse width.
If the power is reduced we will make sure the voltage if increased will
increase the wave width, but if the voltage is reduced we will reduce
the wave width.
The algorithm on which the program is written is written in a
language C
Electrical circuit
The code

float average = 0;

float voltage;

float current = 0;

float power = 0;

float R = 1000;

float s=50;

float TIME=0.001;

float TIMEH=0;

float TIMEL=0;

float power1=1;

float voltage1=2;

float power2=1;

float voltage2=2;

int ENA = 10;

const int IN1 = 9;

const int IN2 =8;

int analogInput = A1;

float vout = 0.0;

float vin = 0.0;

float R1 = 30000.0; //

float R2 = 7500.0; //

int value = 0;
float powerfloat2 = 0;

float powerfloat1 = 0;

float voltagefloat2 = 0;

float voltagefloat1 = 0;

void setup()

Serial.begin(9600);

pinMode(analogInput, INPUT);

pinMode(A0, INPUT);

pinMode(ENA , OUTPUT);

pinMode(IN1, OUTPUT);

pinMode(IN2, OUTPUT);

void loop(){

///////////////////////////////////////////////////////////////////////////////////////////////////////////

float average = 0;

for(int i = 0; i < 1000; i++) {

average = average + (.0264 * analogRead(A0) -13.51);//for the 5A mode,

delay(1);

Serial.print("Current :");

Serial.print(average/1000);

Serial.println("A");

value = analogRead(analogInput);
// vout = (value * 20.0) / 1024.0; // see text

// vin = vout / (R2/(R1+R2));

// voltage=(vin*10)+2;

voltage = map(value, 0, 1023, 0, 5);

power=voltage*(average/1000);

power2=power;

voltage2=voltage;

////////////////////////////////////////////////////////////////////////////////////////////////////////

Serial.print("voltage[1]=");

Serial.print(voltage1);

Serial.print(" voltage[2]=");

Serial.println(voltage2);

Serial.print("power[1]= ");

Serial.print(power1);

Serial.print(" power[2]= ");

Serial.println(power2);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////

voltagefloat2 = voltage2*0.1;

voltagefloat1 = voltage1*0.1;

powerfloat1 =power*0.1;

powerfloat2=power1*0.1;

Serial.println(powerfloat1);

Serial.println( powerfloat2);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////

if ( powerfloat2 == powerfloat1){
s=50;

else if ( powerfloat2> powerfloat1 && voltagefloat1>voltagefloat2){

s=s-1;

else if ( powerfloat2> powerfloat1 && voltagefloat1<voltagefloat2){

s=s+1;

else if ( powerfloat2 < powerfloat1 && voltagefloat1 >voltagefloat2){

s=s+1;

else if ( powerfloat2< powerfloat1 && voltagefloat1<voltagefloat2){

s=s-1;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////

TIMEH=(TIME*s)/100;

TIMEL=TIME-TIMEH;

Serial.print("TIMEH=");

Serial.println(TIMEH*1000);

Serial.print("TIMEL= ");

Serial.println(TIMEL*1000);

delay(1000);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////

power1= power2;

voltage1= voltage2;
}

You might also like