Questions with regards to interrupts

Hello, I have here a code for Arduino Globe POV:

#include "Arduino.h"
#include <avr/pgmspace.h>
#include "world.h"
 
#define SpinInterrupt 0
#define SpinInput 2
 
//Define cols/rows to ports
//column +
#define col0 3
#define col1 4
#define col2 5
#define col3 6
#define col4 7
#define col5 8
#define col6 9
#define col7 10
#define col8 11
 
//row -
#define row0 12
#define row1 13
#define row2 14
#define row3 15
#define row4 16
#define row5 17
#define row6 18
#define row7 19
 
#define LEDOrientation false
 
volatile unsigned long microsPerPixelColumn = 10000;
volatile unsigned long microsPerPixelEight = 0;
volatile unsigned long lastSpinTime = 0;
volatile int column = 0;
volatile int row = 0;
volatile int LEDEight = 0;
volatile int CurrentColumns = 0;
 
unsigned long inturruptDebounce = 80;
bool bStop;
int eightpins[9] = {col0, col1, col2, col3, col4, col5, col6, col7, col8};
 
int pins[72][2] = {{col0, row0},
                   {col0, row1},
                   {col0, row2},
                   {col0, row3},
                   {col0, row4},
                   {col0, row5},
                   {col0, row6},
                   {col0, row7},
 
                   {col1, row0},
                   {col1, row1},
                   {col1, row2},
                   {col1, row3},
                   {col1, row4},
                   {col1, row5},
                   {col1, row6},
                   {col1, row7},
 
                   {col2, row0},
                   {col2, row1},
                   {col2, row2},
                   {col2, row3},
                   {col2, row4},
                   {col2, row5},
                   {col2, row6},
                   {col2, row7},
 
                   {col3, row0},
                   {col3, row1},
                   {col3, row2},
                   {col3, row3},
                   {col3, row4},
                   {col3, row5},
                   {col3, row6},
                   {col3, row7},
 
                   {col4, row0},
                   {col4, row1},
                   {col4, row2},
                   {col4, row3},
                   {col4, row4},
                   {col4, row5},
                   {col4, row6},
                   {col4, row7},
 
                   {col5, row0},
                   {col5, row1},
                   {col5, row2},
                   {col5, row3},
                   {col5, row4},
                   {col5, row5},
                   {col5, row6},
                   {col5, row7},
 
                   {col6, row0},
                   {col6, row1},
                   {col6, row2},
                   {col6, row3},
                   {col6, row4},
                   {col6, row5},
                   {col6, row6},
                   {col6, row7},
 
                   {col7, row0},
                   {col7, row1},
                   {col7, row2},
                   {col7, row3},
                   {col7, row4},
                   {col7, row5},
                   {col7, row6},
                   {col7, row7},
 
                   {col8, row0},
                   {col8, row1},
                   {col8, row2},
                   {col8, row3},
                   {col8, row4},
                   {col8, row5},
                   {col8, row6},
                   {col8, row7}
                  };
 
void setup() 
{
  pinMode(SpinInput, INPUT); //Reed switch
 
  pinMode(col0, OUTPUT);
  pinMode(col1, OUTPUT);
  pinMode(col2, OUTPUT);
  pinMode(col3, OUTPUT);
  pinMode(col4, OUTPUT);
  pinMode(col5, OUTPUT);
  pinMode(col6, OUTPUT);
  pinMode(col7, OUTPUT);
  pinMode(col8, OUTPUT);
 
  pinMode(row0, OUTPUT);
  pinMode(row1, OUTPUT);
  pinMode(row2, OUTPUT);
  pinMode(row3, OUTPUT);
  pinMode(row4, OUTPUT);
  pinMode(row5, OUTPUT);
  pinMode(row6, OUTPUT);
  pinMode(row7, OUTPUT);
 
  CurrentColumns = ImageColumns;
  lastSpinTime = micros();
 
  Clear();
 
  for(int j = 0; j < ImageRows; j++)
  {
    Clear();
    digitalWrite(pins[j][0], !LEDOrientation);
    digitalWrite(pins[j][1], LEDOrientation);
    delay(30);
  }
 
  attachInterrupt(SpinInterrupt, spinInterrupt, FALLING);
}
 
bool inInterrupt = false;
unsigned long spinTime = 0;
void spinInterrupt()
{
  if(lastSpinTime == 0)
  {
    lastSpinTime = micros();
    return;
  }
 
  if(!inInterrupt && micros() - lastSpinTime > inturruptDebounce)
  {
    inInterrupt = true;
    unsigned long newSpinTime = micros() - lastSpinTime;
    if(spinTime != 0 && (newSpinTime > spinTime*2 || newSpinTime < spinTime/2))
    {
      inInterrupt = false;
      return;
    }
    spinTime = newSpinTime;
 
    microsPerPixelColumn = spinTime / CurrentColumns;
    microsPerPixelEight = microsPerPixelColumn / LEDEights;
    lastSpinTime = micros();
    inInterrupt = false;
  }
}
 
void loop()
{
  for(column = 0; column < CurrentColumns; column++)
  {
    for(LEDEight = 0; LEDEight < LEDEights ; LEDEight++)
    {
      DrawLEDGroupsAtOnce(LEDEight, column);
    }
  }
}
 
void DrawLEDGroupsAtOnce(int eight, int column)
{
  prog_uint8_t imageEights = pgm_read_byte(&(Image[column][eight]));
 
  PORTB = (PORTB | B00110000) & ((imageEights << 4) | B11001111);
  PORTC = (PORTC | B00111111) & ((imageEights >> 2) | B11000000);
 
  digitalWrite(eightpins[eight], !LEDOrientation);
 
  if (microsPerPixelEight > 20)
  delayMicroseconds(microsPerPixelEight - 17);
 
  digitalWrite(eightpins[eight], LEDOrientation);
}
 
void Clear()
{
  for(int j = 0; j < ImageRows; j++)
  {
    digitalWrite(pins[j][0], LEDOrientation);
    digitalWrite(pins[j][1], !LEDOrientation);
  }
}

Now I have few questions here.

  1. I really don't know the significance of the ReedSwitch here. I don't see it being used here. IS IT BEING USED HERE?
    1.1 follow up question, I can also use Hall Effect sensor right? In the schematic of that project, the Reedswitch is being pulled up by a 10k resistor to positive 5v and the other side to negative. Simply saying, if that switch is in the magnetic field, it gives a LOW value to digitalreader. My halleffect sensor is also pulled up to high, when in magnetic field, it gives a LOW value. So therefore I can use a halleffect, right? RIGHT? RIGHT? :slight_smile:

  2. The project is for Atmega328p. I can use both UNO and Due bootloader/boards right?
    The link: attachInterrupt() - Arduino Reference confuses me. Look at the codes, The reed switch is in Pin 2 and the interrupt is equals to 0, this means pin 0 , right?
    2.1 follow up to question number 2: If the interrupt uses the pin 0 (if I am right in query no. 2), THEN I CANNOT USE the RX/TX pins for the bluetooth?

Sorry I have a very very littlek knowledge about interrupts.
I hope someone would answer all my questions completely.

The only mention of a reed is in a comment, but assuming there's a reed switch connected to pin 2, you're using it to trigger your interrupt.

The reed switch is in Pin 2 and the interrupt is equals to 0, this means pin 0 , right?

Wrong. Pin 2 is external interrupt 0.

int pins[72][2]

A waste of 144 bytes of precious RAM. Make it "byte".

hi sir! thanks so much for the inputs, and thanks for the suggestion. :slight_smile:
can you also answer my other queries? ehehhe

Anyways, in the code:

#defined SpinInput 2

When I saw that, I pressed CTRL+F to find other uses of that SpinInput reed switch but it was not used. Does it have an importance for the interrupt?

TY

Have you got a clean compile?

pinMode(SpinInput, INPUT);

(BTW, it "#define", not "#defined")

AWOL:

pinMode(SpinInput, INPUT);

(BTW, it "#define", not "#defined")

ah yes sir define. I know it is being defined, but I was wondering why it was never read. Tried searching for digitalRead or analogRead for such SpinInput.

JimboZA:
Have you got a clean compile?

yes sir, clean c ompile and fine upload to the board. My board is Gizduino 644. I am thinking that I should use a Gizduino or other generic boards that has an UNO bootloader and a 328p chip.

but I was wondering why it was never read

It's an interrupt - it is implicitly read every instruction cycle.

Sir! Here, I have search for the datasheet of 328p and 644p.

I want to know if these microcontrollers have the same pins for interrupts.
my guess is: YES? For pin 2? :smiley: I don't really know.

Why guess?
http://arduino.cc/en/Reference/AttachInterrupt
Remember, these are Arduino pin numbers, not AVR pin numbers.

AWOL:
Why guess?
attachInterrupt() - Arduino Reference
Remember, these are Arduino pin numbers, not AVR pin numbers.

sir, it is because I'm gonna burn the codes of my project into a generic version of an arduino. It is called a gizduino with an ATMEGA644p.
here: http://www.e-gizmo.com/KIT/gizduino+%20164,324,644.html The prints in the board has RX0 TX0 and RX1 TX1. From pins 0 to 3. So I thought this might be different from Arduino with a 328p microcontroller.

up. I can't make the project work, so I need to know if 644p is the same with 328p. :frowning: