31 Counts in percents? & continues counts...

Hi, i have a small problem, well two...

SOLVED: The first thing might be easy to solve but i don´t know what´s wrong... while i push the red button shown on the youtube-clip, i will be take measurements with a lightsensor, it takes reading with 31 pulses than prints the results in Magnitudes/arcSecond2... but i have some strange signs between "Readings" and the count itself... three horizontal lines of some kind.... i can´t see what´s wrong in the code iether :confused:

The other problem is that i only wan´t to run that code after i have pushed the button, (take 31 counts, print the result in 5000ms, clean the screen and than jump out of the loop) but as you see now, it seems like it takes the readings even after (and before) i have pushed the button, and i only have to push the button to print the result!
I will be running this thing on battery so i don´t want any unnessesary code to run when i don´t have/wan´t to.

The code:

void loop() {
   val = digitalRead(buttonSQM);
   if (val == LOW) {
      reading = 1;                       //Sätt en flagga att knappen tryckts ned så att läsning pågår
      while(reading) {                      //Gör om det här så länge flaggan för läsning är satt
         if (FreqMeasure.available()) {
            // average several reading together
            sum = sum + FreqMeasure.read();
            count = count + 1;
               lcd.clear();
               lcd.setCursor(0,0);
               lcd.println("Reading");
               lcd.setCursor(9,0);
               lcd.print(count);
               
            if (count > 30) {
               double frequency = F_CPU / (sum / count);
               sum = 0;
               count = 0;

               Msqm = A - 2.5*log10(frequency); //Egen Kod
               Serial.print(Msqm);
               Serial.println(" Mag/Arcsecond2 ");
               lcd.clear();
               lcd.setCursor(0,0);
               lcd.println("Mag/AS2:");
               lcd.setCursor(8,0);
               lcd.print(Msqm);
               delay(5000);
               lcd.clear();
               reading = 0;                    //Nolla flaggan för läsning och släpp ut ur while-loopen då läsning avslutats
            }
         }
      }
   }

Change println("reading") to just print("reading)"

Problem solved.

Oh :slight_smile: thnx... Whats the difference? What about the other problem? :-S

Do a serial print of reading and see if it does stay at 0 when the button is not being pressed. Also, is the button being pulled high?

Hi, can you show the pinMode instruction for the button pin, is it pinMode(buttonSQM, INPUT_PULLUP)?

No it doesnt stay at zero, it can start to display the count at 12 or 27 when i push the button... It is a button that is connected without any resistor... i will post the code when i am at home :slight_smile:

Now it prints the counts as it should without any vertical bars :slight_smile:
One thought though... instead of the 31 counts... can i somehow display that counts in percent? 0-100% of the reading?

Here is the ints for rest of the code:

float Msqm;
const float A = 22.0;
int buttonSQM = A2;
int reading = 0;

FreqMeasure.begin();
  pinMode(buttonSQM, INPUT);
  digitalWrite(buttonSQM, HIGH);

One thought though... instead of the 31 counts... can i somehow display that counts in percent? 0-100% of the reading?

Im not sure I understand, do you mean "percent = (count / 31)" ? If so, you may want to make it a float. What exactly is the "count" ?

HazardsMind:

One thought though... instead of the 31 counts... can i somehow display that counts in percent? 0-100% of the reading?

Im not sure I understand, do you mean "percent = (count / 31)" ? If so, you may want to make it a float. What exactly is the "count" ?

Well, as for now, it shows the counts on the LCD (it counts the "counts", witch is the number of pulses it takes from the sensor, as for now, 31 of them...) if it is possible to show in percent how many of the pulses it has counted, just like a progress-bar :slight_smile: that would be great But the other problem still isnt solved, it starts to count at 11, not 1 as it should... what could that be?

You have a simple debouncing issue and button state changing issue. Both examples are provide in the IDE.

Now it prints the counts as it should without any vertical bars

So, you changed the code?

what could that be?

Based on old code? Not going to go there.

hmm... but when i press the button, it starts to count at 11 every single time (tried at least 10 times now) how can i solve the debounce, and what is IDE? (sorry for my noobiness :P)

When a button or switch "makes", it has noise. This noise can give unwanted results if not filtered out or debounced. The Arduino software (IDE) has provided examples to use and one of them is Debounce.

Oh, so even if i am not near the button, it can initiate the code? But i have two other button and it isn´t any problem with them :S

Pssst. See reply #10.

PaulS:
Pssst. See reply #10.

Yea, i changed the println to just print and now it displays it very nice :slight_smile:

So, i have now added the debounce (at least i think i got i t right) but no change, when i push the button, it starts counting the pulses from 11...

#include <FreqMeasure.h>
#include <Math.h>
#include <LiquidCrystal.h>

float Msqm;
const float A = 22.0;
const int buttonSQM = A2;              // const added, debounce
int val = 0;
int reading = 0;
LiquidCrystal lcd (12, 11, 10, 9, 7, 6);

int buttonState;                       // debounce
int lastButtonState = LOW;             // debounce

long lastDebounceTime = 0;             // debounce
long debounceDelay = 50;               // debounce

void setup() {
   FreqMeasure.begin();
   pinMode(buttonSQM, INPUT_PULLUP);
   digitalWrite(buttonSQM, HIGH);
   lcd.begin(16,2);
}

double sum=0;
int count=0;

void loop() {
   // read the state of the switch into a local variable:
  int reading = digitalRead(buttonSQM);

  // check to see if you just pressed the button 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState = reading;
  
  
   
      reading = 1;                       //Flag the reading
      while(reading) {                      //do this while flag is set
         if (FreqMeasure.available()) {
            // average several reading together
            sum = sum + FreqMeasure.read();
            count = count + 1;
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print("Reading");
            lcd.setCursor(8,0);
            lcd.print(count);
            if (count > 30) {
               double frequency = F_CPU / (sum / count);
               sum = 0;
               count = 0;
               
               Msqm = A - 2.5*log10(frequency);
               lcd.clear();
               lcd.setCursor(0,0);
               lcd.println("Mag/AS2:");
               lcd.setCursor(8,0);
               lcd.print(Msqm);
               delay(5000);
               lcd.clear();
               reading = 0;                    //release flag and jump out of while loop
            }
         }
      }
    }
  }

Take these out, "reading = 1, reading = 0", change "count = count +1" to just "count++" OR "count += 1" both the same.
And when does "lastButtonState" get updated?

HazardsMind:
Take these out, "reading = 1, reading = 0", change "count = count +1" to just "count++" OR "count += 1" both the same.
And when does "lastButtonState" get updated?

I taken out the Reading =1 and 0, changed the count and i haven´t a clue where to put the lastbuttonstate... however, is runs not well at all, the code is running even if i push the button or not, but it doesn´t display the count on the LCD until i push the button... totally lost now im afraid :frowning:

#include <FreqMeasure.h>
#include <Math.h>
#include <LiquidCrystal.h>

float Msqm;
const float A = 22.0;
const int buttonSQM = A2;              // const added, debounce
int val = 0;
int reading = 0;
LiquidCrystal lcd (12, 11, 10, 9, 7, 6);

int buttonState;                       // debounce
int lastButtonState = LOW;             // debounce

long lastDebounceTime = 0;             // debounce
long debounceDelay = 50;               // debounce

void setup() {
   FreqMeasure.begin();
   pinMode(buttonSQM, INPUT_PULLUP);
   digitalWrite(buttonSQM, LOW);
   lcd.begin(16,2);
}

double sum=0;
int count=0;

void loop() {
   // read the state of the switch into a local variable:
  int reading = digitalRead(buttonSQM);

  // check to see if you just pressed the button 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState = reading;
  
           if (FreqMeasure.available()) {
            // average several reading together
            sum = sum + FreqMeasure.read();
            count = count += 1;
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print("Reading");
            lcd.setCursor(8,0);
            lcd.print(count);
            if (count > 30) {
               double frequency = F_CPU / (sum / count);
               sum = 0;
               count = 0;
               
               Msqm = A - 2.5*log10(frequency);
               lcd.clear();
               lcd.setCursor(0,0);
               lcd.println("Mag/AS2:");
               lcd.setCursor(8,0);
               lcd.print(Msqm);
               delay(5000);
               lcd.clear();
               lastButtonState = reading;                    
            }
         }
         
      }
   }
if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    lastbuttonState = reading; // Fixed

if(reading == 1) {
//show your count display
.
.
.
}