Sunday, June 9, 2013

I got the #arduino project to work, but I don't understand why

First, I modified the displayLed() routine to take an argument and modified all of the code to call it that way. But, that didn't work. So, I modified the the interrupt handler to to set a flag that it occurred, and modified the main loop to blink the "caught" LED. This worked, but I don't know why. Is it because interrupt handlers can't call a routine from the main code? This doesn't make sense to me since the, now functioning, version calls other functions. Do I need to declare the displayLed() function differently? Maybe someone can leave a comment or tweet a reason. Here's the code:

int dataPin = 10;        //Define which pins will be used for the Shift Register control
int latchPin = 9;
int clockPin = 8;

long i = 1;
int pauseTime = 100;      // byte #2 value
bool left = true;
bool buttonPushed=false;
volatile int state=LOW;
unsigned long time;
unsigned long lasttime;
void setup()
{
    attachInterrupt(0,pushit,LOW);
    pinMode(dataPin, OUTPUT);       //Configure each IO Pin
    pinMode(latchPin, OUTPUT);
    pinMode(clockPin, OUTPUT);
    pinMode(12,OUTPUT);
    digitalWrite(12,state);
    lasttime==millis();
}
void loop()
{
  displayLed(i);
  if(buttonPushed){
    for(int loopVar=0;loopVar<10;loopVar++){
      displayLed(0);
      delay(150);
      displayLed(i);
      delay(150);
    }
    buttonPushed=false;
  }

  if (left ){
    i = i << 1;
  }
  else
  {
    i = i >> 1;
  }
  if (i == 32768 || i == 1){
    left = !left;
  }
  delay(pauseTime);
  if (pauseTime <= 0){
    pauseTime = 100;
  }
}
void pushit(){
  time=millis();
  if (time-lasttime>50){
    digitalWrite(12,!state);
    state=!state;
    lasttime = time;
    pauseTime = pauseTime - 2;
    buttonPushed=true;
  }
}
void displayLed(unsigned long iVal){
  digitalWrite(latchPin, LOW);            //Pull latch LOW to start sending data
  if (iVal > 255){
    shiftOut(dataPin,clockPin,MSBFIRST,0);
    shiftOut(dataPin,clockPin,MSBFIRST,iVal/256);
  }
  else
  {
    shiftOut(dataPin,clockPin,MSBFIRST,iVal);
    shiftOut(dataPin,clockPin,MSBFIRST,0);
  }  
  digitalWrite(latchPin, HIGH);           //Pull latch HIGH to stop sending data
}

My next challenge is the LCD panel. I looked up code samples on the Arduino web site shows a different version of the LCD. Mine has a 14 pine header mounted on the bottom and a red wire soldered to the bottom. No idea what this is for.





I guess I have to do some tinkering.

2 comments :

  1. HEy, regarding the LCD being 14 pins, most have 16, since 2 extra are used for backlight powering.

    Good luck.

    ReplyDelete
    Replies
    1. I was thinking that, maybe, the red wire was for the backlight. There is also four connections on the side that I have no idea what they are. Was going to try to connect the red wire to 5 volts and see what happens. Thanks for taking the time to read/comment.

      Delete