Jump to content
ELFORUM - Forumul electronistilor

Numarator impulsuri-varianta niq_ro


daniels

Recommended Posts

Vreau sa fac o masina de bobinat la care sa setez un numar de spire, masina le bobineaza, iar la final se opreste

Nu stiu ce biblioteca LiquidCrystal_I2C.h foloseste, dar initial nu tiparea nimic. dupa ce am schimbat linia 

  lcd.begin(16,2); // initialize the LCD

cu 

lcd.init();                      // initialize the lcd 
  lcd.init();

tipareste doar liniile 

  lcd.backlight(); // Turn on the blacklight and print a message.
  lcd.setCursor(0,0);
  lcd.print(" Sistem automat ");
  lcd.setCursor(0,1);
  lcd.print("sw: Nicu FLORICA");

Varianta originala a softului

/*
counter with threshold
original sw by Nicu FLORICA (niq_ro)
*/

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
  
volatile int provimp; // measures sensor pulses
unsigned int totalimp = 0;  // total impulses;
unsigned int totalimp0 = 10;  // previous impulses;
int prag = 50;  // default threshold

#define senzor 2 // Sensor Input (D2)
#define sw1 12  // pin for increase the threshold 
#define sw2 11 // pin for decrease the threshold 
#define releu 13 // pin for relay

#define atras LOW
#define repaos HIGH


void numarare() // Interrupt function
   {
   provimp++;
   }

byte starebuton1,starebuton2;             // the current reading from the input pin
byte citirestarebuton1, citirestarebuton2;   // 
byte ultimastarebuton1 = HIGH;   // the previous reading from the input pin
byte ultimastarebuton2 = HIGH;
unsigned long ultimtpdebounce1 = 0;  // the last time the output pin was toggled
unsigned long ultimtpdebounce2 = 0;
unsigned long tpdebounce = 50;    // the debounce time; increase if the output flickers
unsigned long tpactionare = 0;  // momment at activate relay
unsigned long tpcomanda = 1000;  // activate relay time in ms

void setup()
{
  lcd.begin(16,2); // initialize the LCD
  lcd.clear(); // clear the screen
  lcd.backlight(); // Turn on the blacklight and print a message.
  lcd.setCursor(0,0);
  lcd.print(" Sistem automat ");
  lcd.setCursor(0,1);
  lcd.print("sw: Nicu FLORICA");
  
  pinMode(senzor, INPUT);
  digitalWrite(senzor, HIGH); // Optional Internal Pull-Up
  pinMode(sw1, INPUT);
  pinMode(sw2, INPUT);
  digitalWrite(sw1, HIGH);
  digitalWrite(sw2, HIGH);  
  pinMode(releu, OUTPUT);
  digitalWrite(releu, repaos);

   delay(3000);
   lcd.clear();
   Serial.begin(9600);
   Serial.println(" ");Serial.println(" ");Serial.println(" ");
   Serial.println("------------------------------------------");    
   Serial.println("counter ");
   attachInterrupt(0, numarare, RISING); // Setup Interrupt
   sei(); // Enable interrupts
}

void loop ()
{

// button reading
citirestarebuton1 = digitalRead(sw1);  // read the state of the switch into a local variable: 
  if (citirestarebuton1 != ultimastarebuton1) // If the switch changed, due to noise or pressing:
  {
    ultimtpdebounce1 = millis();  // reset the debouncing timer
  }
  if ((millis() - ultimtpdebounce1) > tpdebounce) 
  {
    if (citirestarebuton1 != starebuton1) // if the button state has changed
    {
      starebuton1 = citirestarebuton1;         
      if (starebuton1 == LOW) // only toggle the LED if the new button state is LOW
      {
        Serial.println("sw1");
        prag = prag + 5;
        if (prag > 100) prag = 5;
        totalimp = 0;  
      }
    }
  }

citirestarebuton2 = digitalRead(sw2);  // read the state of the switch into a local variable: 
  if (citirestarebuton2 != ultimastarebuton2) // If the switch changed, due to noise or pressing:
  {
    ultimtpdebounce2 = millis();  // reset the debouncing timer
  }
  if ((millis() - ultimtpdebounce2) > tpdebounce) 
  {
    if (citirestarebuton2 != starebuton2) // if the button state has changed
    {
      starebuton2 = citirestarebuton2;         
      if (starebuton2 == LOW) // only toggle the LED if the new button state is LOW
      {
      Serial.println("sw2");
      prag = prag - 5; 
      if (prag < 5) prag = 100;
      totalimp = 0;
      }
    }
  }

// count part
      totalimp = totalimp + provimp;  // count all impulses
      provimp = 0; // Reset Counter
      if (totalimp != totalimp0)
      {
      Serial.print(totalimp, DEC); // Print litres/hour
      Serial.println(" pulses -> ");
      Serial.println("------------------------------------------");   
      }
      lcd.setCursor(0,0);
      //lcd.print("n: "); 
      if (totalimp < 1000) lcd.print(" ");
      if (totalimp < 100) lcd.print(" ");
      if (totalimp < 10) lcd.print(" ");
      lcd.print(totalimp,DEC); // Print total pulses  
      lcd.print(" / ");
      lcd.print(prag); // Print threshold
      lcd.print("   ");
totalimp0 = totalimp;

// decision part
if (totalimp >= prag)
{
  totalimp = 0;
  digitalWrite(releu, atras);
  lcd.setCursor(15,0);
  lcd.print("*");
  tpactionare = millis();
}
if (millis() - tpactionare > tpcomanda)
{
  digitalWrite(releu, repaos);
  lcd.setCursor(15,0);
  lcd.print(" ");
}

ultimastarebuton1 = citirestarebuton1;
ultimastarebuton2 = citirestarebuton2;             

}  // end main loop

si varianta putin modificata

/*
counter with threshold
original sw by Nicu FLORICA (niq_ro)
*/

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
  
volatile int provimp; // measures sensor pulses
unsigned int totalimp = 0;  // total impulses;
unsigned int totalimp0 = 10;  // previous impulses;
int prag = 50;  // default threshold

#define senzor 2 // Sensor Input (D2)
#define sw1 12  // pin for increase the threshold 
#define sw2 11 // pin for decrease the threshold 
#define releu 13 // pin for relay

#define atras LOW
#define repaos HIGH


void numarare() // Interrupt function
   {
   provimp++;
   }

byte starebuton1,starebuton2;             // the current reading from the input pin
byte citirestarebuton1, citirestarebuton2;   // 
byte ultimastarebuton1 = HIGH;   // the previous reading from the input pin
byte ultimastarebuton2 = HIGH;
unsigned long ultimtpdebounce1 = 0;  // the last time the output pin was toggled
unsigned long ultimtpdebounce2 = 0;
unsigned long tpdebounce = 50;    // the debounce time; increase if the output flickers
unsigned long tpactionare = 0;  // momment at activate relay
unsigned long tpcomanda = 1000;  // activate relay time in ms

void setup()
{
lcd.init();                      // initialize the lcd 
  lcd.init();
  lcd.clear(); // clear the screen
  lcd.backlight(); // Turn on the blacklight and print a message.
  lcd.setCursor(0,0);
  lcd.print(" Sistem automat ");
  lcd.setCursor(0,1);
  lcd.print("sw: Nicu FLORICA");
  
  pinMode(senzor, INPUT);
  digitalWrite(senzor, HIGH); // Optional Internal Pull-Up
  pinMode(sw1, INPUT);
  pinMode(sw2, INPUT);
  digitalWrite(sw1, HIGH);
  digitalWrite(sw2, HIGH);  
  pinMode(releu, OUTPUT);
  digitalWrite(releu, repaos);

   delay(3000);
   lcd.clear();
   Serial.begin(9600);
   Serial.println(" ");Serial.println(" ");Serial.println(" ");
   Serial.println("------------------------------------------");    
   Serial.println("counter ");
   attachInterrupt(0, numarare, RISING); // Setup Interrupt
   sei(); // Enable interrupts
}

void loop ()
{

// button reading
citirestarebuton1 = digitalRead(sw1);  // read the state of the switch into a local variable: 
  if (citirestarebuton1 != ultimastarebuton1) // If the switch changed, due to noise or pressing:
  {
    ultimtpdebounce1 = millis();  // reset the debouncing timer
  }
  if ((millis() - ultimtpdebounce1) > tpdebounce) 
  {
    if (citirestarebuton1 != starebuton1) // if the button state has changed
    {
      starebuton1 = citirestarebuton1;         
      if (starebuton1 == LOW) // only toggle the LED if the new button state is LOW
      {
        Serial.println("sw1");
        prag = prag + 5;
        if (prag > 100) prag = 5;
        totalimp = 0;  
      }
    }
  }

citirestarebuton2 = digitalRead(sw2);  // read the state of the switch into a local variable: 
  if (citirestarebuton2 != ultimastarebuton2) // If the switch changed, due to noise or pressing:
  {
    ultimtpdebounce2 = millis();  // reset the debouncing timer
  }
  if ((millis() - ultimtpdebounce2) > tpdebounce) 
  {
    if (citirestarebuton2 != starebuton2) // if the button state has changed
    {
      starebuton2 = citirestarebuton2;         
      if (starebuton2 == LOW) // only toggle the LED if the new button state is LOW
      {
      Serial.println("sw2");
      prag = prag - 5; 
      if (prag < 5) prag = 100;
      totalimp = 0;
      }
    }
  }

// count part
      totalimp = totalimp + provimp;  // count all impulses
      provimp = 0; // Reset Counter
      if (totalimp != totalimp0)
      {
      Serial.print(totalimp, DEC); // Print litres/hour
      Serial.println(" pulses -> ");
      Serial.println("------------------------------------------");   
      }
      lcd.setCursor(0,0);
      //lcd.print("n: "); 
      if (totalimp < 1000) lcd.print(" ");
      if (totalimp < 100) lcd.print(" ");
      if (totalimp < 10) lcd.print(" ");
      lcd.print(totalimp,DEC); // Print total pulses  
      lcd.print(" / ");
      lcd.print(prag); // Print threshold
      lcd.print("   ");
totalimp0 = totalimp;

// decision part
if (totalimp >= prag)
{
  totalimp = 0;
  digitalWrite(releu, atras);
  lcd.setCursor(15,0);
  lcd.print("*");
  tpactionare = millis();
}
if (millis() - tpactionare > tpcomanda)
{
  digitalWrite(releu, repaos);
  lcd.setCursor(15,0);
  lcd.print(" ");
}

ultimastarebuton1 = citirestarebuton1;
ultimastarebuton2 = citirestarebuton2;             

}  // end main loop

20251004-154440.jpg

 

Edited by daniels
Link to comment
Posted (edited)

 

Initial arata 1. Senzorul este hall si daca dau cu degetul pe sub placuta incepe sa numere. Butoanele nu functioneaza.

Captur.png

20251004-170423.jpg

 

Edited by daniels
Link to comment
nico_2010

Senzorul Hall merge pe baza de magnet, nu pe baza de deget. Ce senzor ai? Care din variantele de sketch ai folosit-o?

Link to comment
44 minutes ago, daniels said:

Senzorul este hall si daca dau cu degetul pe sub placuta incepe sa numere.

La ce interval vin mesajele in consolă ?
Poti afișa si timestamp. Iconiță in dreapta la fereastra consolă.

Link to comment

Fiecare rand cu pulses-> este o atngere de deget pe sub CI. Numara foarte repede.

Link to comment
2 hours ago, daniels said:
provimp++;

Comentează linia asta din void numarare()
si vezi daca merg butoanele. Adică ce se afisează in consolă.
Se pare că programul tău intră foarte des în intrerupere din cauza senzorului.
Eventual dezactivează intreruperea HAL, sau deconectează senzorul și apasă pe butoane.
Ce se afisează in consolă ?

 

 

Edited by mihaiaurul
Link to comment

daca comentez proimp++ nu mai nunara impulsuri.

Captur.png

Daca scot senzorul incep sa functioneze butoanele.

Captur-1.png

 

Link to comment
20 minutes ago, daniels said:

Daca scot senzorul incep sa functioneze butoanele.

Este clar atunci.
Programul tău stă mai mult în intrerupere decăt să ruleze.
De ce asta ? Habar nu am.
Poate nu corespunde senzorul, poate nu este bine filtrată tensiunea de alimentare ...
Pentru teste, pune un C de 1-10nF pe pinul de intrerupere și GND.
Un osciloscop te-ar fi scos din impas.

Edited by mihaiaurul
Link to comment
Posted (edited)

Erau conectate corect, dar pt ca am ajuns sa suspectez intrerupatoarele, am scos un fir negru la masa cu care atingem direct firele din arduino.

In ultima captura erau montate corect si se vede ca functionau bine.

Edited by daniels
Link to comment
4 minutes ago, daniels said:

In ultima captura erau montate corect si se vede ca functionau bine.

Nu-ți face probleme despre butoane.
Programul tău intră in intreruperea numarare() foarte des și nu dă voie programului principal să ruleze.
Acestă întrerupere este generată de pinul legat la senzoul HAL.
Acolo este problema.

Link to comment

senzorul e conectat la Arduino cu aimentare si pe iesirea digitala la D2 al lui Arduino?

senzor.... Arduino

===============

A0 ........... liber

G   ........... GND

+  ............. 5V

D0 ............ D2

 

 

Edited by niq_ro
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.Terms of Use si Guidelines