Go Down

Topic: interrupt/counter  (Read 362 times) previous topic - next topic

LandonW

Im using a TEENSY 3.2
im attempting to use the interrupt function (I've never done that before)

my interrupt is to read the falling edge. I want to use the interrupt to return a value back to the loop.
The value is used for setting a PWM output.....
I'm getting some error codes I have never seen.

CODE
Code: [Select]

const byte dimmerSwitch = 9;    // temporary switch for counter. counter is pwm for led dimmer
byte dimmerCount;               // track button press
int dimmerValue;                // set PWM with counter 0-3
int Dim;
byte ledRyellow = 4;            // yellow LED right side
byte ledLyellow = 5;            // yellow LED left side

void setup () {
  pinMode(dimmerSwitch, INPUT_PULLUP);
  pinMode(ledLyellow, OUTPUT);
  pinMode(ledLyellow, OUTPUT);
  dimmerCount = 1;

  attachInterrupt(digitalPinToInterrupt(dimmerSwitch), Dim, FALLING);
}

void loop () {
  if (dimmerCount == 3) {
    dimmerCount = 0;
  }
  if (dimmerCount == 0) {
    dimmerValue = 40;
  }
  if (dimmerCount == 1) {
    dimmerValue = 115;
  }
  if (dimmerCount == 2) {
    dimmerValue = 255;
  }

  analogWrite(ledRyellow, dimmerValue);
  analogWrite(ledRyellow, dimmerValue);
  delay(160);
}

void return Dim () {
  dimmerCount++;
}



Error Code

Code: [Select]

Arduino: 1.8.5 (Windows 10), TD: 1.41, Board: "Teensy 3.2 / 3.1, Serial, 72 MHz, Faster, US English"

dimmer_interupt_test: In function 'void setup()':
dimmer_interupt_test:14: error: invalid conversion from 'int' to 'void (*)()' [-fpermissive]
   attachInterrupt(digitalPinToInterrupt(dimmerSwitch), Dim, FALLING);

                                                                    ^

In file included from F:\Arduino\hardware\teensy\avr\cores\teensy3/wiring.h:38:0,

                 from F:\Arduino\hardware\teensy\avr\cores\teensy3/WProgram.h:45,

                 from F:\Arduino\hardware\teensy\avr\cores\teensy3/Arduino.h:3,

                 from C:\Users\Landon\AppData\Local\Temp\arduino_build_182678\sketch\dimmer_interupt_test.ino.cpp:1:

F:\Arduino\hardware\teensy\avr\cores\teensy3/core_pins.h:1931:6: note:   initializing argument 2 of 'void attachInterrupt(uint8_t, void (*)(), int)'

 void attachInterrupt(uint8_t pin, void (*function)(void), int mode);

      ^

G:\arduino\dimmer_interupt_test\dimmer_interupt_test.ino: At global scope:

dimmer_interupt_test:36: error: expected unqualified-id before 'return'
 void return Dim () {

      ^

invalid conversion from 'int' to 'void (*)()' [-fpermissive]

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.



if I could get some guidance on this I would appreciate it, thank you

PaulS

The second argument to attachInterrupt() is the function to be called when the interrupt happens.
The art of getting good answers lies in asking good questions.

LandonW

so I need something like

void dimmerSwitch(){
dimmerCount++;     }

if not could you give an example if you are able to?  the arduino example doesnt quite fit for my application i dont think

Robin2

so I need something like

void dimmerSwitch(){
dimmerCount++;     }
Nearly, but not quite

Your attachInterrupt() line is
Code: [Select]
attachInterrupt(digitalPinToInterrupt(dimmerSwitch), Dim, FALLING);
which means that it is looking for an Interrupt Service Routine called Dim and your code should be
Code: [Select]
void Dim(){
     dimmerCount++;     
}


However IMHO dimmerSwitchISR() would be a more meaningful name

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

LandonW

I guess I'm a special needs kind of person.  I apologize, interrupts are new to me.

this is what i have now

ERROR CODE
Code: [Select]

Arduino: 1.8.5 (Windows 10), TD: 1.41, Board: "Teensy 3.2 / 3.1, Serial, 72 MHz, Faster, US English"

SIX:65: error: 'void Dim()' redeclared as different kind of symbol
 void Dim () {

          ^

G:\arduino\SIX\SIX.ino:8:15: note: previous declaration 'volatile byte Dim'

 volatile byte Dim;               // interupt name

               ^

SIX: In function 'void setup()':
SIX:33: error: invalid conversion from 'byte {aka unsigned char}' to 'void (*)()' [-fpermissive]
   attachInterrupt(digitalPinToInterrupt(dimmerSwitchISR), Dim, FALLING);

                                                                       ^

In file included from F:\Arduino\hardware\teensy\avr\cores\teensy3/wiring.h:38:0,

                 from F:\Arduino\hardware\teensy\avr\cores\teensy3/WProgram.h:45,

                 from F:\Arduino\hardware\teensy\avr\cores\teensy3/Arduino.h:3,

                 from C:\Users\Landon\AppData\Local\Temp\arduino_build_402341\sketch\SIX.ino.cpp:1:

F:\Arduino\hardware\teensy\avr\cores\teensy3/core_pins.h:1931:6: note:   initializing argument 2 of 'void attachInterrupt(uint8_t, void (*)(), int)'

 void attachInterrupt(uint8_t pin, void (*function)(void), int mode);

      ^

SIX: In function 'void Dim()':
SIX:65: error: 'void Dim()' redeclared as different kind of symbol
 void Dim () {

           ^

G:\arduino\SIX\SIX.ino:8:15: note: previous declaration 'volatile byte Dim'

 volatile byte Dim;               // interupt name

               ^

'void Dim()' redeclared as different kind of symbol

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.



PROGRAM CODE
Code: [Select]

volatile byte dimmerSwitchISR = 9;    // temporary switch for counter. counter is pwm for led dimmer
byte dimmerCount;               // track button press
int dimmerValue;                // set PWM with counter 0-3
volatile byte Dim;               // interupt name
byte ledRred = 3;               // red LED right side
byte ledRyellow = 4;            // yellow LED right side
byte ledLyellow = 5;            // yellow LED left side
byte ledLred = 6;               // red LED left side
const byte irA = 21;            // middle sensor, address selection
const byte irB = 22;            // right side sensor, address selection
const byte irC = 23;            // left side sensor, address selection
int battSensor = A0;            // read battery voltage, pin 14 on teensy
int hotPixleValue;              // amount of pixles from sensor that are detecting heat


void setup() {
  // put your setup code here, to run once:

  // pinMode(dimmerSwitchISR,INPUT_PULLUP);
  pinMode(ledRred, OUTPUT);
  pinMode(ledRyellow, OUTPUT);
  pinMode(ledLyellow, OUTPUT);
  pinMode(ledLred, OUTPUT);
  pinMode(irA, OUTPUT);
  pinMode(irB, OUTPUT);
  pinMode(irC, OUTPUT);
  dimmerCount = 1;

  attachInterrupt(digitalPinToInterrupt(dimmerSwitchISR), Dim, FALLING);
}

void loop() {
  // put your main code here, to run repeatedly:


  /*if (digitalRead(dimmerSwitch) == LOW){
      //dimmerCount++;
    }*/
  if (dimmerCount == 3) {
    dimmerCount = 0;
  }
  if (dimmerCount == 0) {
    dimmerValue = 40;
  }
  if (dimmerCount == 1) {
    dimmerValue = 115;
  }
  if (dimmerCount == 2) {
    dimmerValue = 255;
  }

  analogWrite(ledLyellow, dimmerValue);
  analogWrite(ledLred, dimmerValue);
  analogWrite(ledRred, dimmerValue);
  analogWrite(ledRyellow, dimmerValue);
  delay(160);


}

void Dim () {
  dimmerCount++;
}


TolpuddleSartre

You've got function and a variable called "Dim"

LandonW

You've got function and a variable called "Dim"
Ah,  thank you. I commented out the variable and it compiled.  I was probably doing something wrong but I recall the error codes saying it that "Dim" wasn't named... that's why I listed it as a variable.

Thank you all very much, I learned something new today

LandonW

Does the "Dim" function need to return the dimmerCount to the "loop" function?

when I push the button the LED's don't seem to react

TolpuddleSartre

The Dim function is an interrupt service routine - it can't take arguments, or return values.

LandonW

The Dim function is an interrupt service routine - it can't take arguments, or return values.
i guess i went about doing this wrong...
any suggestions on how i a can accomplish this task???

TolpuddleSartre

Code: [Select]
volatile byte dimmerSwitchISR = 9;    // temporary switch for counter. counter is pwm for led dimmer
byte dimmerCount;           
I don't know what a teensy is, but one of those should be volatile, and the other one shouldn't.

LandonW

Code: [Select]

const byte dimmerSwitchISR = 9;    // temporary switch for counter. counter is pwm for led dimmer
volatile byte dimmerCount;               // track button press
byte dimmerValue;                // set PWM with counter 0-3



a teensy is like Arduino but better.  it uses the Arduino IDE and language but its way faster and the pin out capabilities are far more flexible.


the reason Im using one is because eventually this program will be processing a AMG8833 sensor.

cattledog

Did the declaration of volatile byte dimmerCount fix your problem? If not, please post your current code.

LandonW

no, it didn't seem to fix the issue.

here is my full code as requested
Code: [Select]

/* infra red proximity detector by, Landon Williams
  teensy 3.2  and   AMG8833
*/

const byte dimmerSwitchISR = 9;    // temporary switch for counter. counter is pwm for led dimmer
volatile byte dimmerCount;               // track button press
byte dimmerValue;                // set PWM with counter 0-3
//volatile byte Dim;               // interupt name
byte ledRred = 3;               // red LED right side
byte ledRyellow = 4;            // yellow LED right side
byte ledLyellow = 5;            // yellow LED left side
byte ledLred = 6;               // red LED left side
const byte irA = 21;            // middle sensor, address selection
const byte irB = 22;            // right side sensor, address selection
const byte irC = 23;            // left side sensor, address selection
int battSensor = A0;            // read battery voltage, pin 14 on teensy


void setup() {
  // put your setup code here, to run once:

  // pinMode(dimmerSwitchISR,INPUT_PULLUP);

  pinMode(ledRred, OUTPUT);
  pinMode(ledRyellow, OUTPUT);
  pinMode(ledLyellow, OUTPUT);
  pinMode(ledLred, OUTPUT);
  pinMode(irA, OUTPUT);
  pinMode(irB, OUTPUT);
  pinMode(irC, OUTPUT);
  dimmerCount = 1;

  attachInterrupt(digitalPinToInterrupt(dimmerSwitchISR), Dim, FALLING);
}

void loop() {
  // put your main code here, to run repeatedly:


  /*if (digitalRead(dimmerSwitch) == LOW){
      //dimmerCount++;
    }*/
  if (dimmerCount == 3) {
    dimmerCount = 0;
  }
  if (dimmerCount == 0) {
    dimmerValue = 40;
  }
  if (dimmerCount == 1) {
    dimmerValue = 115;
  }
  if (dimmerCount == 2) {
    dimmerValue = 255;
  }

  analogWrite(ledLyellow, dimmerValue);
  analogWrite(ledLred, dimmerValue);
  analogWrite(ledRred, dimmerValue);
  analogWrite(ledRyellow, dimmerValue);
  delay(160);


}

void Dim () {
   dimmerCount++;
}


ChrisTenone

#14
May 27, 2018, 06:18 am Last Edit: May 27, 2018, 07:16 am by ChrisTenone Reason: *Now Featuring* - tested node
Hi LandonW,

You are using a Teensy 3.2, which uses a Cortex M4 processor, so the way you set and handle interrupts may be different. None-the-less, here is an example of how to use an interrupt. This example expects an Arduino Uno, but you can try it on your board.

First connect a switch between pin 2 and ground. Hook your board to a serial port on your computer, and load the following sketch:

Code: [Select]

const byte inputPin = 2; // <-- switch <-- gnd
const byte outputPin = 13;  // --> builtin LED

volatile boolean inputState = false;
boolean lastInputState = false;

long count = 0L;

void setInputState() {
  inputState = digitalRead(inputPin);
}

void setup() {
  pinMode(inputPin, INPUT_PULLUP);
  pinMode(outputPin, OUTPUT);

  Serial.begin(115200);

  attachInterrupt(digitalPinToInterrupt(inputPin), setInputState, CHANGE);
}

void loop() {
  if (inputState != lastInputState) {
    digitalWrite(outputPin, inputState);
    lastInputState = inputState;

    Serial.println(count++);
  }
}


The built-in LED will turn on and off according to the switch, and the total number of times the switch was thrown will be shown on the serial monitor. The only thing the ISR does is set a variable high or low, depending on the state of the input pin. Everything else happens in the loop() function.
Atmosphere carries combustion vapors to places where they will do good instead of harm - Mike Faraday's 'History of a Candle': https://www.youtube.com/watch?v=6W0MHZ4jb4A

Whoops ::)

Go Up