Pages: 1 [2]   Go Down
Author Topic: LCD displaying weird Symbols after Interrupt  (Read 2669 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

loop with some proper indentation, and a bit compacting

Code:
void loop ()
{
  if ( TDCinterrupt1)
  {
    cli();
    crankAngle = ULmap(micros(), TDCtime, nextTDC, 0, 360);  // Change time into degrees
    sei();
    crankAngle = constrain(crankAngle,  0, 360); // Limit crankshaft vales from 0 to 360
  }
  
//----------------------------------  Button Counter Control  
buttonstate = digitalRead(timingPin);
 
  // Update Button Counter
  if ((buttonstate != lastbuttonstate) && (buttonstate == HIGH)) buttonpushcounter++;
  lastbuttonstate = buttonstate;
  if( buttonpushcounter > 9) buttonpushcounter = 0;
  
 
//--------------------------------------- Button Timing Control
  timing = timingcontrol();
  // timing; ?????

//--------------------------------  Timing Center

// If the crankshaft is inbetween 1 to 179 degrees of revolution then...
  if ( crankAngle >= a && crankAngle <= a1)
  {
    digitalWrite(V17Pin, HIGH); // Fire Valve 1 and 7 (Intake)
    digitalWrite(V46Pin, HIGH); // Fire Valve 4 and 6 (Exhaust)
    Serial.println("Intake (1,7) Exhaust (4,6) Fired 100%"); // Print Status and Percent of Valve Open
    lcd.setCursor(12,1);
    lcd.print ("++");
  }
  else
  {
    digitalWrite(V17Pin, LOW); // Don't Fire Valve 1 and 7 (Intake)
    digitalWrite(V46Pin, LOW); // Don't Fire Valve 4 and 6 (Exhaust)
     lcd.setCursor(12,1);
     lcd.print ("  ");
  }
  
  // If the crankshaft is inbetween 181 to 359 degrees of revolution then...
  if ( crankAngle >= b && crankAngle <= b1)
  {
     digitalWrite(V35Pin, HIGH); // Fire Valve 3 and 5 (Intake)
     digitalWrite(V28Pin, HIGH); // Fire Valve 2 and 8 (Exhaust)
     lcd.setCursor(14,1);
     lcd.print ("++");
     Serial.println("Intake (3,5) Exhaust (2,8) Fired 100%"); // Print Status and Percent of Valve Open
  }
  else
  {
    digitalWrite(V35Pin, LOW); // Don't  Fire Valve 3 and 5 (Intake)
    digitalWrite(V28Pin, LOW); // Don't Fire Valve 2 and 8 (Exhaust)
    lcd.setCursor(14,1);
    lcd.print ("  ");
  }      

//------------------------------- RPM Display
      
  lcd.setCursor(1,1);
  lcd.print(rpm,DEC);

//------------------------------- START MODE

  start = startmode();
  // start;  

//--------------------------------Maitenance Mode

  maint = maintmode();
  // maint;
}

Code:
unsigned long ULmap(unsigned long x, unsigned long in_min, unsigned long in_max, unsigned long out_min, unsigned long out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

United States
Offline Offline
Full Member
***
Karma: 0
Posts: 148
Learning
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Furthermore you must spend some time learning about array's - http://www.arduino.cc/en/Reference/Array - and functions - http://www.arduino.cc/en/Reference/FunctionDeclaration.

If you have mastered these two basic techniques you can cleanup your code quite a lot.

Also important to learn is to use proper identation as that makes code better readable.

I've been trying. I've learned all of this with no background or class's in programming, so my brain isn't exactly wired to think in "code". I am a mechanical engineer major, who assumed this was easier then it actually is  :smiley

I was searching for a function like array, but didn't know how to find that actual definition.

Quote
cli();     // where is the sei()
With "maitenance mode", I won't be needing to activate the interrupt. It's mainly something to allow me to turn the engine over by hand. If no valves we open, it be impossible. I also bought a switch, so that delay will no longer be there.

Quote
In fact as all values are linear (almost) this can be optimized to
Code:

int timingcontrol()
{
  a = 1;
  b = 181;

  a1 = 180 - 18 * buttonpushcounter;
  if (buttonpushcounter ==0) a1 = a1 -1;  // makes 179
  b1 = a1 + 180;
  
  lcd.setCursor(12,0);
  lcd.print(100-10*buttonpushcounter);
  lcd.print("%");
}

This is awesome. Looks like it would work perfectly. If only I knew about setting up these functions.

Quote
loop with some proper indentation, and a bit compacting
Great, thank you. I will post an updated version of mine with this included after this post.

Quote
// timing; ?????

Why the question marks?

Quote
// start;

why do you have // in front of all the functions I'm calling? The // makes it so arduino can't see that.
« Last Edit: January 04, 2011, 08:18:28 pm by Newman180 » Logged

United States
Offline Offline
Full Member
***
Karma: 0
Posts: 148
Learning
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've attempted to clean the code further as well as label everything more clearly.

I got rid of the ULmap at the moment as it still won't compile.
I also attempted to use a fucntion in start mode to get rid of all the clutter. I don't know if I implemented it correctly. Please let me know. Thanks

Code:
//--- NEWMAN180  1/4/11
 //------------------------------------------------------------------------
 #include <LiquidCrystal.h>
 volatile unsigned int revPeriod; // Rotation Time
 volatile unsigned long prevTDCtime; // Previous time Hall Sensor Picked up
 volatile unsigned long TDCtime; // Current TDC Time
 volatile unsigned long nextTDC; // Predicted next TDC Time
 volatile unsigned long rpm; // Used to calculate RPM
 volatile unsigned int crankAngle; // Crankshaft Angle out of 360 degrees
 unsigned int a; // first crank angle of 180
 unsigned int a1; // second crank angle of 180
 unsigned int b; // first crank angle of 360
 unsigned int b1; // second crank angle of 360
 boolean TDCinterrupt1 = false; // States very first interrupt is ignored
 const int timingPin = 14; // Timing Button Assignment
 const int maintbuttonPin = 15; // Maint Button Assignment
 const int pushstartPin = 16; // Push Start Button Assignment
 int pushstartState = 0; // Push Start Button State
 int maintbuttonState = 0; // Maint Button State
 int buttonstate = 0; // Used to count button pushes for Timing
 int buttonpushcounter = 0;
 int lastbuttonstate = 0;
 int start; // Used when declaring Start Mode Function  
 int timing; // Used when declaring Timing Mode Function
 int maint; // Used when declaring Maint Mode Function
 const int V17Pin = 9; // Valves 1 and 7 assigned to Pin 3  Intake
 const int V35Pin = 11; // Valves 3 and 5 assigned to Pin 4  Intake
 const int V28Pin = 10; // Valves 2 and 8 assigned to Pin 5  Exhaust
 const int V46Pin = 12; // Valves 4 and 6 assigned to Pin 6  Exhaust
 LiquidCrystal lcd (8,7,6,5,4,3);
  
  void setup()
 {
    // Communicate to Serial Port
   Serial.begin (9600);
   lcd.begin(16,2);
   pinMode (V17Pin, OUTPUT); // Valve 1&7 as Output
   pinMode (V35Pin, OUTPUT); // Valve 3&5 as Output
   pinMode (V28Pin, OUTPUT); // Valve 2&8 as Output
   pinMode (V46Pin, OUTPUT); // Valve 4&6 as Output
   pinMode (pushstartPin, INPUT); // Push Start Button Input
   pinMode (timingPin, INPUT); // Timing Button Input
   pinMode (maintbuttonPin, INPUT); // Maint Button Input
   rpm = 0;
   nextTDC = 1;
   lcd.setCursor(0,0);
   lcd.print("ON");
   attachInterrupt(0,TDCinterrupt, RISING); // Interrupt 0 is Pin 2 Hall Effect Sensor  
   digitalWrite(2,HIGH);
   delay(3500);
 }
  
 void TDCinterrupt()
{
  TDCinterrupt1 = true;  // First interrupt has passed
  prevTDCtime = TDCtime;  // save last TDC
  TDCtime = micros();  // save THIS TDC
  revPeriod = TDCtime - prevTDCtime;  // calculate revolution period
  if (prevTDCtime > TDCtime)  {
  revPeriod = TDCtime - prevTDCtime + 2147483648;}  // correct for micros() overflow every 71 minutes
  nextTDC = TDCtime + revPeriod;  
  rpm = 60000000L/revPeriod; // Calculate RPM divide rotation period by the difference of micros to minute
 }

void loop ()
{
  if ( TDCinterrupt1)
  {
    cli // Disable Interrupt
    crankAngle = map(micros(), TDCtime, nextTDC, 0, 360);  // Change time into degrees
    sei(); // Enable Interrupt
    crankAngle = constrain(crankAngle,  0, 360); // Limit crankshaft vales from 0 to 360
  }

//----------------------------------  Button Counter Control
buttonstate = digitalRead(timingPin);
  // Update Button Counter
  if ((buttonstate != lastbuttonstate) && (buttonstate == HIGH)){ buttonpushcounter++;} // If the buttonstate is greater than last, and it is high, increase counter
  lastbuttonstate = buttonstate;
  if( buttonpushcounter > 9) {buttonpushcounter = 0;} // If buttonstate has been pushed more than 9 times, reset to 0

//--------------------------------------- Button Timing Control
 timing = timingcontrol(); //
 timing; // Calling Timing Control Function

//--------------------------------  Timing Center
  if ( crankAngle >= a && crankAngle <= a1)// If the crankshaft is inbetween 1 to 179 degrees of revolution then a and a1 from timing function
  {
    digitalWrite(V17Pin, HIGH); // Fire Valve 1 and 7 (Intake)
    digitalWrite(V46Pin, HIGH); // Fire Valve 4 and 6 (Exhaust)
    Serial.println("Intake (1,7) Exhaust (4,6) Fired 100%"); // Print Status and Percent of Valve Open
    lcd.setCursor(12,1);
    lcd.print ("++");
  }
  else
  {
    digitalWrite(V17Pin, LOW); // Don't Fire Valve 1 and 7 (Intake)
    digitalWrite(V46Pin, LOW); // Don't Fire Valve 4 and 6 (Exhaust)
     lcd.setCursor(12,1);
     lcd.print ("  ");
  }
 
  if ( crankAngle >= b && crankAngle <= b1) // If the crankshaft is inbetween 181 to 359 degrees of revolution then use b and b1 from timing function
  {
     digitalWrite(V35Pin, HIGH); // Fire Valve 3 and 5 (Intake)
     digitalWrite(V28Pin, HIGH); // Fire Valve 2 and 8 (Exhaust)
     lcd.setCursor(14,1);
     lcd.print ("++");
     Serial.println("Intake (3,5) Exhaust (2,8) Fired 100%"); // Print Status and Percent of Valve Open
  }
  else
  {
    digitalWrite(V35Pin, LOW); // Don't  Fire Valve 3 and 5 (Intake)
    digitalWrite(V28Pin, LOW); // Don't Fire Valve 2 and 8 (Exhaust)
    lcd.setCursor(14,1);
    lcd.print ("  ");
  }

//------------------------------- RPM Display    
  lcd.setCursor(1,1);
  lcd.print(rpm,DEC);
//------------------------------- START MODE
  start = startmode();
  start; // Calling Start Mode function
//--------------------------------Maitenance Mode
  maint = maintmode();
  maint; // Calling Maint Mode Function
}

//-------------------------Start Mode Function
int startmode (){
  if (TDCtime == 0){
  pushstartState = digitalRead(pushstartPin);
  if (pushstartState == HIGH){  
  delay(3000);
  Serial.print ("** Start MODE **");
           lcd.clear();
           lcd.setCursor(3,0);
           lcd.print ("Start Mode");
 // Open All Valves for 3.5 Seconds          
           digitalWrite( V17Pin, HIGH);
           digitalWrite( V35Pin, HIGH);
           digitalWrite( V28Pin, HIGH);
           digitalWrite( V46Pin, HIGH);
           lcd.setCursor(0,1);
           delay(3500);
// Close All Valves and wait for 15 seconds
           digitalWrite( V17Pin, LOW);
           digitalWrite( V35Pin, LOW);
           digitalWrite( V28Pin, LOW);
           digitalWrite( V46Pin, LOW);
           delay(15000);
           lcd.clear();
           lcd.print("Starting");
    int i;
    int n;
    n = 5;
    for (i = 0; i < n ; i++) { // Do the following function  5 times in a row. Open one intake and all exhaust
           digitalWrite( V17Pin, HIGH);
           digitalWrite( V28Pin, HIGH);
           digitalWrite( V46Pin, HIGH);
           delay(100);
           digitalWrite( V28Pin, LOW);// Open and close exhaust every 10th of a second
           delay(100);}
    int j;
    int m;
    m = 5;
    for (j = 0; j < m ; j++) { // Do the following function  5 times in a row. Open other intake and all exhaust                    
           digitalWrite( V35Pin, HIGH);
           digitalWrite( V28Pin, HIGH);
           digitalWrite( V46Pin, HIGH);
           delay(100);
           digitalWrite( V46Pin, LOW);// Open and close exhaust every 10th of a second
           delay(100);}
else{    
    lcd.setCursor(0,0);
    lcd.print("START *KEY*");// If tdc time still equals 0, Please hit start button again    
}
  }
    }
 //-------------------------------- Timing Control Function
 int timingcontrol()
{
  a = 1;
  b = 181;

  a1 = 180 - 18 * buttonpushcounter; // a1 is upper end of first half of crank angle
  if (buttonpushcounter ==0) a1 = a1 -1;  // When buttonpushcounter equals 0, it demands a seperate function to equal 179 rather than 0
  b1 = a1 + 180; // b1 is the upper end of the second half of crank angle
  
  lcd.setCursor(12,0);
  lcd.print(100-10*buttonpushcounter); // print value of timing
  lcd.print("%");
}
//-------------------------------- Maint Mode Function
 int maintmode() {
    maintbuttonState = digitalRead(maintbuttonPin);
   if (maintbuttonState == HIGH){
     cli(); // disable interrupt
     TDCtime = 0; // set all values to 0 to enforce no "automatic" rotation
     nextTDC = 0;
     prevTDCtime = 0;
     lcd.clear();
     lcd.print("Maintenance MODE");
     digitalWrite(V28Pin,HIGH); // Open all Exhaust and stay open as long as maintbutton is high
     digitalWrite(V46Pin,HIGH);
  }
    
   }
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I don't know if I implemented it correctly
If it does compile you can check it.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

United States
Offline Offline
Full Member
***
Karma: 0
Posts: 148
Learning
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If it does compile you can check it.
it compiles, I just didn't know if I applied the it to the "formula" correctly. I guess since you didn't make a comment, I did smiley-wink
I'm still not sure about using the delays in start mode though. I feel like I'll miss an important interrupt.
Logged

Pages: 1 [2]   Go Up
Jump to: