Omgangs tæller (Læst 1935x)

Offline Elfix

  • Spændingsdeler
  • ****
  • Indlæg: 59
  • Antal brugbare Indlæg: 1
  • Gammeldam
    • Vis profil
Omgangs tæller
« Dato: Januar 16, 2015, 20:43:26 »
Hej DEF
Jeg har lige et emne her til weekenden :)
Jeg vil gerne tælle omgange på min pillesnegl så jeg troede at jeg kunne bruge (eksempel/statechangedetection) men problemet er at den ligger inde i loppen som
aflæser mine 5 ds18b20er og en hc-sr04 i serial monitor med en delay,det virker fint; men ikke omgangstælleren da den jo kun register en impuls hvis det lige rammer når loppen køre!
jeg vil gerne regne forbruget ud da jeg ved at det er 0,033 kg. pr. omgang jeg bruge pt. et tryk
indtil min lm393 3144 kommer i næste uge (der er 2 magneter pr. omgang)
Vedhæfter lige min kode!
Håber at der er nogen der kan hjælpe >:(

Mvh Jonny og god weekend
Alle opgaver løses kun umuligheder tager lidt længere tid

 

Offline gerd

  • µProcessoren
  • *
  • Indlæg: 898
  • Antal brugbare Indlæg: 95
    • Vis profil
    • Hjemmeside med nogle af mine projekter
Sv: Omgangs tæller
« Svar #1 Dato: Januar 16, 2015, 21:31:39 »
Med en timer interrupt

Kode:
#include "TimerOne.h"
void setup()
{
  Timer1.initialize(10000);         // initialize timer1, and set a 10ms second period    
  Timer1.attachInterrupt(my_loop);  // attaches my_loop() as a timer overflow interrupt
}

void my_loop()
{
  // this is called every 10ms. Keep it as short as possible. Do not use delay() inside !!
}
 
void loop()
{
  // your program here...
}

 

Offline Elfix

  • Spændingsdeler
  • ****
  • Indlæg: 59
  • Antal brugbare Indlæg: 1
  • Gammeldam
    • Vis profil
Sv: Omgangs tæller
« Svar #2 Dato: Januar 17, 2015, 11:26:52 »
Hej Gerd

Mener du så at jeg skal ligge (hall sensor) delen op i (my_loop) og lade resten ligge i (void loop) :-\

Mvh Jonny
Alle opgaver løses kun umuligheder tager lidt længere tid

 

Offline gerd

  • µProcessoren
  • *
  • Indlæg: 898
  • Antal brugbare Indlæg: 95
    • Vis profil
    • Hjemmeside med nogle af mine projekter
Sv: Omgangs tæller
« Svar #3 Dato: Januar 17, 2015, 13:27:22 »
Ja. Prøv det.
Det kan være at en af dinne 3 moduler OneWireDallasTemperature eller NewPing har også brugt for timer1. Så virker det ikke. Men prøv det.

 

Offline Elfix

  • Spændingsdeler
  • ****
  • Indlæg: 59
  • Antal brugbare Indlæg: 1
  • Gammeldam
    • Vis profil
Sv: Omgangs tæller
« Svar #4 Dato: Januar 17, 2015, 14:43:08 »
 Hej Gerd
Nu har jeg prøvet det jeg skrev men for den kendte meddelelse (a function-definition is not allowed here before '{' token) i line 120 og 130 men jeg kan bare se hvad der er galt jeg har nok set mig blind på et eller andet

Mvh Jonny


Kode:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <NewPing.h>
#include "TimerOne.h"

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9

//HC-SR04
#define TRIGGER_PIN  7  
#define ECHO_PIN     6  
#define MAX_DISTANCE 100 

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 

//Hall_sensor
const int  buttonPin = 3;    // the pin that the pushbutton is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button


//DS18B20
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// arrays to hold device addresses
DeviceAddress udeTemp, indeTemp, kedelTemp, returTemp, varmvandsTemp;

void setup(void)
{
  Timer1.initialize(10000);         // initialize timer1, and set a 10ms second period   
  Timer1.attachInterrupt(my_loop);  // attaches my_loop() as a timer overflow interrupt
}
void my_loop()
{
  // this is called every 10ms. Keep it as short as possible. Do not use delay() inside !!
  //Hall sensor
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {

      buttonPushCounter++;
    }


    {
      // start serial port
      Serial.begin(9600);
      // Serial.println("Dallas Temperature IC Control Library Demo");

      // Start up the library
      sensors.begin();

      // locate devices on the bus
      Serial.print("Locating devices...");
      Serial.print("Found ");
      Serial.print(sensors.getDeviceCount(), DEC);
      Serial.println(" devices.");

      // report parasite power requirements
      // Serial.print("Parasite power is: "); 
      // if (sensors.isParasitePowerMode()) Serial.println("ON");
      // else Serial.println("OFF");

      // method 1: by index
      if (!sensors.getAddress(udeTemp, 1)) Serial.println("Unable to find address for Device 0");

      if (!sensors.getAddress(indeTemp, 2)) Serial.println("Unable to find address for Device 2");

      if (!sensors.getAddress(kedelTemp, 0)) Serial.println("Unable to find address for Device 0");

      if (!sensors.getAddress(returTemp, 3)) Serial.println("Unable to find address for Device 3");

      if (!sensors.getAddress(varmvandsTemp, 4)) Serial.println("Unable to find address for Device 4");


      // set the resolution to 9 bit
      sensors.setResolution(udeTemp, TEMPERATURE_PRECISION);
      sensors.setResolution(indeTemp, TEMPERATURE_PRECISION);
      sensors.setResolution(kedelTemp, TEMPERATURE_PRECISION);
      sensors.setResolution(returTemp, TEMPERATURE_PRECISION);
      sensors.setResolution(varmvandsTemp, TEMPERATURE_PRECISION);



      Serial.print("Device 1 Resolution: ");
      Serial.print(sensors.getResolution(udeTemp), DEC); 
      Serial.println();

      Serial.print("Device 2 Resolution: ");
      Serial.print(sensors.getResolution(indeTemp), DEC);
      Serial.println();

      Serial.print("Device 0 Resolution: ");
      Serial.print(sensors.getResolution(kedelTemp), DEC); 
      Serial.println();

      Serial.print("Device 3 Resolution: ");
      Serial.print(sensors.getResolution(returTemp), DEC);
      Serial.println();

      Serial.print("Device 4 Resolution: ");
      Serial.print(sensors.getResolution(varmvandsTemp), DEC);
      Serial.println();
    }

    // function to print a device address
    void printAddress(DeviceAddress deviceAddress)
    {
      for (uint8_t i = 0; i < 8; i++)

        // zero pad the address if necessary
        if (deviceAddress[i] < 16) Serial.print("0");
      Serial.print(deviceAddress[i], HEX);
    }


    void loop(void)
    { 
      sensors.requestTemperatures();

      Serial.println();
      Serial.println();
      Serial.println(" ---------------------------------");
      Serial.print(" GAMMELDAM: ");
      Serial.println(" Stoker control v.1.0");
      Serial.println(" Created by Jonny Rethmeier");
      Serial.println(" ---------------------------------");
      Serial.println();
      Serial.println();
      Serial.println();

      Serial.print(" Udetemperatur-: ");
      Serial.print(sensors.getTempC(udeTemp));
      Serial.println(" C");
      Serial.println();
      Serial.println();

      Serial.print(" Indetemperatur-: ");
      Serial.print(sensors.getTempC(indeTemp));
      Serial.println(" C");
      Serial.println();
      Serial.println();

      Serial.print(" Kedeltemperatur-: ");
      Serial.print(sensors.getTempC(kedelTemp));
      Serial.println(" C");
      Serial.println();
      Serial.println();

      Serial.print(" Returtemperatur-: ");
      Serial.print(sensors.getTempC(returTemp));

      Serial.println(" C");
      Serial.println();
      Serial.println();

      Serial.print(" Varmvandstemperatur-: ");
      Serial.print(sensors.getTempC(varmvandsTemp));
      Serial.println(" C");
      Serial.println();
      Serial.println();


      //HC-SR04
      delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
      unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
      Serial.print(" Beholdning i pilletank-: ");
      Serial.print(100-uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
      Serial.print("%");
      Serial.println();
      Serial.print(" (Minimum 15%)");
      Serial.println();

      //Hall sensor
      // read the pushbutton input pin:
      //buttonState = digitalRead(buttonPin);

      // compare the buttonState to its previous state
      //if (buttonState != lastButtonState) {
      // if the state has changed, increment the counter
      // if (buttonState == HIGH) {

      buttonPushCounter++;
      Serial.println();
      Serial.print(" Omgang af Pillesnegl: ");
      Serial.print(buttonPushCounter);
      Serial.println();

      Serial.print(" Pilleforbrug:  ");
      Serial.print(buttonPushCounter*(0.03));
      Serial.print(" kg.  ");
      Serial.println();
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;




  delay(10000);

}




Alle opgaver løses kun umuligheder tager lidt længere tid

 

Offline gerd

  • µProcessoren
  • *
  • Indlæg: 898
  • Antal brugbare Indlæg: 95
    • Vis profil
    • Hjemmeside med nogle af mine projekter
Sv: Omgangs tæller
« Svar #5 Dato: Januar 17, 2015, 15:10:08 »
Kode:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <NewPing.h>
#include "TimerOne.h"

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9

//HC-SR04
#define TRIGGER_PIN  7  
#define ECHO_PIN     6  
#define MAX_DISTANCE 100 

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 

//Hall_sensor
const int  buttonPin = 3;    // the pin that the pushbutton is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

// US sensor
unsigned int uS;

//DS18B20
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// arrays to hold device addresses
DeviceAddress udeTemp, indeTemp, kedelTemp, returTemp, varmvandsTemp;

void setup(void)
{
  // start serial port
  Serial.begin(9600);
 // Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();

  // locate devices on the bus
  Serial.print("Locating devices...");
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");

  // report parasite power requirements
  // Serial.print("Parasite power is: "); 
  // if (sensors.isParasitePowerMode()) Serial.println("ON");
  // else Serial.println("OFF");
 
  // method 1: by index
  if (!sensors.getAddress(udeTemp, 1)) Serial.println("Unable to find address for Device 0");
  
  if (!sensors.getAddress(indeTemp, 2)) Serial.println("Unable to find address for Device 2");
 
  if (!sensors.getAddress(kedelTemp, 0)) Serial.println("Unable to find address for Device 0");
  
  if (!sensors.getAddress(returTemp, 3)) Serial.println("Unable to find address for Device 3");
 
  if (!sensors.getAddress(varmvandsTemp, 4)) Serial.println("Unable to find address for Device 4");


  // set the resolution to 9 bit
  sensors.setResolution(udeTemp, TEMPERATURE_PRECISION);
  sensors.setResolution(indeTemp, TEMPERATURE_PRECISION);
  sensors.setResolution(kedelTemp, TEMPERATURE_PRECISION);
  sensors.setResolution(returTemp, TEMPERATURE_PRECISION);
  sensors.setResolution(varmvandsTemp, TEMPERATURE_PRECISION);
  
  

  Serial.print("Device 1 Resolution: ");
  Serial.print(sensors.getResolution(udeTemp), DEC); 
  Serial.println();

  Serial.print("Device 2 Resolution: ");
  Serial.print(sensors.getResolution(indeTemp), DEC);
  Serial.println();
  
  Serial.print("Device 0 Resolution: ");
  Serial.print(sensors.getResolution(kedelTemp), DEC); 
  Serial.println();

  Serial.print("Device 3 Resolution: ");
  Serial.print(sensors.getResolution(returTemp), DEC);
  Serial.println();
  
  Serial.print("Device 4 Resolution: ");
  Serial.print(sensors.getResolution(varmvandsTemp), DEC);
  Serial.println();
  
  Timer1.initialize(10000);         // initialize timer1, and set a 10ms second period   
  Timer1.attachInterrupt(my_loop);  // attaches my_loop() as a timer overflow interrupt

}

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    // zero pad the address if necessary
    if (deviceAddress[i] < 16) Serial.print("0");
   Serial.print(deviceAddress[i], HEX);
  }
}

void my_loop()
{
  // this is called every 10ms. Keep it as short as possible. Do not use delay() inside !!
  // Hall sensor
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
 
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
       buttonPushCounter++;
    }
  }
  
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;
  
}

void loop(void)
{ 
  sensors.requestTemperatures();
  
  Serial.println();
  Serial.println();
  Serial.println(" ---------------------------------");
  Serial.print(" GAMMELDAM: ");
  Serial.println(" Stoker control v.1.0");
  Serial.println(" Created by Jonny Rethmeier");
  Serial.println(" ---------------------------------");
  Serial.println();
  Serial.println();
  Serial.println();
  
  Serial.print(" Udetemperatur-: ");
  Serial.print(sensors.getTempC(udeTemp));
  Serial.println(" C");
  Serial.println();
  Serial.println();
  
  Serial.print(" Indetemperatur-: ");
  Serial.print(sensors.getTempC(indeTemp));
  Serial.println(" C");
  Serial.println();
  Serial.println();
  
  Serial.print(" Kedeltemperatur-: ");
  Serial.print(sensors.getTempC(kedelTemp));
  Serial.println(" C");
  Serial.println();
  Serial.println();
  
  Serial.print(" Returtemperatur-: ");
  Serial.print(sensors.getTempC(returTemp));
  
  Serial.println(" C");
  Serial.println();
  Serial.println();
  
  Serial.print(" Varmvandstemperatur-: ");
  Serial.print(sensors.getTempC(varmvandsTemp));
  Serial.println(" C");
  Serial.println();
  Serial.println();
  
  
  //HC-SR04
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print(" Beholdning i pilletank-: ");
  Serial.print(100-uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.print("%");
  Serial.println();
  Serial.print(" (Minimum 15%)");
  Serial.println();
  
  Serial.println();
  Serial.print(" Omgang af Pillesnegl: ");
  Serial.print(buttonPushCounter);
  Serial.println();
     
  Serial.print(" Pilleforbrug:  ");
  Serial.print(buttonPushCounter*(0.03));
  Serial.print(" kg.  ");
  Serial.println();
 
  delay(1000);
  
}

 

Offline Elfix

  • Spændingsdeler
  • ****
  • Indlæg: 59
  • Antal brugbare Indlæg: 1
  • Gammeldam
    • Vis profil
Sv: Omgangs tæller
« Svar #6 Dato: Januar 17, 2015, 16:42:42 »
Hej Gerd

Mange tak for hjælpen nu køre det bare :)!!!!! det er dejlig at værre i sådan et forum hvor der er en
som dig :)
Alle opgaver løses kun umuligheder tager lidt længere tid