help with atachinterrupt on teensy 3.5

Status
Not open for further replies.
hi i'm having some problems using interruptions on gpio pins, i want to control the level of a digital potentiometer, for now i just want that when i press a button the interrupt occurs, but my code is just not working. can some one please help me with this, here is my code
#include <SPI.h>
#include <stdio.h>
char str[100];
// set pin 10 as the slave select for the digital pot:
const int slave_Select_Pin = 31;
volatile int level1 = 255;
volatile int level2 = 255;
const int BUTTON1 = 2;
volatile boolean flagB1;

void setup() {
// put your setup code here, to run once:
pinMode (slave_Select_Pin, OUTPUT);
Serial.begin(9600);
SPI1.setMOSI(21);
// initialize SPI:
SPI1.begin();
MSP42010PotWrite(slave_Select_Pin, B00010001, level1);
MSP42010PotWrite(slave_Select_Pin, B00010010, level2);
pinMode(BUTTON1, INPUT);
attachInterrupt (BUTTON1, isrButton1, RISING);

}

void loop() {
// put your main code here, to run repeatedly:
if (flagB1 == true)
{
Serial.println("BUTTON 1 interrupt has occurred");
flagB1=false;
}

}
void MSP42010PotWrite(int slaveSelectPin, byte address, int value) {
// take the SS pin low to select the chip:
digitalWrite(slaveSelectPin,LOW);
// send in the address and value via SPI:
SPI1.transfer(address);
SPI1.transfer(value);
// take the SS pin high to de-select the chip:
digitalWrite(slaveSelectPin,HIGH);
}
void isrButton1 ()
{
flagB1 = true;
}
 
You haven't said what you mean by "is just not working" but when I tried your code and connected a wire to pin 2, the serial monitor printed "BUTTON 1 interrupt has occurred" dozens of times. That's because pin2 was floating. Set the pullup on that pin and it should behave:
Code:
pinMode(BUTTON1, INPUT_PULLUP);

If there are any other problems, a bit of detail would help.

Pete
 
You haven't said what you mean by "is just not working" but when I tried your code and connected a wire to pin 2, the serial monitor printed "BUTTON 1 interrupt has occurred" dozens of times. That's because pin2 was floating. Set the pullup on that pin and it should behave:
Code:
pinMode(BUTTON1, INPUT_PULLUP);

If there are any other problems, a bit of detail would help.

Pete
thanks for reply i mean when i try it the interrupt doesn't seem to occur because there is nothing printed in the serial terminal, i'm connecting a button to the pin 2 when i press it there is 4V in the pin 2,and when it is not press there is 0v.so i wonder if i have to configure the pin in pull up ?
 
The normal way is to connect the button between the pin and GND, so when you press the button the voltage goes low. I know that may seem backwards, but it the most common approach.

If you have connected the button between the pin and 3.3V, then configure the pin with pinMode(BUTTON1, INPUT_PULLDOWN);

You should also consider using the Bounce library rather than attachInterrupt to read the pin. Bounce automatically deals with mechanical chatter. To see an example, click File > Examples > Teensy > USB_Keyboard > Buttons. Remember, this example (and nearly all others) expects the buttons to connect between the pin and GND.
 
the button is just for test it i'm gonna replace it with a touch sensor that gives me 5 volts when i touch it and 0 volts when not so how do i have to configure the pin ? in pull_down ? i thought i have to configure it just like an input, thanks for help
 
so how do i have to configure the pin ? in pull_down ? i thought i have to configure it just like an input, thanks for help

The answer depends on how you connected the circuitry, whether you used a real resistor. But we can't see your circuit. You gave just barely enough info in msg #3 to guess it was wired in the opposite polarity of the normal button to GND.

You can get pretty good help here, but we're limited in how well we can answer your questions when we don't know what you're doing.
 
Status
Not open for further replies.
Back
Top