attachInterrupt not working

Status
Not open for further replies.

Markus_L811

Well-known member
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:
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.
 
Now with DS18B20 One-Wire-Slave inside

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.
 

Attachments

  • OneWireSlave.cpp
    19.1 KB · Views: 173
  • OneWireSlave.h
    5.2 KB · Views: 208
Last edited:
Some new sketches

Some new Sketches to play with the 1-Wire Slave

1nd Analogread for temper

2st Serial in for temper
 

Attachments

  • DS18B20_Slave_Interrupt_T3_analogread.ino
    2.5 KB · Views: 122
  • DS18B20_Slave_Interrupt_T3_Serial.ino
    3 KB · Views: 117
Last edited:
Status
Not open for further replies.
Back
Top