Controller sensorvalue

Hi,

Let me first introduce my project: I'm making a pilot box (with RFID, LCD and interlock (based on a H-bridge) which has to control the charging proces of an Electric Vehicle.

Everthing is working well except one thing: I'm using the frequency/ PWM library (PWM frequency library - Libraries - Arduino Forum) to generate a DC or PWM signal. With this signal the EV can see if he is ready to charge (9V PWM) or not (12V DC) or the vehicle is charging (6V PWM). Pin 10 of the arduino MEGA is translated from +5/0 to +12/-12. I'm using an opamp to get the 12V and -12V signal (see attachment). the -input is used for the divider and the + input for the PWM. It has to generate a 12V DC , a 9V PWM or a 6V PWM signal with a 1kHz frequency. But i'm having some problems with that. I'm reading an analog value (sensorValue) which will select wether it's a PWM signal (9V or 6V) or not (12V DC) signal.

My question:

When I'm reading my sensorvalue with, for example, ReadAnalogValue, I'm getting stable values. But when I'm running my whole program and looking at the serial monitor, the sensorvalue will always be 0 (look at the attachment). When I'm looking at the scope there are three different voltages. On my scheme in attachment, the values of AD1 are the values of sensorValue.

My code (very straightforward):

Code:

#include <PWM.h> //We include the library for //changing the frequency of the PWM signal
//http://forum.arduino.cc/index.php?topic=117425.0


int32_t frequency=1000; //We set the frequency of the PWM at 1000 Hz




//Declare all variables in head section

float Vpilot=0;


const int sensorPin = A1;
int sensorValue = 0; //This wil measure the input voltage with a number between 0 annd 1023
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value


int PWMsignal=0; //Variable for knowing if the EV supply equipment produces a PWM signal or not
int Ipp=0; //We create a variable to indicate the maximum current that can flows in the cable. If any of the resistors below is connected, then the maximum current will always be 0 A (No current)

int Pin=10; //Generates the PWM with a 1000Hz frequency
int Ivp=32; //We assign the current limit set for the equipment in A (a maximum of 32A with the switch)

int PP=A6; //Pin attached before the resistor of the proximity
int I=0; //Variable that contains the real current that will flow


int VolPP; //Variable to store the voltage before the resistor of the proximity (It is a number between 0 and 1023)
float VPP;
float v2;

//Duty Cycle
float DC; //The duty cycle for PWM
int DCint; //The duty cycle for the output pin



boolean stateA=true; 
boolean stateB=false;
boolean stateC=false;
boolean del = false;
int Mos=13; //Pin for Mosfet (Contacto)r
int prox;

//The setup routine runs once when you press reset:

//-----------------------------------------------------
void setup() {
 
 
 Serial.begin(9600); //Initialize serial communication at 9600 bits per second
   InitTimersSafe();
   bool success=SetPinFrequencySafe(10,frequency);

 

}

//The loop routine runs over and over again forever:
void loop() {
 
pinMode (PP, INPUT);
VolPP=analogRead(A6); //Variable to store the voltage before the resistor of the proximity (It is a number between 0 and 1023)
v2=VolPP*5;
VPP=v2/1023; //We transform the voltage from 0- 1023 to 0-5V and we storage it in the variable VPP
 prox=0;
//We check the voltage before the resistor of the proximity and depending on it we can know how much is the resistor and assign the maximum current

if (VPP>2.9 && VPP<3.15){ //The resistor of the proximity is around 1500Ω
prox=1500;
}
if (VPP>1.80 && VPP<2.18){ //The resistor of the proximity is around 680Ω
prox=680;
}
if (VPP>0.85 && VPP<1){ //The resistor of the proximity is around 220Ω
prox=220;
}
if (VPP>0.42 && VPP<0.51){ //The resistor of the proximity is around 100Ω
prox=100;
}
Serial.print("VPP:");
Serial.println(VPP);
//We check the voltage before the resistor of the proximity and depending on it we can know how much is the resistor and assign the maximum current

switch (prox){
 case 1500: //The resistor of the proximity is around 1500Ω
 Ipp=13;
 break;
 case 680: //The resistor of the proximity is around 680 Ω
 Ipp=20;
 break;
 case 220: //The resistor of the proximity is around 220Ω

 Ipp=32;
 break;
 case 100: //The resistor of the proximity is around 100Ω

 Ipp=63;
 break;
 
 default:
 Ipp=0;
 }
 I=min(Ipp,Ivp);
 Serial.println("Proximity:");
 Serial.print(prox);
 Serial.println(" - "),
 Serial.println(I);
 //lcd.print(I);
 //----------------------------------------------------------------------------------------------------------------------------
 
//We make 12 measurements of the voltage (1.2ms) to make sure in case we have a PWM signal which one is the highest and the lowest voltage:
//int sensorValue = A1;

for (int i=0; i<100; i++){
 
 sensorValue = analogRead(sensorPin); //Read the input on pin A1 (0-1023)
 Vpilot=((sensorValue-512)*24/1023);
 delay(150);
 //Vpilot is now between -12 and +12
 
 if (sensorValue > sensorMax) {
     sensorMax = sensorValue;
   }

   // record the minimum sensor value
   if (sensorValue < sensorMin) {
     sensorMin = sensorValue;
 }
 }
 //Note: 5V corresponds to a range of -12 to +12

 Serial.print("sensorvalue:");
 Serial.println(sensorValue);
 pinMode(Mos, OUTPUT);
 //We determine if there is or not a PWM signal:
   if (sensorValue>=930){
   stateB=false;
   stateC=false;
   stateA=true;
   digitalWrite(Mos, LOW);
   PWMsignal=0; //State A, no pilot
   }
   if (sensorValue>=650 && sensorValue<=730){
   stateB=true;
   stateC=false;
   stateA=false;
   digitalWrite(Mos, LOW);
   PWMsignal=1; //If the pilot´s voltage  isaround 9V, it means we are in state B1  and we create a PWM signal to go t   state B2
   }
   else if(sensorValue<480 && I > 0)
   {
   stateC=true;
   stateB=false;
   stateA=false;
   digitalWrite(Mos, HIGH);
   PWMsignal=1; //If the pilot´s voltage is around 6V, it means we are in state C1 and we create a PWM signal to go to  state C2
   }

//---------------------------------------------------------------------------

   Serial.print("PWMsignal:");
   Serial.println(PWMsignal);
   pinMode(Pin, OUTPUT);
   if (PWMsignal == 0) { //When there is current onthe Pin 9 and there is not a PWM signal,then the pilot´s voltage is 12 V
     
       digitalWrite(10, LOW);
   }
   //
  // float DC = 0.0;
  if (PWMsignal == 1) { //When there is a PWM signal on the pin 9, then the pilot´s voltage is a PWM signal between 12V and - 12V
     // We create the PWM signal in the PIN 9 between 12V and -12V

     if((I>=6) && (I<=51))
     {
     DC = (I / 0.6) / 100;
     }
     //85% < duty cycle <= 96%
     else if (I > 51 && I <= 80) {

       DC = ((I / 2.5) + 64) / 100;
       
     }
     
     else if (I == 80)
     {
       DC = 96;
       //10% <= duty cycle <= 85%
     }
   //  /100  >> percentage
   //float DC = 0.0;
   DCint = DC * 255;
   
   //As Analog output pin is 8bit we can get maximum 2^8=256 or a range of values between 0 to 255.
   
   //Sending the value 255, to the LED input produces 100% duty-cycle, which results in full power on a PWM pin. 
   //Sending the minimum value 0, to the LED input produces 0% duty-cycle, which results in no power on a PWM pin.

   //Next line disabled for testing


   pwmWrite(Pin, DCint); //We create the PWM signal in the Pin 9 with a duty cycle=DCint
   Serial.print("Dutycycle:");
   Serial.print(DCint);
   Serial.print(" - ");
   Serial.println(DC);

  }
}

Thanks for the help guys,

kind regards,

Please put your code in its own window as seen in other posts. This can be done by placing

[code] and [/code]

around the code or use the </> icon. This makes it easier for others to read.

How to use this forum

Weedpharma