delay problem

hi all,

I am the first time user for Arduino and first time for C++ knowledge. My Arduino board is OSEPP Uno R3 Plus with atmega328P processor. I have the problem with my code that I try to reading the voltage at Pin A1, converting, and write the PWM signal to Pin3 right after Pin5 goes high. But, the delay waveform from Pin5 to Pin3 is almost 20ms when I probe with scope. What I want is the delay can't longer than 2ms. If anyone knows the problem, please help. I appreciate your help very much, see my code below:

int Pin5 = 5;
int Pin3 = 3;
int ultraPwmValue = 0;
int ultraValue = 0;

void setup() {                
  
  pinMode(Pin5, OUTPUT);
  pinMode(Pin3, OUTPUT);

 }



void loop() {
 
  TCCR2B = TCCR2B & 0b11111000 | 0x07;    // set up the timer2 to 30Hz clock    
  digitalWrite(Pin5, HIGH);
  ultraValue = analogRead(A1); 
  ultraPwmValue = map(ultraValue, 0, 1023, 235, 10);  
  analogWrite(Pin3, ultraPwmValue);
                     
}

Changing Timer2 could be causing a problem. Try commenting out that line and testing how long it takes.

analogRead() is only good for 8000 read/sec. You also have overhead for the digitalWrite() and digitalRead().

The map is bad and very slowwwwww try shift 2 right or a simple divide by 4.

What are you using the PWM output for?

Mark

Not sure what you are trying to do but how about rearranging the code a bit?

  TCCR2B = TCCR2B & 0b11111000 | 0x07;    // set up the timer2 to 30Hz clock    
  ultraValue = analogRead(A1); 
  ultraPwmValue = map(ultraValue, 0, 1023, 235, 10);  
  digitalWrite(Pin5, HIGH);
  analogWrite(Pin3, ultraPwmValue);

Forgot to add that your using a 10 bit analogRead() and then converting it to 8 bits. However the arduinos ADC does support an 8 bit analog read ( its not a standard function in the IDE) which would cut out the conversion.

Mark

hi all,

thanks for fast response.

Changing Timer2 could be causing a problem. Try commenting out that line and testing how long it takes.

yes, I did tried comment timer2 out before I posted my question. Without changing timer2 to 30hz (by default, it run at 500hz), I see the day 2ms instead of 20ms. I thought it related to the timer2, but one of the replier said the timer should not change the speed of the system (which is 16Mhz clock). I am confusing, it did not matter what I change the order of the code or tried many tricks, the delay always there unless I turn off the timer2. However, I need the clock of PWM Pin3 at 30hz to generate 30ms on and 3ms off based on the analog reading at Pin A1.

analogRead() is only good for 8000 read/sec. You also have overhead for the digitalWrite() and digitalRead().

The map is bad and very slowwwwww try shift 2 right or a simple divide by 4.

What are you using the PWM output for?

If I map(readValue, 0, 1023, 0, 255). How many read cycles to finish the "map" instruction?

Since I don't know much the C++ syntax, if you can send me some tutorial how to use shift right 2 instead of map function or send me some example code, it'd be big help for me.

I use it to control the LED brightness.

Forgot to add that your using a 10 bit analogRead() and then converting it to 8 bits. However the arduinos ADC does support an 8 bit analog read ( its not a standard function in the IDE) which would cut out the conversion.

how can I use 8 bit instead of 10 bit?

Look in the datasheet for all the was the ADC can be used and in the reference section of this site for the shift operators.

Mark

sonh:
the delay waveform from Pin5 to Pin3 is almost 20ms when I probe with scope.

Do you have any idea how fast your loop() is running? I guess the delay you're referring to is the delay between a change to the input appearing on the output, and the latency here will depend on the sampling interval as well as the output timing.

It sounds as if you have use of a scope, so I suggest adding a line of code to toggle an output pin so you can see what frequency you're achieving.

Also, do you really need to set timer2 frequency every time round loop?

hi all,

thank you all for your kind response. I found another way to implement this issue already. Thanks again for your inputs.

Also, do you really need to set timer2 frequency every time round loop?

No, I don’t. But I just put it there for the quick experiment. I moved it to the void set up (), but it made no different.

Happy New Year to All !!!

hi all,

I read the atmega328P datasheet and its documentation and I see the clocks definition. I have a question as follow:

  1. CPU clock
  2. I/O clock
  3. asynchronous clock

I wonder whether these clocks can be configured to run independently and with different frequency. Do you think so? if yes, how can I set these clocks.

thanks