|
|
Elektronika.lt portalo forumas
Jūs esate neprisijungęs lankytojas. Norint dalyvauti diskusijose, būtina užsiregistruoti ir prisijungti prie forumo.
Prisijungę galėsite kurti naujas temas, atsakyti į kitų užduotus klausimus, balsuoti forumo apklausose.
Administracija pasilieka teisę pašalinti pasisakymus bei dalyvius,
kurie nesilaiko forumo taisyklių.
Pastebėjus nusižengimus, prašome pranešti.
Dabar yra 2024 10 08, 23:48. Visos datos yra GMT + 2 valandos.
|
|
|
|
Forumas » Mikrovaldikliai » Iveikiamo rato laikmatis (arduino)
|
Jūs negalite rašyti naujų pranešimų į šį forumą Jūs negalite atsakinėti į pranešimus šiame forume Jūs negalite redaguoti savo pranešimų šiame forume Jūs negalite ištrinti savo pranešimų šiame forume Jūs negalite dalyvauti apklausose šiame forume
|
|
|
|
|
|
Iveikiamo rato laikmatis (arduino) |
Parašytas: 2020 01 27, 23:16 |
|
|
|
Sveiki visi,
gal galite padeti. Ant arduino noriu pasileisti rato laiko laikroduka. Programos kodas "skolintas" ir siektiek pritemptas prie mano idejos. Programa kaip ir veikia, bet nesualvoju kaip padaryti kad skaiciuotu tukstantasias sekundes dalis. Gal kas paprotins kaip padaryti. Yra du mygtukai, vienas paleidzia ir sustabdo, o kitas reset i 00:00:00
P.S. programuoti nemoku, vairuoti geriau sekasi. Irenginukas veikia bet norisi patobulinti.
Atsiprasau uz diletantiskus klausimus
Kodas: |
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int button1 = 9; //Mygtukas i 2 digital pin
const int button2 = 8; //Mygtukas i 3 digital pin
long lastPressedBtn1 = 0; //Last millisecond we pressed button1
long lastPressedBtn2 = 0; //Last millisecond we pressed button2;
boolean run = false; //Should the system run?
int button1State = LOW; //The value the button1 reads
int button1CurrState = LOW; //The last value of button1
int button2State = LOW; //The value of button1
int button2CurrState = LOW; //The last value of button2
long timer = 0; //The timer
int second = 0;
int minute = 0;
int tenth = 0;
/**
* SETUP
*/
void setup() {
pinMode(button1, INPUT);
pinMode(button2, INPUT);
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("Rato laikas");
lcd.setCursor(0,1);
lcd.print("Laukiam");
}
/**
* This is the timer function
* For each tenth of a second call the tick() function
*/
void tickClock() {
Serial.println(millis()/10000);
if((timer - millis()/10000) >= 10 || timer == 0) {
tick();
timer = millis()/10000;
}
}
void loop() {
tickClock(); //Start ticking the clock
button1State = digitalRead(button1);
button2State = digitalRead(button2);
checkStart();
checkClear();
}
/**
* Check for button1 press
* If the first button is pressed,
* be sure that there is at least 100 ms since the last press
*/
void checkStart() {
if(button1State != button1CurrState) { //Check if the button state has changed
if(button1State == HIGH && (millis()-lastPressedBtn1) > 100 || lastPressedBtn1 == 0) {
lastPressedBtn1 = millis();
run = !run; //Switch the running state
}
}
button1CurrState = button1State; //Set the current state equals the button state
}
/**
* Check for button 2 press
* If the second button is pressed,
* be sure that there is at least 100 since the last press
*/
void checkClear() {
if(button2State != button2CurrState) {
if(button2State == HIGH && (millis()-lastPressedBtn2) > 100 || lastPressedBtn2 == 0) {
lastPressedBtn2 = millis();
// lcd.clearLine(1) ; //Clear the display
}
}
button2CurrState = button2State; //Set btn2 current state equals the btn2State
}
/**
* Tick one tenth of a second if it should be running
*/
void tick() {
if(run) {
updateLCD();
if(tenth == 99) {
tenth = 0;
if(second == 59) {
second = 0;
minute++;
} else {
second++;
}
} else {
tenth++;
}
}
}
/**
* Update the LCD-display
* The extra 0s and : is for the sake of beauty :)
*/
void updateLCD() {
lcd.setCursor(0,1);
if(minute < 10) { // If hour does not have 2 digits
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if(second < 10) { // If minute does not have 2 digits
lcd.print("0");
}
lcd.print(second, DEC);
lcd.print(":");
lcd.print(tenth, DEC );
lcd.print(" ");
}
/**
* Clear the display
* Set all time variables equal 0
void clearAll() {
run = false;
second = 0;
minute = 0;
tenth = 0;
updateLCD();
}*/
|
|
|
Paskutinį kartą redagavo kactuz, 2020 11 25, 18:03. Redaguota 2 kartus(ų) |
|
|
|
|
|
Iveikiamo rato laikmatis (arduino) |
Parašytas: 2020 01 28, 13:53 |
|
|
|
Realiai tai jau viskas padaryta, ir darau sofware update tik tik realybeje as naudoju I2c LCD moduliuka kad maziau laiduku butu. O pavyzdyje taip pakoriagaves kad galeciau pasitikrinti ant TINKERCAD simuliatoriaus ar kaip ji pavadinti.
Aciu, uz koda. pameginsiu pasigilinti ir pritaikyti |
|
|
|
|
|
|
Iveikiamo rato laikmatis (arduino) |
Parašytas: 2020 01 28, 14:28 |
|
|
|
jei islaikai D2 ir D3 ( INT0, INT1 ) ir mygtukai i GND, tai viskas toliau ok
ankstesnis variantas turejo klaida - neatsinaujino info ekrane po stop...
Kodas: |
const int button1 = 2; // Mygtukas 2 digital pin i GND
const int button2 = 3; // Mygtukas 3 digital pin i GND
/*
const int LCD_D4 = 4;
const int LCD_D5 = 5;
const int LCD_D6 = 6;
const int LCD_D7 = 7;
const int LCD_RS = 8;
const int LCD_EN = 9;
#include <LiquidCrystal.h>
LiquidCrystal lcd(LCD_EN,LCD_RS,LCD_D4,LCD_D5,LCD_D6,LCD_D7);
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
static volatile uint32_t timer_ms=0; //The timer
static volatile uint32_t timer_last=0;
static volatile bool is_running=false;
static volatile bool do_refresh=false;
static void btn1_press(void){
detachInterrupt(digitalPinToInterrupt(button1));
if(is_running){ // stop timer_ms
timer_ms+=millis()-timer_last;
lcd.setCursor(12,1);
lcd.print(F(" run"));
is_running=false;
}else{ // start timer_ms
timer_last=millis();
lcd.setCursor(12,1);
lcd.print(F("!run"));
is_running=true;
}
_delay_ms(100); // simple debounce
attachInterrupt(digitalPinToInterrupt(button1),btn1_press,FALLING);
do_refresh=true;
}
static void btn2_press( void ) {
detachInterrupt(digitalPinToInterrupt(button2));
timer_ms=0; // clear timer_ms
timer_last=millis();
_delay_ms(100); // simple debounce
attachInterrupt(digitalPinToInterrupt(button2),btn2_press,FALLING);
}
void setup(void) {
pinMode(button1,INPUT);
digitalWrite(button1,HIGH); // with PullUp, btn to GNG
pinMode(button2,INPUT);
digitalWrite(button2,HIGH); // with PullUp, btn to GND
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print(F("Rato laikas !run"));
lcd.setCursor(0,1);
lcd.print(F("Laukiam"));
attachInterrupt(digitalPinToInterrupt(button1),btn1_press,FALLING);
attachInterrupt(digitalPinToInterrupt(button2),btn2_press,FALLING);
}
static void output(int hours,int minutes,int seconds,int hundredths){
char buf[17];
sprintf(buf,"%02ih %02im %02is %02ith",hours,minutes,seconds,hundredths);
lcd.setCursor(0,1);
lcd.print(buf);
}
void loop(void){
static uint8_t hundredths=0;
uint32_t tmp_ms;
if(is_running || do_refresh){
uint32_t current_ms=millis();
noInterrupts();
tmp_ms=timer_ms+current_ms-timer_last;
timer_ms=tmp_ms;
timer_last=current_ms;
interrupts();
hundredths=(tmp_ms/10)%100;
if(!(hundredths%20)){ // refresh @20 hundredths
do_refresh=true;
}
}
if(do_refresh){
do_refresh=false;
uint8_t seconds=tmp_ms%60;
uint8_t minutes=(tmp_ms/60)%60;
uint8_t hours=tmp_ms/3600;
output(hours,minutes,seconds,hundredths);
}
}
|
|
|
|
|
|
|
|
Iveikiamo rato laikmatis (arduino) |
Parašytas: 2020 11 24, 00:17 |
|
|
|
sveiki, vel atejo ikvepimas programinti Pataisiau kaip noreciau kad veiktu isvedimas ir koks butu tikslumas ( tukstantoji sekundes dalis )
Irasius programulke i arduina, viskas veikia, taciau ka cia skaiciuoja nerealus laikas yra. Manau kad cia tickClock su funkcija milis ta "tika" apraso ir kas kiek laiko jis buna. Bet nzn kaip cia patvarkyti kad "tikas" butu kas 1ms.
Man cia logiskai ristis turetu su taktiniu dazniu arduino, bet pavyzdziuose neradau, tai kad ir nusikalbu cia as.
Imetu mano programulke
Kodas: |
------------------------------------------------------------------------
//Veikia taip kaip noreciau, tik klaidindai laika skaiciuoja
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int button1 = 8; //Mygtukas i 2 digital pin
const int button2 = 9; //Mygtukas i 3 digital pin
long lastPressedBtn1 = 0; //Last millisecond we pressed button1
long lastPressedBtn2 = 0; //Last millisecond we pressed button2;
boolean run = false; //Should the system run?
int button1State = LOW; //The value the button1 reads
int button1CurrState = LOW; //The last value of button1
int button2State = LOW; //The value of button1
int button2CurrState = LOW; //The last value of button2
unsigned long timer = 0; //The timer
int second = 0;
int minute = 0;
int tenth = 0;
int simt = 0;
int tukst = 0;
/**
* SETUP
*/
void setup() {
pinMode(button1, INPUT);
pinMode(button2, INPUT);
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("Rato laikas");
lcd.setCursor(0,1);
lcd.print("Laukiam");
}
/**
* This is the timer function
* For each tenth of a second call the tick() function
*/
void tickClock() {
Serial.println(millis()/1);
if((timer - millis()/1) >= 10 || timer == 0) {
tick();
timer =millis()/1 ;
}
}
/**
* Loop
*/
void loop() {
tickClock(); //Start ticking the clock
button1State = digitalRead(button1);
button2State = digitalRead(button2);
checkStart();
checkClear();
}
/**
* Check for button1 press
* If the first button is pressed,
* be sure that there is at least 100 ms since the last press
*/
void checkStart() {
if(button1State != button1CurrState) { //Check if the button state has changed
if(button1State == HIGH && (millis()-lastPressedBtn1) > 100 || lastPressedBtn1 == 0) {
lastPressedBtn1 = millis();
run = !run; //Switch the running state
}
}
button1CurrState = button1State; //Set the current state equals the button state
}
/**
* Check for button 2 press
* If the second button is pressed,
* be sure that there is at least 100 since the last press
*/
void checkClear() {
if(button2State != button2CurrState) {
if(button2State == HIGH && (millis()-lastPressedBtn2) > 100 || lastPressedBtn2 == 0 ) {
lastPressedBtn2 = millis();
run = false;
minute = 0;
second = 0;
tenth = 0;
simt = 0;
tukst = 0;
updateLCD();
lcd.setCursor(0,1);
lcd.print("*Laukiam*");
}
}
button2CurrState = button2State; //Set btn2 current state equals the btn2State
}
/**
* Tick one tenth of a second if it should be running
*/
void tick() {
if(run) {
updateLCD();
if(tukst == 9) {
tukst = 0;
if(simt == 9) {
simt = 0;
if(tenth == 9) {
tenth = 0;
if(second == 59) {
second = 0;
minute++;
} else {
second++;}
} else {
tenth++;}
} else {
simt++;}
} else {
tukst++;}
}
}
/**
* Update the LCD-display
* The extra 0s and : is for the sake of beauty :)
*/
void updateLCD() {
lcd.setCursor(0,1);
if(minute < 10) { // If hour does not have 2 digits
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if(second < 10) { // If minute does not have 2 digits
lcd.print("0");
}
lcd.print(second, DEC);
lcd.print(".");
if(tenth < 10) { // If tenth does not have 2 digits
lcd.print(tenth, DEC);
}
if(simt < 10) {
lcd.print(simt, DEC);
}
if(tukst < 10) { // If tenth does not have 2 digits
lcd.print(tukst, DEC);
}
} |
|
|
Paskutinį kartą redagavo kactuz, 2020 11 25, 17:56. Redaguota 1 kartą |
|
|
|
|
|
Iveikiamo rato laikmatis (arduino) |
Parašytas: 2020 11 24, 10:55 |
|
|
|
tai paprastai valdiklis tau tiksliai 1ms neislaikys. nes vykdant programa uztrunka tam tikra kieki laiko. tau reiketu TIMER pradėti naudoti, kuris sustoja vykdyti programa ivykdo kas yra timer aprasyta (pvz laikas++; prideda 1 ms) ir toliau sukasi programa. |
|
_________________ Skype: dmb-220 |
|
|
|
|
Iveikiamo rato laikmatis (arduino) |
Parašytas: 2020 11 25, 18:02 |
|
|
|
Gal galetumete issamiau papasakoti, as programuoti visai nemoku. Tiesiog rasta programa pritempti noriu iki man norimo varianto.
Siaip jei skaiciuoja programa, tik desimtasias ir simtasias viskas OK . Bet kai dadejau kad skaiciuotu ir tukstantaja dalį sekundes veikia jau kreivai.
Tikslumas yra toks
10 s realaus laiko yra 00:00:600 mano laikmacio
Tiksliai veikianti programa skaiciuojanti simtasias ir desimtasias sekundes dalis yra pirmame poste. |
|
|
|
|
|
|
Iveikiamo rato laikmatis (arduino) |
Parašytas: 2020 11 25, 18:47 |
|
|
|
Kodas: |
volatile uint16_t milisekundes=0;
volatile uint8_t sekundes=0;
volatile uint8_t minutes=0;
volatile uint8_t valandos=0;
ISR (TIMER1_OVF_vect){ // arba CAPTA/B
uint16_t tmp16;
if (999>(tmp16=milisekundes)) milisekundes=tmp16+1;
else{
uint8_t tmp8;
milisecundes=0;
if (59>(tmp8=sekundes)) sekundes=tmp8+1;
else{
if (59>(tmp8=minutes)) minutes=tmp8+1;
else{
if (23>(tmp8=valandos)) valandos=tmp8+1;
else {
valandos=0;
}
minutes=0;
}
sekundes=0;
}
}
}
void setup(){
// nustatai iejimus/isejimus
// nustatai Timer1 pagal ICR1 arba autoload
}
void main(){
if (button1debounced){
milisekundes=0;
sekundes=0;
minutes=0;
valandos=0;
timer1start();
}
if (button2debounced){
timer1stop();
}
// isvedimas
noInterrupts();
uint16_t _milisekundes=milisekundes;
interrupts();
if (!(milisekundes%100)){
uint8_t _sekundes=sekundes;
uint8_t _minutes=minutes;
uint8_t _valandos=valandos;
// toliau ijunk fantazija
}
}
void timer1start(){
// nustatai darbui su autoload arba per ICR1
// kazkur programoje dar galesi ta autoload reiksme
// pasikoreguoti, kad gautum tiksliai 1ms trukme
// niekas netrukdo paderinama kondensatoriu prikabinti prie kvarco
}
// ir t.t.
|
|
|
|
|
|
|
|
Iveikiamo rato laikmatis (arduino) |
Parašytas: 2020 11 25, 19:16 |
|
|
|
Atsiprasau as nesu tiek pazenges, as bandziau aiskintis ta Jusu praeita pavizdi, bet pasiklydau. Sukompiliavus klaidu nebuvo, bet neveikia. Visai nieko neisveda net I LCD |
|
|
|
|
|
|
Google paieška forume |
|
|
Naujos temos forume |
|
|
FS25 Tractors
Farming Simulator 25 Mods,
FS25 Maps,
FS25 Trucks |
|
ETS2 Mods
ETS2 Trucks,
ETS2 Bus,
Euro Truck Simulator 2 Mods
|
|
FS22 Tractors
Farming Simulator 22 Mods,
FS22 Maps,
FS25 Mods |
|
VAT calculator
VAT number check,
What is VAT,
How much is VAT |
|
LEGO
Mänguköök,
mudelautod,
nukuvanker |
|
Thermal monocular
Thermal vision camera,
Night vision ar scope,
Night vision spotting scope |
|
FS25 Mods
FS25 Harvesters,
FS25 Tractors Mods,
FS25 Maps Mods |
|
Dantų protezavimas
All on 4 implantai,
Endodontija mikroskopu,
Dantų implantacija |
|
FS25 Mods
FS25 Maps,
FS25 Cheats,
FS25 Install Mods |
|
GTA 6 Weapons
GTA 6 Characters,
GTA 6 Map,
GTA 6 Vehicles |
|
FS25 Mods
Farming Simulator 25 Mods |
|
|
|