1. For downloading SimTools plugins you need a Download Package. Get it with virtual coins that you receive for forum activity or Buy Download Package - We have a zero Spam tolerance so read our forum rules first.

    Buy Download Package Now!
  2. Do not try to cheat our system and do not post an unnecessary amount of useless posts only to earn credits here. We have a zero spam tolerance policy and this will cause a ban of your user account. Otherwise we wish you a pleasant stay here! Read the forum rules
  3. We have a few rules which you need to read and accept before posting anything here! Following these rules will keep the forum clean and your stay pleasant here. Do not following these rules will lead to permanent exclusion from this website: Read the forum rules.

Puzzle with arduino uno Rev3 - Led is blinking only if serial.prin is used...

Discussion in 'Miscellaneous' started by Oms, Jun 10, 2018.

  1. Oms

    Oms known as "Doc6dof"

    Joined:
    May 30, 2017
    Messages:
    60
    Location:
    France Colomiers
    Balance:
    232Coins
    Ratings:
    +21 / 0 / -0
    My Motion Simulator:
    DC motor, AC motor, Arduino, 6DOF
    Hello everybody
    I am currently develop the Arduino code for my dynamic simulator (see https://www.xsimulator.net/community/threads/launching-of-my-6dof-low-cost-dynamic-simulator.11594/ =Launching of my 6dof low cost dynamic simulator).
    I am using the timer 2 (for several counters, ...) and I have the following problem, that can simply illustrate with a simple blinking led code:
    1) In the first version, where I use print.serial to control the counter progress, the led is well blinking.
    2) In the 2nd version, where I just suppress the line "print.serial(...), the led is stoppink to blink !!!!!! I do not understand, theoricaly there is not side effect with the function print.serial, no ?
    Congratulation and thank you a lot if you find the raison, and the solution:

    1) version with "print.serial" :

    unsigned int counter_generic, toggle=0;
    void setup() {
    Serial.begin(115200);
    pinMode(LED_BUILTIN, OUTPUT);
    // timer 2 configured at 16uS (62500 Hz)
    cli(); // Unactivation of the globale interruption
    bitClear (TCCR2A, WGM20); // WGM20 = 0
    bitClear (TCCR2A, WGM21); // WGM21 = 0
    TCCR2B = 0b00000110; // Clock / 256 soit 16 uS et WGM22 = 0
    TIMSK2 = 0b00000001; // Locale interruption autorised by TOIE0
    sei(); // Activation of the globale interruption}
    void loop() {
    if (toggle == 0){
    digitalWrite (LED_BUILTIN,1);
    toggle = 1;}
    else {
    digitalWrite (LED_BUILTIN,0);
    toggle = 0;}
    // delay 1 seconds
    counter_generic=0;
    delay_1s_a:
    if (counter_generic < 1000 ){
    Serial.println(counter_generic);
    goto delay_1s_a;}
    }
    // Routine d'interruption timer 2
    ISR(TIMER2_OVF_vect)
    { TCNT2 = 256 - 62; // 62 x 16 µS = 0,992 µS, so close to 1mS
    counter_generic++;}


    2) version without "print.serial" (you can check, nothing else is changed) but the led do not want to blink...:


    unsigned int counter_generic, toggle=0;
    void setup(){
    Serial.begin(115200);
    pinMode(LED_BUILTIN, OUTPUT);

    // timer 2 configured at 16uS (62500 Hz)
    cli(); // Unactivation of the globale interruption
    bitClear (TCCR2A, WGM20); // WGM20 = 0
    bitClear (TCCR2A, WGM21); // WGM21 = 0
    TCCR2B = 0b00000110; // Clock / 256 soit 16 uS et WGM22 = 0
    TIMSK2 = 0b00000001; // Locale interruption autorised by TOIE0
    sei();} // Activation of the globale interruption}
    void loop(){
    if ( toggle == 0){
    digitalWrite (LED_BUILTIN,1);
    toggle = 1;}
    else {
    digitalWrite (LED_BUILTIN,0);
    toggle = 0;}
    // delay 1 seconds
    counter_generic=0;
    delay_1s_a:
    if (counter_generic < 1000 ){
    // Serial.println(counter_generic);
    goto delay_1s_a;}}
    // Routine d'interruption timer 2
    ISR(TIMER2_OVF_vect)
    { TCNT2 = 256 - 62; // 62 x 16 µS = 0,992 µS, so close to 1mS
    counter_generic++;}