1. Do not share user accounts! Any account that is shared by another person will be blocked and closed. This means: we will close not only the account that is shared, but also the main account of the user who uses another person's account. We have the ability to detect account sharing, so please do not try to cheat the system. This action will take place on 04/18/2023. Read all forum rules.
    Dismiss Notice
  2. 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 Now a Download Plan!
  3. 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
  4. 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. Do not follow these rules can lead to permanent exclusion from this website: Read the forum rules.
    Are you a company? Read our company 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:
    93
    Location:
    France Colomiers
    Balance:
    - 173Coins
    Ratings:
    +37 / 0 / -0
    My Motion Simulator:
    DC motor, AC motor, Arduino, Motion platform
    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++;}