Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: Problem with dividing two integers on: February 08, 2013, 03:49:30 am
I got it to work with this:

Code:
     
 float Faktor = 0;
 float elapsedTime2 ;



elapsedTime2 = elapsedTime;
      Faktor = (float)Points / (float)elapsedTime2;
       lcd.setCursor(6,3);
       lcd.print( (float)Faktor * 1000);
2  Using Arduino / Programming Questions / Re: Problem with dividing two integers on: February 08, 2013, 03:27:30 am
Sorry about that..

I expected to get 4.4 when Points =88 and elapsedTime =20.00
instead i got -1
3  Using Arduino / Programming Questions / Re: Problem with dividing two integers on: February 08, 2013, 03:14:07 am
Thank you UKHeliBob for making that "easy to learn" example. I got it working:)

4  Using Arduino / Programming Questions / Re: Problem with dividing two integers on: February 08, 2013, 02:39:24 am
michinyon:
Well, all of this is pretty new to me, and i have no previously knowledge of programming...

Do you know how i can get my equation to work?


Nick Gammon:
I can see on a lcd what the values are so it is easy to check if i get the same as on a calculator
5  Using Arduino / Programming Questions / Problem with dividing two integers on: February 08, 2013, 02:04:49 am
Her is my code:

Code:
#include <Wire.h>
 #include <LiquidCrystal.h>

 LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


 #define ledPin  13                  // LED connected to digital pin 13
 #define buttonPin 9                 // button on pin 4
 #define alfaButton 8
 #define charlieButton 6
 #define deltaButton 10
 
 int value = LOW;                    // previous value of the LED
 int buttonState;                    // variable to store button state
 int lastButtonState;                // variable to store last button state
 long interval = 100;                // blink interval - change to suit
 long previousMillis = 0;            // variable to store last time LED was updated
 long startTime ;                    // start time for stop watch
 long elapsedTime ;                  // elapsed time for stop watch
 int fractional;                     // variable used to store fractional part of time
 int fractional2;
 
unsigned long LastDebounceTime = 0;
unsigned long DebounceDelay = 50;
unsigned long LastDebounceTime2 = 0;
unsigned long DebounceDelay2 = 50;
unsigned long LastDebounceTime3 = 0;
unsigned long DebounceDelay3 = 50;

 boolean test = false;
 boolean start = false;
 boolean lockout = false;            //**NEW BOOLEAN**
 byte lastbuttonState2 = 0;          // new global variable
 byte buttonState1 = 0;
 byte buttonState2 = 0;
 
 int Points = 0;
 int Faktor = 0;
 
int SwitchState =0;
int SwitchDebounce;
int LastSwitchState=HIGH;
int LastSwitchDebounce=LOW;
int LastSwitchState2=HIGH;
int LastSwitchDebounce2=LOW;
int LastSwitchState3=HIGH;
int LastSwitchDebounce3=LOW;
int Counter=0;
int Counter2=0;
int Counter3=0;

 void setup()
 {
   Serial.begin(9600);

   lcd.begin(16, 4);
   // Print a message to the LCD.
  lcd.print("Test");

    lcd.setCursor(0,1);     
    lcd.print("A:");

    lcd.setCursor(-4,2);   
    lcd.print("C:");
   
    lcd.setCursor(-4,3);
    lcd.print("D:");   

   pinMode(ledPin, OUTPUT);         // sets the digital pin as output

   pinMode(buttonPin, INPUT);       // not really necessary, pins default to INPUT anyway
   digitalWrite(buttonPin, HIGH);   // turn on pullup resistors. Wire button so that press shorts pin to ground.

 }

 void loop()
 {  // no more debounce
   buttonState2 = digitalRead(buttonPin);
   if(buttonState2 == HIGH){
     if(lockout == false){ // #1
       start = true;
       startTime = millis();
       lastButtonState = buttonState2;
       lockout = true; // (The lockout) this will make sure it does not go back to this IF statement
     }
   }
   if(buttonState2 == HIGH && start == true){ // #2
     elapsedTime =   millis() - startTime;              // store elapsed time
     lastButtonState = buttonState;                     // store buttonState in lastButtonState, to compare next time

     lcd.setCursor(9,1);

     lcd.print(" ");
     // routine to report elapsed time
     lcd.print( (int)(elapsedTime / 1000UL));         // divide by 1000 to convert to seconds - then cast to an int to print

     lcd.print(".");

     fractional = (int)(elapsedTime % 1000UL);



     lcd.print(fractional);  // print fractional part of time
   }
   //else lcd.clear(); // #3
   

 //_____________________________________________________       
       
  int CurrentSwitch = digitalRead(alfaButton);
  if (CurrentSwitch != LastSwitchDebounce)
  {
    LastDebounceTime = millis();
  }
  if ((millis() - LastDebounceTime) > DebounceDelay)
  {
    if (CurrentSwitch != LastSwitchState)
    {
      if (CurrentSwitch == HIGH)
      {
        Counter=Counter+5;
        lcd.setCursor(2,1);
        lcd.print(Counter);
      }
    }
    LastSwitchState=CurrentSwitch;
  }
  LastSwitchDebounce = CurrentSwitch;
 
 //----------------------------------------
int CurrentSwitch2 = digitalRead(charlieButton);
  if (CurrentSwitch2 != LastSwitchDebounce2)
  {
    LastDebounceTime2 = millis();
  }
  if ((millis() - LastDebounceTime2) > DebounceDelay)
  {
    if (CurrentSwitch2 != LastSwitchState2)
    {
      if (CurrentSwitch2 == HIGH)
      {
        Counter2=Counter2+3;
        lcd.setCursor(-2,2);
        lcd.print(Counter2);
      }
    }
    LastSwitchState2=CurrentSwitch2;
  }
  LastSwitchDebounce2 = CurrentSwitch2;
 
 //----------------------------------------
int CurrentSwitch3 = digitalRead(deltaButton);
  if (CurrentSwitch3 != LastSwitchDebounce3)
  {
    LastDebounceTime3 = millis();
  }
  if ((millis() - LastDebounceTime3) > DebounceDelay)
  {
    if (CurrentSwitch3 != LastSwitchState3)
    {
      if (CurrentSwitch3 == HIGH)
      {
        Counter3=Counter3+1;
        lcd.setCursor(-2,3);
        lcd.print(Counter3);
      }
    }
    LastSwitchState3=CurrentSwitch3;
  }
  LastSwitchDebounce3 = CurrentSwitch3;
  //---------------------------------------- 



        Points=Counter+Counter2+Counter3;
       // Points=Points * 1000;
       
        lcd.setCursor(6,2);
        lcd.print((int)(Points));
       
       lcd.setCursor(5,1);
       lcd.print("TID:");
       
       lcd.setCursor(1,2);
       lcd.print("PNG:");
       
       lcd.setCursor(1,3);
       lcd.print("FAK:");
       
      // Faktor = Points / elapsedTime;
       lcd.setCursor(6,3);
       lcd.print(elapsedTime / Points);
       
     
                               
 }

This works: lcd.print(elapsedTime / Points);
But this doesnt: lcd.print(Points / elapsedTime);

What is causing this strange behavior?
6  Using Arduino / Programming Questions / Re: Simple button counter that increments by 5 on: February 04, 2013, 01:54:46 pm
Delta_G
The code works as i want it to, but the code is probably full of wrong code and i will clean it up when the project is complete:)
7  Using Arduino / Programming Questions / Re: Simple button counter that increments by 5 on: February 04, 2013, 01:52:09 pm
When i opened the serial monitor it showed 5 without pressing the button..
8  Using Arduino / Programming Questions / Re: Simple button counter that increments by 5 on: February 03, 2013, 07:25:25 am
If i changed it to 5 it started at 10, but you showed me what figure to change, and if i changed it to -5 it started at zero as i wanted.

Thank you "ash901226" for solving my problem:)
9  Using Arduino / Programming Questions / Re: Simple button counter that increments by 5 on: February 03, 2013, 06:47:35 am
It works great, but i need it to start from zero and not from 5 as it is now. Do you know how i can make that happen?
10  Using Arduino / Programming Questions / Re: Simple button counter that increments by 5 on: February 03, 2013, 05:56:14 am
Yes, for the time beeing..
11  Using Arduino / Programming Questions / Re: Simple button counter that increments by 5 on: February 03, 2013, 05:43:08 am
I realize that the code is far from optimal, but there should be a fix for my emmidiate problem, as i have one other button that reacts when being pushed.

When i hold the button down it behaves as a timer and increment by 5 each second... Any suggestions to why it does this?
12  Using Arduino / Programming Questions / Simple button counter that increments by 5 on: February 03, 2013, 05:12:12 am
As the title says, i need a button counter that increments by 5 for each push of the button.

Here is what i have so far:

Code:
buttonState3 = digitalRead(alfaButton);

if (buttonState3 == HIGH){
  alfa = ++ alfaVal;
    alfaVal2 = alfa / 100 * 5;
    lcd.setCursor(2,1);     
    lcd.print(alfaVal2);

This actually displays what i want, but i have to keep the button pressed for about a second to make it increment.
Anyone knows a fix for this?

Here is the full code:

Code:
#include <Wire.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);



#define buttonPin 9                 // button on pin 4
#define alfaButton 8

int value = LOW;                    // previous value of the LED
int buttonState;                    // variable to store button state
int lastButtonState;                // variable to store last button state
long interval = 100;                // blink interval - change to suit
long previousMillis = 0;            // variable to store last time LED was updated
long startTime ;                    // start time for stop watch
long elapsedTime ;                  // elapsed time for stop watch
int fractional;                     // variable used to store fractional part of time
int alfa = 0;
int alfaVal = 5;
int alfaVal2 = 0;


boolean start = false;
byte lastbuttonState2 = 0;          // new global variable
byte buttonState1 = 0;
byte buttonState2 = 0;
byte buttonState3 = 0;
byte buttonState4 = 0;

void setup()
{
  Serial.begin(9600);

  lcd.begin(16, 4);
  // Print a message to the LCD.
  lcd.print("PRAKTISK SKYTING");

    lcd.setCursor(0,1);     
    lcd.print("A:");

    lcd.setCursor(-4,2);   
    lcd.print("C:");
   
    lcd.setCursor(-4,3);
    lcd.print("D:");   


  pinMode(buttonPin, INPUT);       // not really necessary, pins default to INPUT anyway
  pinMode(alfaButton, INPUT);       // not really necessary, pins default to INPUT anyway

  digitalWrite(buttonPin, HIGH);   // turn on pullup resistors. Wire button so that press shorts pin to ground.
  digitalWrite(alfaButton, HIGH);   // turn on pullup resistors. Wire button so that press shorts pin to ground.


}

void loop()
{
  buttonState1 = digitalRead(buttonPin);
  delay(10);
 
  buttonState2 = digitalRead(buttonPin);

  if(buttonState1 == buttonState2) {
    if(buttonState2 == LOW){
      start = true;
      startTime = millis();
      lastButtonState = buttonState2;
    }
 
  if(start == true){
  elapsedTime =   millis() - startTime;              // store elapsed time
    lastButtonState = buttonState;                     // store buttonState in lastButtonState, to compare next time

     
    lcd.setCursor(7,3);
    lcd.print("");
    // routine to report elapsed time
    lcd.print( (int)(millis() / 1000UL));         // divide by 1000 to convert to seconds - then cast to an int to print

    lcd.print(".");                             

    fractional = (int)(millis() % 1000UL);

    if (fractional == 0)
      lcd.print("000");     
    else if (fractional < 10)   
      lcd.print("00");       
    else if (fractional < 100)
      lcd.print("0");       

    lcd.print(fractional);  // print fractional part of time
  }

  }
 
 
 
 
 
// Sound timer 
 
 
    // check for button press
    buttonState = digitalRead(buttonPin);                   // read the button state and store

    if (buttonState == HIGH && lastButtonState == LOW){     // check for a high to low transition
       // if true then found a new button press while clock is not running - start the clock

       startTime = elapsedTime;                                  // store the start time
       delay(5);                                               // short delay to debounce switch
       lastButtonState = buttonState;                          // store buttonState in lastButtonState, to compare next time

    }

    else if (buttonState == LOW && lastButtonState == HIGH){     // check for a high to low transition
       // if true then found a new button press while clock is running - stop the clock and report

         elapsedTime =   millis(); - startTime;              // store elapsed time
         lastButtonState = buttonState;                     // store buttonState in lastButtonState, to compare next time




 
 
 
  lcd.setCursor(4,2);
  lcd.print("   ");
        // routine to report elapsed time
         lcd.print( (int)(elapsedTime / 1000UL));         // divide by 1000 to convert to seconds - then cast to an int to print

         lcd.print(".");                             // print decimal point

         // use modulo operator to get fractional part of time
        fractional = (int)(elapsedTime % 1000UL);

        // pad in leading zeros - wouldn't it be nice if
        // Arduino language had a flag for this? :)
        if (fractional == 0)
           lcd.print("000");      // add three zero's
        else if (fractional < 10)    // if fractional < 10 the 0 is ignored giving a wrong time, so add the zeros
           lcd.print("00");       // add two zeros
        else if (fractional < 100)
           lcd.print("0");        // add one zero

        lcd.print(fractional);  // print fractional part of time

    }

    else{
       lastButtonState = buttonState;
 
 
 
 
}
buttonState3 = digitalRead(alfaButton);

if (buttonState3 == HIGH){
  alfa = ++ alfaVal;
    alfaVal2 = alfa / 100 * 5;
    lcd.setCursor(2,1);     
    lcd.print(alfaVal2);

}
}
13  Using Arduino / Programming Questions / Re: Cant stop code from being run after reset on: February 03, 2013, 04:49:14 am
Thanks to everyone for helping me in this mather, and a special thanks to "HazardsMind" for solving my problem.

Here is the functioning code HazardsMind gave me:

Code:
#include <Wire.h>
 #include <LiquidCrystal.h>

 LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


 #define ledPin  13                  // LED connected to digital pin 13
 #define buttonPin 9                 // button on pin 4

 int value = LOW;                    // previous value of the LED
 int buttonState;                    // variable to store button state
 int lastButtonState;                // variable to store last button state
 long interval = 100;                // blink interval - change to suit
 long previousMillis = 0;            // variable to store last time LED was updated
 long startTime ;                    // start time for stop watch
 long elapsedTime ;                  // elapsed time for stop watch
 int fractional;                     // variable used to store fractional part of time

 boolean test = false;
 boolean start = false;
 boolean lockout = false;            //**NEW BOOLEAN**
 byte lastbuttonState2 = 0;          // new global variable
 byte buttonState1 = 0;
 byte buttonState2 = 0;

 void setup()
 {
   Serial.begin(9600);

   lcd.begin(16, 4);
   // Print a message to the LCD.
   lcd.print("Test");

   pinMode(ledPin, OUTPUT);         // sets the digital pin as output

   pinMode(buttonPin, INPUT);       // not really necessary, pins default to INPUT anyway
   digitalWrite(buttonPin, HIGH);   // turn on pullup resistors. Wire button so that press shorts pin to ground.

 }

 void loop()
 {  // no more debounce
   buttonState2 = digitalRead(buttonPin);
   if(buttonState2 == HIGH){
     if(lockout == false){ // #1
       start = true;
       startTime = millis();
       lastButtonState = buttonState2;
       lockout = true; // (The lockout) this will make sure it does not go back to this IF statement
     }
   }
   if(buttonState2 == HIGH && start == true){ // #2
     elapsedTime =   millis() - startTime;              // store elapsed time
     lastButtonState = buttonState;                     // store buttonState in lastButtonState, to compare next time

     lcd.setCursor(0,2);

     lcd.print(" ");
     // routine to report elapsed time
     lcd.print( (int)(elapsedTime / 1000UL));         // divide by 1000 to convert to seconds - then cast to an int to print

     lcd.print(".");

     fractional = (int)(elapsedTime % 1000UL);

     if (fractional == 0)
       lcd.print("000");
     else if (fractional < 10)
       lcd.print("00");
     else if (fractional < 100)
       lcd.print("0");

     lcd.print(fractional);  // print fractional part of time
   }
   else lcd.clear(); // #3
 }
14  Using Arduino / Programming Questions / Re: Cant stop code from being run after reset on: February 01, 2013, 06:15:38 pm
I am new at this so the learning curve is pretty steep.

couldnt you add the 3 lines in the code for me?
15  Using Arduino / Programming Questions / Re: Cant stop code from being run after reset on: February 01, 2013, 05:36:51 pm
I still cant get it to work...

Here is the code:

Code:
#include <Wire.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


#define ledPin  13                  // LED connected to digital pin 13
#define buttonPin 9                 // button on pin 4

int value = LOW;                    // previous value of the LED
int buttonState;                    // variable to store button state
int lastButtonState;                // variable to store last button state
long interval = 100;                // blink interval - change to suit
long previousMillis = 0;            // variable to store last time LED was updated
long startTime ;                    // start time for stop watch
long elapsedTime ;                  // elapsed time for stop watch
int fractional;                     // variable used to store fractional part of time

boolean test = false;
boolean start = false;
byte lastbuttonState2 = 0;          // new global variable
byte buttonState1 = 0;
byte buttonState2 = 0;

void setup()
{
  Serial.begin(9600);

  lcd.begin(16, 4);
  // Print a message to the LCD.
  lcd.print("Test");

  pinMode(ledPin, OUTPUT);         // sets the digital pin as output

  pinMode(buttonPin, INPUT);       // not really necessary, pins default to INPUT anyway
  digitalWrite(buttonPin, HIGH);   // turn on pullup resistors. Wire button so that press shorts pin to ground.

}

void loop()
{
  buttonState1 = digitalRead(buttonPin);
  delay(20);
 
  buttonState2 = digitalRead(buttonPin);

  if(buttonState1 == buttonState2) {
    if(buttonState2 == HIGH){
      start = true;
      startTime = millis();
      lastButtonState = buttonState2;
    }
 
  if(start == true){
  elapsedTime =   millis() - startTime;              // store elapsed time
    lastButtonState = buttonState;                     // store buttonState in lastButtonState, to compare next time

    lcd.setCursor(0,2);

    lcd.print(" ");
    // routine to report elapsed time
    lcd.print( (int)(elapsedTime / 1000UL));         // divide by 1000 to convert to seconds - then cast to an int to print

    lcd.print(".");                             

    fractional = (int)(elapsedTime % 1000UL);

    if (fractional == 0)
      lcd.print("000");     
    else if (fractional < 10)   
      lcd.print("00");       
    else if (fractional < 100)
      lcd.print("0");       

    lcd.print(fractional);  // print fractional part of time
  }
  if(test == true){
    if(buttonState1 == HIGH)
    start == true;
    else;{}
    }
  }
}
Pages: [1] 2