please guys can you help me with increassing speed of pulssing? i want to regulate the speed of pulsing of 2 outputs with variable "freq". itss too sslow
int u;
float ampl = 0;
int krok = 25;
int count =0;
unsigned long aktmillis;
unsigned long predmillis = 0;
void setup() {
TCCR0B = TCCR0B & B11111000 | B00000010;
//B00000010; 7.8kHz
//B00000011; 980Hz
Serial.begin(9600);
pinMode(u,OUTPUT);
digitalWrite(u,LOW);
}
void loop() {
aktmillis = millis();
Please use code tags for your code.. Do not use serial communication (Serial.println) in time critical code. The calculation "count%2 ==0" is slow, use "count & 1 == 0" instead.
% 2 will be true whenever a number is even (0, 2, 4, 6 and so on). & 1 will do the same (by testing if bit 0 is off) with less clock cycles, thus it is faster. Please use code tags, ppl in here may disregard your question if not.
You also do not assign anything to the variable 'u' so it gets initialized to 0 which means you are setting pin0 as an output. Then, in your loop() you assign 'u' to 5 or 6, neither of which has been initialized as an OUTPUT
so, count is 0,when ampl starts from 0(count is 1) to 250 than back to 0(count is 2) than count is resseted to 0, pin is sswitched from 5 to 6 and ampl do the same. To blh64, my thought was to sswitch pins, i declared variable u and as output but in "if" condition when count is resseted u will be set to 5 or 6.So i changed it and set it to 5 and 6 at the beggining and looks like its working faster ssso thanks:) but while one otput i pulssing, the second looks like has some sswitching through it :-/
int u1 =5;
int u2 =6;
float ampl = 0;
float krok = 25.5;
int count =0;
unsigned long aktmillis;
unsigned long predmillis = 0;
void setup() {
TCCR0B = TCCR0B & B11111000 | B00000010;
//B00000010; 7.8kHz
//B00000011; 980Hz //Serial.begin(9600);
pinMode(u1,OUTPUT);
pinMode(u2,OUTPUT);
digitalWrite(u1,LOW);
digitalWrite(u2,LOW);
}
void loop() {
aktmillis = millis();
float pot = analogRead(A0);
float freq = map(pot,0,1023,10,200); // changing frequency of pulsing
if(aktmillis - predmillis >=freq) //basic millis condition
{analogWrite(u1,ampl);
analogWrite(u2,ampl);
ampl = ampl + krok; //width of pwm for pulsing
if(ampl==255.0 || ampl==0.0)krok = -krok; //change of direction of incressing pwm
if(ampl == 0.0)count++; //when ampl goess from 0 to 250 and back to 0,count will be 2
else;
if(count%2 ==0){count =0;digitalWrite(u1,LOW);}//when count =2 switch outputs,and resset count to 0
else{digitalWrite(u2,LOW);};
predmillis = aktmillis;} //Serial.println(aktmillis);
}
It is unlikely that ampl will be exactly 255.0 or 0.0 It would be better to allow some slop:
if (ampl >= 254.99 || ampl <= 0.01)
analogWrite(pin, value) takes an integer value. analogRead() returns an integer value. map() takes and returns integers. I recommend you turn all of your float values to integers. The only reason you currently need float values is so you can use an increment of 25.5. If you double your PWM values (0-510)then you can use the integer increment 51. Divide the value by 2 when you pass it to analogWrite().
const int u1 = 5;
const int u2 = 6;
int ampl = 0;
int krok = 51;
void setup()
{
TCCR0B = (TCCR0B & B11111000) | B00000010;
//B00000010; 7.8kHz
//B00000011; 980Hz
//Serial.begin(9600);
pinMode(u1, OUTPUT);
pinMode(u2, OUTPUT);
digitalWrite(u1, LOW);
digitalWrite(u2, LOW);
}
void loop()
{
static unsigned long predmillis = 0;
unsigned int pot = analogRead(A0);
unsigned int freq = map(pot, 0, 1023, 10, 200); // changing frequency of pulsing
unsigned long aktmillis = millis();
if (aktmillis - predmillis >= freq) //basic millis condition
{
predmillis += freq;
analogWrite(u1, ampl / 2);
analogWrite(u2, ampl / 2);
ampl = ampl + krok; //width of pwm for pulsing
if (ampl >= 510)
{
krok = -krok; //change of direction of incressing pwm
ampl = 510;
}
if (ampl <= 0)
{
krok = -krok; //change of direction of incressing pwm
ampl = 0;
}
// All of that 'count' stuff is useless since it just sets the output
// to LOW when ampl is zero. But when ampl is 0 the output will be set
// to LOW at the top of the next loop (in a few microseconds)
}
}