Go Down

Topic: Nano + LCD + Hall sensor. Need help with code. (Read 135 times) previous topic - next topic

GytisPa

Oct 04, 2017, 11:07 pm Last Edit: Oct 05, 2017, 01:37 pm by GytisPa
Hi, I am  beginner arduino user. I want to make a project that calculate revolutions. I will use nano + lcd with I2C and hall sensor. Revoliutions I will use in two formulas and the results will be displayed on the screen. I started writing the program and simulating it in the proteus program. Everything is ok with revoliutions. But I do not get another result from formulas. Because my English is poor, so it's hard for me to understand some of the arduino functions. I'm sorry for that.
Code: [Select]

#include <LiquidCrystal.h>

// ekrano pajungimo pinai
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

//kintamieji
int seklosLikutis = 2000;
volatile unsigned long pasetasPlotas = 0;
volatile unsigned long cnt=0;

//jutiklis
const int hallPin = 2; // pin 2 = int 0

//reiksmes
static float normaPerSuki = 0.1095;
static float plotasPerSuki = 0.0476;
static char outstr[15];

// --------------------------------------------------------------------------

void doCount() // interrupt callback should be as short as possible!
{
 if(digitalRead(hallPin) == LOW)
 {
   cnt++;
   float pasetasPlotas = plotasPerSuki + pasetasPlotas;
   float seklosLikutis = normaPerSuki + seklosLikutis;
 }
}

// --------------------------------------------------------------------------
void setup()
{
 lcd.begin(16, 2);
 lcd.setCursor(1,0);
 lcd.print("Vaderstad Rapid");
 lcd.setCursor(2,1);
 lcd.print("Versija 1.0");
 delay(1000);
 lcd.clear();
 
 pinMode(hallPin,INPUT_PULLUP);
 digitalWrite(hallPin,HIGH);
 attachInterrupt(digitalPinToInterrupt(hallPin), doCount, FALLING);  // hall pin on interrupt 0 = pin 2
 digitalWrite(hallPin,HIGH);
 pinMode(hallPin,INPUT_PULLUP);
}
// --------------------------------------------------------------------------

void loop()
{
 dtostrf(pasetasPlotas,7, 4, outstr);
 lcd.setCursor(0,0);
 lcd.print("Plotas:");
 lcd.setCursor(8,0);
 lcd.print(outstr);
 lcd.setCursor(0,1);
 lcd.print("Apsisukimai:");
 lcd.setCursor(13,1);
 lcd.print(cnt);      
}

TomGeorge

Hi,


Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

GytisPa

Hi,


Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :)
Done. Thanks Tom

TomGeorge

Hi,
Your diagram is not complete, where is the hall effect sensor?
Your CAD should be able to export as a jpg file rather  using screen capture.

Thanks.. Tom.... :)
Everything runs on smoke, let the smoke out, it stops running....

GytisPa

But code is good? In proteus i am using button like a hall efect sensor.

gfvalvo

But code is good?
The code is not good. Here are the errors I found with a quick look. There may be others:

* You define variable 'pasetasPlotas' in two different places as two different variable types.

* You're accessing shared volatile variables in the 'loop()' function without first protecting them by disabling interrupts. Since these accesses are probably not atomic, you can get unreliable and unfathomable results.

* You're printing to the LCD on EVERY pass through 'loop()'. That will happen hundreds (or thousands) of times per second. That's way too often. Either add a 'delay()' -- a poor technique choice. Or, learn to use 'millis()' timers -- much better technique.

* The 'static' designation of your global variables is unnecessary. Not really an error, it doesn't hurt, but it's unnecessary.


GytisPa

#6
Oct 07, 2017, 10:17 am Last Edit: Oct 07, 2017, 10:39 am by GytisPa
Thanks for reply. Edited code:

Code: [Select]
// --------------------------------------------------------------------------
#include <LiquidCrystal.h>

// ekrano pajungimo pinai
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

//kintamieji
float seklosLikutis = 2000.0;
float pasetasPlotas = 0;

volatile unsigned long cnt=0;
unsigned long previousMillis = 0;    
const long interval = 1000;  

//jutiklis
const int hallPin = 2; // pin 2 = int 0

//reiksmes
float normaPerSuki = 0.1095;
float plotasPerSuki = 0.0476;

char str1[15];
char str2[15];
// --------------------------------------------------------------------------

void doCount() // interrupt callback should be as short as possible!
{
  if(digitalRead(hallPin) == LOW)
  {
    cnt++;
    pasetasPlotas = plotasPerSuki + pasetasPlotas;
    seklosLikutis = seklosLikutis - normaPerSuki;
  }
}

// --------------------------------------------------------------------------
void setup()
{
  lcd.begin(16, 2);
  lcd.setCursor(1,0);
  lcd.print("Vaderstad Rapid");
  lcd.setCursor(2,1);
  lcd.print("Versija 1.0");
  delay(1000);
  lcd.clear();
  
  pinMode(hallPin,INPUT_PULLUP);
  digitalWrite(hallPin,HIGH);
  attachInterrupt(digitalPinToInterrupt(hallPin), doCount, FALLING);  // hall pin on interrupt 0 = pin 2
  digitalWrite(hallPin,HIGH);
  pinMode(hallPin,INPUT_PULLUP);
}
// --------------------------------------------------------------------------

void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    
    dtostrf(pasetasPlotas,7, 4, str1);
    dtostrf(seklosLikutis,7, 4, str2);
    
    lcd.setCursor(0,0);
    lcd.print("Plotas:");
    //lcd.setCursor(8,0);
    lcd.print(str1);
    lcd.print("a");
    lcd.setCursor(0,1);
    lcd.print("Sekla:");
    //lcd.setCursor(7,1);
    lcd.print(str2);
    lcd.print("kg");
    //lcd.print("Apsisukimai:");
    //lcd.setCursor(13,1);
    //lcd.print(cnt);
  }          
}

// --------------------------------------------------------------------------

Go Up