i have this code for a project i found online. I'm not very good at programming so i come to you for help. I am trying to invert the polarity on all of the bit because the LEDs i am using are common anode and not common cathode. here is the code part 1.
//based on buttton_test
//This is the v1 release of the RGB door lock code. The hard coded code is:
// [r][g][b][r]
// [r][g][b][r]
// The buttons will flash blue in order while idle.
// On a press, the color is toggled, starting from blank. The password is entered
// by pressing the buttons to set the color code.
// When the code is entered, the pad will turn green and unlock for 10 seconds.
// If the counter is exceeded, the pad will turn red for 30 seconds.
#define DATAOUT 11//MOSI (pin 7 of AD5206)
#define DATAIN 12//MISO - not used, but part of builtin SPI
#define SPICLOCK 13//sck (pin 8 of AD5206)
#define SLAVESELECT 10 //removed the slave switching code entirely.
#define COLS 4 //x axis
#define ROWS 2 //y axis
#define effect_select 1 //choose idle effect
const byte colpin[COLS] = { //pins for led column grounding transistors
14,15,16,17}; // Using the analog inputs as digital pins (14=A0,15=A1,16=A2,17=A3)
const byte buttonWrite[ROWS] = {
2, 3}; //Pins for the Vin of the buttons, y-axis
const byte buttonRead[COLS] = {
9, 8, 7, 6}; //Pins for reading the state of the buttons, x-axis
boolean pressed[COLS][ROWS] = {
0};
const byte lock_pin = 4;
boolean lockState = 0;
boolean effect[COLS][ROWS] = {
0};
byte effect_color = 3;
byte effect_state = 0;
byte effect_count = 0;
// The pot register numbers for each of the red, green, and blue channels
// Address map for AD5206:
// Pin bin dec
// 2 101 5
// 11 100 4
// 14 010 2
// 17 000 0
// 20 001 1
// 23 011 3
const byte red[2] = {
5, 0};
const byte green[2] = {
4, 1};
const byte blue[2] = {
2, 3};
// Main data for the drawing routine
byte rGrid[COLS][ROWS] = {
0};
byte gGrid[COLS][ROWS] = {
0};
byte bGrid[COLS][ROWS] = {
0};
// Entry code definition
byte rCode[COLS][ROWS] = {
0};
byte gCode[COLS][ROWS] = {
0};
byte bCode[COLS][ROWS] = {
0};
byte COLORS = 4;//0 1 2 3
byte rColors[] = {0, 255, 0, 0 }; //red
byte gColors[] = {0, 0, 255, 0 }; //green
byte bColors[] = {0, 0, 0, 255}; //blue
byte colorcode[COLS][ROWS] = { 0 };
byte count;
void setup(){
count = 0; //init count
//set entry code
colorcode[0][0] = 1; //row 1
colorcode[1][0] = 2;
colorcode[2][0] = 3;
colorcode[3][0] = 1;
colorcode[0][1] = 1; //row 2
colorcode[1][1] = 2;
colorcode[2][1] = 3;
colorcode[3][1] = 1;
Serial.begin(19200);
pinMode(lock_pin, OUTPUT);
digitalWrite(lock_pin, LOW);
//setup the button inputs and outputs
for(int i = 0; i < ROWS; ++i){ //set row lines to output and zero them
pinMode(buttonWrite[i], OUTPUT);
digitalWrite(buttonWrite[i],LOW);
}
for(int j = 0; j<COLS; ++j) { //set column lines to input
pinMode(buttonRead[j], INPUT);
}
byte i;
byte clr;
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
for(byte c = 0; c < COLS; ++c){
pinMode(colpin[c], OUTPUT); // Initialize LED columns
digitalWrite(colpin[c], LOW); // Turn all LED cols off
}
digitalWrite(SLAVESELECT,HIGH); //disable device // SPCR = 01010000
//interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
//sample on leading edge of clk,system clock/4 (fastest)
SPCR = (1<<SPE)|(1<<MSTR);
clr=SPSR;
clr=SPDR;
delay(10);
// clear all of the pot registers
for (i=0;i<6;i++)
{
write_pot(i,0);
}
grid_init(); //clear rgb grid.
code_init(); //set code in memory.
effect_init();
}
void grid_init(){
//initialize the button grids with blank data
for(byte c = 0; c < COLS; ++c){
for(byte r = 0; r < ROWS; ++r){
rGrid[c][r] = 0;
gGrid[c][r] = 0;
bGrid[c][r] = 0;
}
}
}
void effect_init(){
//initialize the button grids with blank data
for(byte c = 0; c < COLS; ++c){
for(byte r = 0; r < ROWS; ++r){
effect[c][r] = 0;
}
}
effect[0][0] = 1;
}
void loop(){
Serial.print(".");
effect_count++;
if(count > 15){
color_effect(255, 0, 0, 30);
grid_init();
count = 0;
}
for(byte r = 0; r < ROWS; ++r){
digitalWrite(buttonWrite[r], HIGH); //bring button row high for reading presses
clear_pot(); //clear pot regs between rows, otherwise the row not being read will be lit during other row reads.
if(code_check()){
count = 0;
Serial.print("code matched!"); //silly debug statment.
open_door(); //unlock the door.
grid_init(); //clear entered code and return to scanning mode.
}
for(byte c = 0; c < COLS; ++c){
if(pressed[c][r] != digitalRead(buttonRead[c])){
pressed[c][r] = digitalRead(buttonRead[c]);
if(pressed[c][r]){
if(count == 0){
grid_init(); //clear the button states once a code entry is begun.
}
on_press(c, r); //on button press, call on_press
count++;
} else {
on_release(r, c); //on release, call on_release
}
} else {
if(pressed[r][c]){
while_pressed(c, r); //on button hold
} else {
while_released(c,r); //on release
}
}
if(count == 0){
if(effect_count == 20) {
idle_effect();
effect_count = 0;
}
}
write_pot(red[r],rGrid[c][r]); //writes state of colors to the digital pot register
write_pot(green[r],gGrid[c][r]);
write_pot(blue[r],bGrid[c][r]);
digitalWrite(colpin[c], HIGH); //turn one col on while pot is written.
delayMicroseconds(750); // display. Persistance of Vision makes things appear lit constantly.
digitalWrite(colpin[c], LOW); //turn the col back off
}
digitalWrite(buttonWrite[r], LOW); //bring current row low.
delay(4);
}
//delay(10);
}