[pre]//commutatoR, Pwmming LOW SIDE
#define S1 0// 3 hall sensors, on the first 3 pins of Uncle Ardu
#define S2 1
#define S3 2
#define H1 7// 3 digital outputs
#define H2 8
#define H3 12
#define L1 9 // 3 pwm outputs WAS 3 5 6, CHANGED TO BE ABLE TO SET PWM FREQ
#define L2 10
#define L3 11
#define GAS 0 //gas (AKA THROTTLE) potentiometer pin
#define CURRSENS 1 // current sensor pin
void setup(){
TCCR1B = TCCR1B & 0b11111000 | 0x02; // change freq for pins 9 & 10
TCCR2B = TCCR2B & 0b11111000 | 0x02; //change freq to 11 & 3, to 3.9 khz as above
pinMode(H1, OUTPUT); // the 3 highside digital output
pinMode(H2, OUTPUT);
pinMode(H3, OUTPUT);
pinMode(S1, INPUT); // the 3 hall sensor, imputs
pinMode(S2, INPUT);
pinMode(S3, INPUT);
} // If I`m not going wrong only digital pins should be configured in here
int valS1 = 0; //variables where hall states are saved, so we read them only once
int valS2 = 0;
int valS3 = 0;
int gasread; //the raw analog read from the throttle
int stadiocorrente;// the value that, basing on the halls readings, indicates the currint step of the commutation (goes from 1 to 6, with 0 being incoherent readings)
int ultimostadio;//"LAST STEP", used to see if the rotor entered in a new step since last commutation occured. Extremely useful, allows to re-write phase status only when really needed
int gasprint = 0; //gas value after some statistical math to smooth its value and after current limiting "decisions", ready to be printed on the pwm outputs
unsigned long timenow; //millis now since arudino booted
unsigned long timelastwrote;//millis since arduino booted measured when last pwm writing function was performed
void loop(){valS1=(digitalRead(S1)); // Now we get data from the 3 halls
valS2=(digitalRead(S2));
valS3=(digitalRead(S3));
//Now we find wich step are we in
if(valS1>0){ //IF S1 IS HIGH
if(valS2>0){// OK NOW AS A SUBROUTINE OF "YES S1 IS HIGH" WE EVALUATE S2, AND, IN THE SAME WAY, S3 AS A SUBROUTINE OF BOTH S2 HIGH AND S2 LOW
if(valS3>0) {stadiocorrente=2;
}
else{stadiocorrente=3;};
}
else
{ if(valS3>0) {stadiocorrente=0;///SE S1 E` ALTO, MA S2 E` BASSO.... VALUTIAMO S3
}
else {stadiocorrente=4;};
};
}
else //WELL, S1 IS LOW...
{if(valS2>0){ if(valS3>0){stadiocorrente=1;} // // OK NOW AS A SUBROUTINE OF "S1 IS LOW" WE EVALUATE S2, AND, IN THE SAME WAY, S3 AS A SUBROUTINE OF BOTH S2 HIGH AND S2 LOW
else {stadiocorrente=0;};
}
else
{if(valS3>0){stadiocorrente=6;}
else{stadiocorrente=5;};
};
};
/////////// BY NOW WE KNOW WICH STEP ARE WE IN! COOL!///////////
timenow=millis(); // we get "what time is it uncle Ardu"?
if ((stadiocorrente!=ultimostadio)or((timenow-timelastwrote)>100)){ //IF THE COMMUTATION STEP NOW DETECTED DIFFERS FROM THE ONE DETECTED ON THE LAST LOOP OR WRITING HAS NOT BEEN PERFORMED IN THE LAST .08 SECONDS WE CHANGE THE OUTPUTS, OTHERWISE WE SKIP ALL THIS BLOCK
switch (stadiocorrente) { // BASING ON THE CURRRENT STEP WE SET OUTPUTS
case 1:analogWrite(L1,0); analogWrite(L2,gasprint); analogWrite(L3,0); digitalWrite(H1,LOW); digitalWrite(H2,LOW); digitalWrite(H3,HIGH);
break;
// case 2: analogWrite(L1,0); analogWrite(L2,gasprint); analogWrite(L3,0); digitalWrite(H1,HIGH); digitalWrite(H2,LOW); digitalWrite(H3,LOW);
//break;
//case 3:analogWrite(L1,0); analogWrite(L2,0); analogWrite(L3,gasprint); digitalWrite(H1,HIGH); digitalWrite(H2,LOW); digitalWrite(H3,LOW);
//break;
// case 4:analogWrite(L1,0); analogWrite(L2,0); analogWrite(L3,gasprint); digitalWrite(H1,LOW); digitalWrite(H2,HIGH); digitalWrite(H3,LOW);
// break;
/// case 5:analogWrite(L1,gasprint); analogWrite(L2,0); analogWrite(L3,0); digitalWrite(H1,LOW); digitalWrite(H2,HIGH); digitalWrite(H3,LOW);
// break;
// case 6:analogWrite(L1,gasprint); analogWrite(L2,0); analogWrite(L3,0); digitalWrite(H1,LOW); digitalWrite(H2,LOW); digitalWrite(H3,HIGH);
// break;
default:analogWrite(L1,0); analogWrite(L2,0); analogWrite(L3,0); digitalWrite(H1,LOW); digitalWrite(H2,LOW); digitalWrite(H3,LOW); // WE KEEP IT FOR ERROR MODE, WERE THE CASE IS 0 OR SOMETHING (REALLY) WEIRD
break; }
timelastwrote=timenow; }//ok last outputs writing was... now!
/////////PART OF THE CODE THAT READS GAS SENSOR AND CURRENT SENSOR BEGINS HERE///////
//WE READ THE GAS SENSOR AND THE CURRENT SENSOR EVERY .05 SECONDS OR SO (BASING ON THE ARDU FUNCTIONS THAT GIVES MILLLISECONDS FROM BOOT),
//WE AVERAGE LAST 4 READINGS FOR THE GAS (NOT FOR THE CURRENT) AND WE DECIDE WHAT THE GASPRINT VALUE IS GONNA BE.
//THIS PART IS REALLY EASY BUT (AS REGARDS THE THROTTLE) DIFFERS A BIT DEPENDING ON WHAT POT ARE YOU USING.
gasread=(analogRead(GAS));//we read teh value from the gas pot
gasprint=100;
////END OF THE AWESOME PART OF THE CODE MENTIONED ABOVE////
ultimostadio=stadiocorrente; //LAST STEP=CURRENT STEP... WE SAVE THE STEP COMING TO THE LAST READINGS SO IN NEXT LOOP WE CAN SEE IF SOMETHING CHANGED. With this we are done with a cycle
}//end of loop, e viva la figa!! :-)[/pre]