Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: attachInterrupt not working

  1. #1
    Senior Member
    Join Date
    Apr 2013
    Posts
    163

    Now with DS18B20 One-Wire-Slave Sample inside [attachInterrupt not working: solved]

    Hello,

    I tried following sketch on the Teensy 3

    Code:
    int pin = 13;
    volatile int state = LOW;
    volatile unsigned long previousMillis = 0;
    volatile unsigned long old_previousMillis = 0;
    volatile unsigned long difference = 0;
    
    void setup() {
      Serial.begin(9600);
      pinMode(pin, OUTPUT);
      attachInterrupt(12, blink, CHANGE);
    }
    
    void loop() {
      digitalWrite(pin, state);
      if (difference >= 100 && difference <= 1500)
        Serial.println(difference);
    }
    
    void blink() {
      old_previousMillis = previousMillis;
      previousMillis = micros();
      difference = previousMillis - old_previousMillis;
      state = !state;
    }
    To look at the Onewire timing but I get nothing on the T3, the Onewire Master runs on an second T3 and drives a net with 2 DS18B20 and worked with them well.

    Grounds (GND) connected btw. The Master is connected with my PC and has the DS18x20 running from Paul, the other T3 with the sketch above runs with seperate power over the USB connector.
    Last edited by Markus_L811; 06-21-2013 at 07:14 PM.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,076
    Try adding pinMode(12, INPUT);

    On Teensy3, the digital pins default to disable, not inputs. You need to use pinMode to make the pin act as an input.

  3. #3
    Senior Member
    Join Date
    Apr 2013
    Posts
    163

    Now with DS18B20 One-Wire-Slave inside

    Quote Originally Posted by PaulStoffregen View Post
    Try adding pinMode(12, INPUT);

    On Teensy3, the digital pins default to disable, not inputs. You need to use pinMode to make the pin act as an input.
    Thanks Paul good to know, I wish I know that befor, man that freaked me out but now there is it DS18B20 slavemode for the T3.

    This is the Sketch for the T3 and the libs are in the attachment.

    Code:
    #include <OneWireSlave.h>
    
    //                     {Fami, <---, ----, ----, ID--, ----, --->,  CRC} 
    unsigned char rom[8] = {0x28, 0xAD, 0xDA, 0xCE, 0x0F, 0x00, 0x11, 0x00};
    //                            {TLSB, TMSB, THRH, TLRL, Conf, 0xFF, Rese, 0x10,  CRC}
    unsigned char scratchpad[9] = {0x7F, 0x09, 0x4B, 0x46, 0x7F, 0xFF, 0x00, 0x10, 0x00};
    //                                       {TLSB, TMSB}
    unsigned char scratchpadtemperature[2] = {0x7F, 0x09};
    
    OneWireSlave ds(12);
    
    //Interrupt
    volatile long previousmicros = 0;
    volatile long old_previousmicros = 0;
    volatile long difference = 0;
    
    //Blinking
    const int ledPin =  13;
    int ledState = LOW;             // ledState used to set the LED
    long previousMillis = 0;        // will store last time LED was updated
    long interval = 750;            // interval at which to blink (milliseconds)
    
    void setup() {
      Serial.begin(9600);
      pinMode(ledPin, OUTPUT);
      pinMode(12, INPUT);
      ds.init(rom);
      ds.setScratchpad(scratchpad);
      ds.setPower(PARASITE);
      ds.setResolution(9);
      attachInterrupt(12, DS18B20, CHANGE);
      ds.attach44h(temper);
    }
    
    void loop() {
      blinking();
      //ds.waitForRequest(false);
    }
    
    void temper() {
      scratchpadtemperature[0] = scratchpadtemperature[0] + 1;
      ds.setTemperature(scratchpadtemperature);
    }
    
    void DS18B20() {
      old_previousmicros = previousmicros;
      previousmicros = micros();
      difference = previousmicros - old_previousmicros;
      if (difference >= 330 && difference <= 500) {
        ds.waitForRequestInterrupt(false);
      }
    }
    
    void blinking() {
      unsigned long currentMillis = millis(); 
      if(currentMillis - previousMillis > interval) {
        previousMillis = currentMillis;
        ledState = !ledState;
        digitalWrite(ledPin, ledState);
      }
    }
    If I have more time next, I will make it smarter the next days, wishes welcome.
    Attached Files Attached Files
    Last edited by Markus_L811; 06-21-2013 at 07:13 PM.

  4. #4
    Senior Member
    Join Date
    Apr 2013
    Posts
    163

    Some new sketches

    Some new Sketches to play with the 1-Wire Slave

    1nd Analogread for temper

    2st Serial in for temper
    Attached Files Attached Files
    Last edited by Markus_L811; 06-25-2013 at 10:42 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •