Pages: [1]   Go Down
Author Topic: Using interrupts  (Read 1185 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wrote a little Sketch for an RGB LED. I use 4 Buttons, 1 to reset the values of the 3 colors. Compiling does not show any error, but after the Reset of the board all colors are full brightness.I triggered the reset interrupt even with wires, leaving the button out. But nothing happens.
This is the code:
int red=0;
int green=0;
int blue=0;
boolean changed=true;

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
pinMode(50, INPUT);
pinMode(48, INPUT);
pinMode(46, INPUT);
pinMode(44, INPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
attachInterrupt(95, resetto0, CHANGE);
attachInterrupt(48, incrred, CHANGE);
attachInterrupt(46, incrgreen, CHANGE);
attachInterrupt(44, incrblue, CHANGE);
}

void loop() {
  while (1) {
  Serial.write("START");
  if ( changed=true)
  {
    analogWrite(7, 0);
    analogWrite(6, 0);
    analogWrite(5, 0);
    changed=false;
    }
    Serial.write("WAIT");
    }
  // put your main code here, to run repeatedly:
 
}
void resetto0()
{
  red=0;
  green=0;
  blue=0;
  changed=true;
  Serial.write('RESET');
  delay(25);
  }
void incrred()
{
  red++;
  changed=true;
  Serial.write('RED');
  delay(25);
  }
void incrgreen()
{
   green++;
   changed=true;
   Serial.write('GREEN');
   delay(25);
   }
void incrblue()
{
   blue++;
   changed=true;
   Serial.write('BLUE');
   delay(25);
   }
   
Any idea?
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First of all, don't use "delay()" and "Serial.write("")" commands in interrupt routines.. these commands are known to cause problems in interrupt service routines. Try to find a way around!

Secondly you define pin 50 as INPUT but you define your interrupt for pin 95.. I guess you want the interrupt to be on pin 50?

Maybe one of the things solves your problem smiley-wink
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, thanx. This code has undergone many changes this evening. Of course it has and was pin 50. This mistake happened during changing the code over and over.
This serialstuff is just for debugging purposes. The original is of course without that serial rubbish. But the behavior was the same, all the time.
I also put all Interuptfunctions between setup and loop.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the original code. I didnt save the other stuff.

int red=0;
int green=0;
int blue=0;
boolean changed=true;

void setup() {
  // put your setup code here, to run once:
pinMode(50, INPUT);
pinMode(48, INPUT);
pinMode(46, INPUT);
pinMode(44, INPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
attachInterrupt(50, resetto0, RISING);
attachInterrupt(48, incrred, RISING);
attachInterrupt(46, incrgreen, RISING);
attachInterrupt(44, incrblue, RISING);
}
void resetto0() {
  red=0;
  green=0;
  blue=0;
  changed=true;
  delay(25);
  }
void incrred() {
  red++;
  changed=true;
  delay(25);
  }
void incrgreen() {
   green++;
   changed=true;
   delay(25);
   }
void incrblue() {
   blue++;
   changed=true;
   delay(25);
   }
   
void loop() {
  if ( changed=true)
  {
    analogWrite(7, red);
    analogWrite(6, green);
    analogWrite(5, blue);
    changed=false;
    }
   
  // put your main code here, to run repeatedly:
 
}
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

as I said.. remove the "delay(25);" commands and try again
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow, thank you! That really helped somehow. Its doing some weared stuff, when I touch the wires, but something has changed definitly.
I think I have to pull down the open end (when unpressed) with a resistor to GND. Now I FEEL the meaning of "PULL DOWN RESISTOR" LOL.
I know there is a function "debounce" on ATMEL MEGA boards for the little switches. Thats why I used the delay stuff.
Anyways, I have to find aother solution for that debounce...
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure the "delay(25)" command would help for debouncing anyways, since I think the interrupt can be retriggered while handling the service routine. So you're not avoiding multiple interrupt triggering.

However there is a simple solution for debouncing switches using the hardware debounce filter of the DUE:
http://forum.arduino.cc/index.php/topic,156474.0.html (I think its the third time this week I'm posting this link  smiley-lol)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanx a lot. I will analyse and try tomorrow.
I just started this week after 15 years out of electronic stuff. I'm into IT stuff, so my main focus was on the programming stuff.
After all went fine with the ATMEGA on wTuesday and wednesday I thought its time for the ARM.
Another possibility to avoid the triggering could be a a cicuit with 2 resistors and 1 capacitor. ut if it works with software I will be fine with that, since it saves space on the breadboard smiley-grin
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I tried today. Works perfect. Thanx a lot. I can step by 1 for each color. I'm quiet happy with that.
I changed from int to byte with the variables for the colors. But I'm quiet surprised, that the IF Statement does not work well:
//Start of foreign Code
// input pin to debounce, pin 24 is on PORT A

// get bitmask for register manipulation
int mask_1 = digitalPinToBitMask(50);
int mask_2 = digitalPinToBitMask(48);
int mask_3 = digitalPinToBitMask(46);
int mask_4 = digitalPinToBitMask(44);


 
  // activate input filters for pin 24
//  REG_PIOA_IFER = mask_1;
  // choose debounce filter as input filter for pin 24
//  REG_PIOA_DIFSR = mask_1;
  // set clock divider for slow clock -> rejects pulses shorter than (DIV+1)*31µs and accepts pulses longer than 2*(DIV+1)*31µs
//  REG_PIOA_SCDR = 1000;


// End of foreign Code




byte red=0;
byte green=0;
byte blue=0;
boolean changed=false;

void setup() {
  // put your setup code here, to run once:
  // The INPUTS are on Port C
pinMode(50, INPUT);
pinMode(48, INPUT);
pinMode(46, INPUT);
pinMode(44, INPUT);
REG_PIOC_IFER = mask_1;
REG_PIOC_DIFSR = mask_1;
REG_PIOC_SCDR = 1000;
REG_PIOC_IFER = mask_2;
REG_PIOC_DIFSR = mask_2;
REG_PIOC_SCDR = 1000;
REG_PIOC_IFER = mask_3;
REG_PIOC_DIFSR = mask_3;
REG_PIOC_SCDR = 1000;
REG_PIOC_IFER = mask_4;
REG_PIOC_DIFSR = mask_4;
REG_PIOC_SCDR = 1000;



pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);

attachInterrupt(50, resetto0, RISING);
    attachInterrupt(48, incrred, RISING);
attachInterrupt(46, incrgreen, RISING);
attachInterrupt(44, incrblue, RISING);
}
void resetto0() {
  red=0;
  green=0;
  blue=0;
  changed=true;
  }
  \
void incrred() {
    red++;
  changed=true;
  }
void incrgreen() {
   green++;
   changed=true;
   }
void incrblue() {
  blue++;
   changed=true;
   }
   
void loop() {
  if ( changed=true)
  {
    noInterrupts();
    if (red < 254){
    analogWrite(7, red);
    }
    analogWrite(6, green);
    analogWrite(5, blue);
    changed=false;
    interrupts();
    Serial.begin(57600);
    Serial.println(red);
    //Serial.println(red, BIN);
    Serial.println(green);
    //Serial.println(green,HEX);
    Serial.println(blue);
    //Serial.println(blue,OCT);
    delay(100);
        }
   
  // put your main code here, to run repeatedly:
 
}
I expected on the sreial to wait for a change of one color, but it keeps scrolling and scrolling in my monitor.
The time the colors were INT I placed a IF (color> < 255) STATEMENT IN THE ISR. But it gave me an error, as soon, as I reached more then 255.
OK I thoughtt. Place it outside the ISR. Possibly it shouldn't be inside a ISR. So I placed that code in the LOOP code. The same resukt.
I feel al little confused. Is adruino THAT different. Where can I find some docs, that help me?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, problem solved. something has to be == mot only =
Found a good thread here with programming hints.
Logged

Pages: [1]   Go Up
Jump to: