User Tools

Site Tools


altivar11_counter

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
altivar11_counter [2013/03/05 00:06]
admin
altivar11_counter [2016/09/01 14:17] (current)
Line 889: Line 889:
     delay(sCd);     delay(sCd);
   }   }
 +}
 +</​file>​
 +//
 +<file ino Counter_3.0.0.ino>​
 +/*
 +Author: Dubravko Penezic
 +
 +Version: 1.0, 2012
 +
 +This code is for counting rotation.
 +
 +Source code is provided as is, without any warranty.
 +
 +Distributetd under CC BY v 3.0
 +
 +http://​pcb.daince.net/​doku.php?​id=arduino_code
 +*/
 +
 +// Format numbers to string
 +#include <​DpFormatNumber.h>​
 +DpFormatNumber fN;
 +
 +// Display settings
 +#include <​LiquidCrystal.h>​
 +#include <​DpLCDBigNumber4L.h>​
 +// LCD pins
 +int BeLi_LCD_pin = 9;
 +int RS_LCD_pin = 7;
 +int En_LCD_pin = 8;
 +int D4_LCD_pin = 13;
 +int D5_LCD_pin = 12;
 +int D6_LCD_pin = 11;
 +int D7_LCD_pin = 10;
 + 
 +// initialize the library with the numbers of the interface pins
 +LiquidCrystal lcd(RS_LCD_pin,​ En_LCD_pin, D4_LCD_pin, D5_LCD_pin, D6_LCD_pin, D7_LCD_pin);​
 +
 +DpLCDBigNumber4L lcdn(&​lcd);​
 +
 +//Switch settings
 +//Analogue value for key
 +int  adc_key_val[6] ={30, 170, 390, 570, 790, 1024 };
 +//Key function
 +int key_fun[6] = {3, //Count up
 +                  2, //Count down
 +                  4, //10x, reset
 +                  0, //Start
 +                  1  //Stop
 +                  };
 +//Number of keys
 +int NUM_KEYS = 6;
 +//Analog pin
 +int aKeyPin = 7;
 +
 +int key = 5;
 +
 +//Digital input
 +int od = 2;
 +
 +// work type - 0 setting counter
 +//             1 counting and controling electro motore
 +
 +int work_type = 0; 
 +int start_count = 0;
 +
 +// counter
 +volatile long actCounter = 0;
 +boolean counting = false;
 +char cs[7];
 +// time in miliseconds waiting for another digiti when setting counter
 +int sCd = 100;
 +int Ramp2DT = 5000;
 +
 +void setup(void) {
 +
 +  pinMode(BeLi_LCD_pin,​ OUTPUT);
 +  digitalWrite(BeLi_LCD_pin,​ HIGH);
 +
 +  attachInterrupt(0,​ countRotation,​ RISING);
 +  ​
 +  // start serial port
 +  Serial.begin(9600);​
 +
 +  Serial.println("​Counter v 2.0");
 +
 +  lcd.begin(40,​ 2);
 +  lcd.noCursor();​
 +  lcd.clear();​
 +  showStartMessage();​
 +  lcdn.setGC();​
 +}
 +
 +void loop(void) {
 +  showCounter(actCounter);​
 +  key = readKeyPad();​
 +  if(key == key_fun[4]){
 +      actCounter = 0;
 +      lcd.clear();​
 +  }  ​
 +}
 +
 +void countRotation() {
 +  actCounter++;​
 +}
 +
 +void showCounter(long counter_tmp) {
 +  if(counter_tmp<​0) fN.SigLon2ForStr(counter_tmp,​7,​true,​cs);​
 +  else fN.SigLon2ForStr(counter_tmp,​6,​true,​cs);​
 +  lcdn.printStrNo(cs,​0);​
 +}
 +
 +void showStartMessage(){
 +  lcd.setCursor(0,​ 0);
 +  lcd.print("​Counter v1.0"​);​
 +  lcd.setCursor(0,​ 1);
 +  lcd.print(" ​       2013        ");
 +  lcd.setCursor(20,​ 0);
 +  lcd.print("​Author:"​);​
 +  lcd.setCursor(20,​ 1);
 +  lcd.print("​Dubravko Penezic"​);​
 +  delay(5000);​
 +  lcd.clear();​
 +}
 +
 +int readKeyPad() {
 +  int adc_key_in;
 +  int key_begin;
 +  int key_end;
 +  ​
 +  adc_key_in = analogRead(aKeyPin); ​   // read the value from the sensor  ​
 +  key_begin = get_key(adc_key_in); ​    // convert into key press
 +  delay(50);​  ​              // wait for debounce time
 +  adc_key_in = analogRead(aKeyPin); ​   // read the value from the sensor  ​
 +  key_end = get_key(adc_key_in); ​      // convert into key press
 + 
 +  if(key_begin != key_end) return 5;   // some error, nothing is press
 +  else return key_begin;
 +}
 +
 +// Convert ADC value to key number
 +int get_key(unsigned int input) {
 + 
 +  int k;
 + 
 +  for (k = 0; k < NUM_KEYS; k++) {
 +    if (input < adc_key_val[k]) {
 +      return k;
 +    }
 +  }
 + 
 +  if (k >= NUM_KEYS)
 +    k = -1;     // No valid key pressed
 + 
 +  return k;
 } }
 </​file>​ </​file>​
altivar11_counter.txt ยท Last modified: 2016/09/01 14:17 (external edit)