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.
-
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? -
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.