timer naar 4digit display

algemene C code
Berichten: 4079
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: timer naar 4digit display

Berichtdoor shooter » 02 Jan 2016, 16:32

cpp code
void loop() {
// put your main code here, to run repeatedly:
display.printDualCounter(hours, minutes);
int h=hours; // dit kan geen unsigned zijn want dan kan ik niet testen op <0
int m=minutes;
for ( h ; h < 0; h--) { // let op h is wel 0 want 0:59 bestaat, dus test klopt niet en
if (h<>hours){m=59;}// test om te zien of het eerste half uur voorbij is dus je kunt beginnen met 1:30
for (m ; m < 0; m--) { //hier is een m weggevallen en de test is anders
display.printTime((h, m);
delay(1000);
};
// dit gaat vanzelf zie m = 59; // reset minute timer
};

// dat is vanzelf omdat je eerst door de for loops gaat. if(hours == 0 && minutes == 0)
display.print("FAIL");
}


je zult zien dat de tijd van 1000 niet helemaal goed is, en wat wil je minuten of uren?
nu duurt het 2 minuten
maar je gebruikt uren dus even de variabelen aanpassen naar een beetje logische namen.
probeer deze code maar eens (zal niet foutloos zijn maar ach dat is nou juist het leren.
paul deelen
shooter@home.nl

Advertisement

Berichten: 86
Geregistreerd: 31 Dec 2015, 14:11

Re: timer naar 4digit display

Berichtdoor christiaan » 19 Jan 2016, 21:23

Heb onderstaande code momenteel, werkt als een klok module.
Deze code werkt namelijk wel met de arduino mega, de vorige code die ik had niet namelijk. Alleen wie kan me helpen om die code om te bouwen zodat het ook een timer module wordt???

cpp code
//  Author:Frankie.Chu
// Date:9 April,2012
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
// Modified record:
//
/*******************************************************************************/
#include <TimerOne.h>
#include "TM1637.h"
#define ON 1
#define OFF 0

int8_t TimeDisp[] = {0x00,0x00,0x00,0x00};
unsigned char ClockPoint = 1;
unsigned char Update;
unsigned char halfsecond = 0;
unsigned char second;
unsigned char minute = 0;
unsigned char hour = 12;


#define CLK 39//pins definitions for TM1637 and can be changed to other ports
#define DIO 38
TM1637 tm1637(CLK,DIO);

void setup()
{
tm1637.set();
tm1637.init();
Timer1.initialize(500000);//timing for 500ms
Timer1.attachInterrupt(TimingISR);//declare the interrupt serve routine:TimingISR
}
void loop()
{
if(Update == ON)
{
TimeUpdate();
tm1637.display(TimeDisp);
}

}
void TimingISR()
{
halfsecond ++;
Update = ON;
if(halfsecond == 2){
second ++;
if(second == 60)
{
minute ++;
if(minute == 60)
{
hour ++;
if(hour == 24)hour = 0;
minute = 0;
}
second = 0;
}
halfsecond = 0;
}
// Serial.println(second);
ClockPoint = (~ClockPoint) & 0x01;
}
void TimeUpdate(void)
{
if(ClockPoint)tm1637.point(POINT_ON);
else tm1637.point(POINT_OFF);
TimeDisp[0] = hour / 10;
TimeDisp[1] = hour % 10;
TimeDisp[2] = minute / 10;
TimeDisp[3] = minute % 10;
Update = OFF;
}

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: timer naar 4digit display

Berichtdoor nicoverduin » 19 Jan 2016, 21:28

"de vorige code werkt niet"? Wat werkt niet?
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 86
Geregistreerd: 31 Dec 2015, 14:11

Re: timer naar 4digit display

Berichtdoor christiaan » 19 Jan 2016, 21:41

De arduino mega start mijn 4 digit display niet op.

Hieronder de originele code die ik toen functioneel had en volledig werkt op een UNO
cpp code
/*
Basic usage example

Demonstrated some of the basic functionality of the library. Initialize the display, set the backlight brightness, print some text, count from 0 to 100 and print on display and blink some text.

Note: make sure to set your serial monitor to line end: NEW LINE!

The circuit:
* connect TM1637 pin CLK to Arduino pin D4
* connect TM1637 pin DIO to Arduino pin D5
* connect TM1637 pin Vcc to Arduino pin 5V
* connect TM1637 pin GND to Arduino pin GND

Created 25 September 2015
By Bram Harmsen

https://github.com/bremme/arduino-tm1637

*/

// include the SevenSegmentTM1637 library
#include "SevenSegmentTM1637.h"
#include "SevenSegmentExtended.h"

/* initialize global TM1637 Display object
* The constructor takes two arguments, the number of the clock pin and the digital output pin:
* SevenSegmentTM1637(byte pinCLK, byte pinDIO);
*/
const byte PIN_CLK = 39; // define CLK pin (any digital pin)
const byte PIN_DIO = 38; // define DIO pin (any digital pin)
const byte d = 1000; // define D (delay) for 1000 milliseconds (= 1 second)
SevenSegmentExtended display(PIN_CLK, PIN_DIO);

unsigned long Watch, _micro, time = micros();
unsigned int Clock = 0, R_clock;
boolean Reset = false, Stop = false, Paused = false;
volatile boolean timeFlag = false;
uint8_t h;
uint8_t m;
uint8_t s;


const int powerBtn = 2;
const int setBtn = 3;
bool startGame = false; // initialize gameEnd?


void setup() {
// put your setup code here, to run once:
pinMode(powerBtn, INPUT);
pinMode(setBtn, INPUT);

//Serial.begin(9600); // initializes the Serial connection @ 9600 baud
display.begin(); // initializes the display
display.setBacklight(50); // set the brightness to 100 %
display.print("INIT"); // display INIT on the display
delay(1000); // wait 1000 ms
display.clear();

//Serial.begin(9600);
SetTimer(0,1,12); // 10 seconds
StartTimer();

}

void loop() {
// put your main code here, to run repeatedly:
int powerWaarde = digitalRead(powerBtn);
int setWaarde = digitalRead(setBtn);

CountDownTimer(); // run the timer

// this prevents the time from being constantly shown.
if (TimeHasChanged() )
{
//Serial.print(ShowHours());
//Serial.print(":");
// Serial.print(ShowMinutes());
// Serial.print(":");
// Serial.println(ShowSeconds());
ShowMinutes();
ShowSeconds();
display.printTime((uint8_t)m, s);
//Serial.print(":");
//Serial.print(ShowMilliSeconds());
//Serial.print(":");
//Serial.println(ShowMicroSeconds());
// This DOES NOT format the time to 0:0x when seconds is less than 10.
// if you need to format the time to standard format, use the sprintf() function.
}
}

boolean CountDownTimer()
{
static unsigned long duration = 1000000; // 1 second
timeFlag = false;

if (!Stop && !Paused) // if not Stopped or Paused, run timer
{
// check the time difference and see if 1 second has elapsed
if ((_micro = micros()) - time > duration )
{
Clock--;
timeFlag = true;

if (Clock == 0) { // check to see if the clock is 0
Stop = true; // If so, stop the timer
delay(100);
display.print("FAIL");
delay(10000);
}

// check to see if micros() has rolled over, if not,
// then increment "time" by duration
_micro < time ? time = _micro : time += duration;
}
}
return !Stop; // return the state of the timer
}

void ResetTimer()
{
SetTimer(R_clock);
Stop = false;
}

void StartTimer()
{
Watch = micros(); // get the initial microseconds at the start of the timer
Stop = false;
Paused = false;
}

void StopTimer()
{
Stop = true;
}

void StopTimerAt(unsigned int hours, unsigned int minutes, unsigned int seconds)
{
if (TimeCheck(hours, minutes, seconds) )
Stop = true;
}

void PauseTimer()
{
Paused = true;
}

void ResumeTimer() // You can resume the timer if you ever stop it.
{
Paused = false;
}

void SetTimer(unsigned int hours, unsigned int minutes, unsigned int seconds)
{
// This handles invalid time overflow ie 1(H), 0(M), 120(S) -> 1, 2, 0
unsigned int _S = (seconds / 60), _M = (minutes / 60);
if(_S) minutes += _S;
if(_M) hours += _M;

Clock = (hours * 3600) + (minutes * 60) + (seconds % 60);
R_clock = Clock;
Stop = false;
}

void SetTimer(unsigned int seconds)
{
// StartTimer(seconds / 3600, (seconds / 3600) / 60, seconds % 60);
Clock = seconds;
R_clock = Clock;
Stop = false;
}

int ShowHours()
{
return Clock / 3600;
}

int ShowMinutes()
{
//return (Clock / 60) % 60; // Original
m = (Clock / 60) % 60;
}

int ShowSeconds()
{
//return Clock % 60; // Original
s = Clock % 60;
}

unsigned long ShowMilliSeconds()
{
return (_micro - Watch)/ 1000.0;
}

unsigned long ShowMicroSeconds()
{
return _micro - Watch;
}

boolean TimeHasChanged()
{
return timeFlag;
}

// output true if timer equals requested time
boolean TimeCheck(unsigned int hours, unsigned int minutes, unsigned int seconds)
{
return (hours == ShowHours() && minutes == ShowMinutes() && seconds == ShowSeconds());
}

Berichten: 4079
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: timer naar 4digit display

Berichtdoor shooter » 19 Jan 2016, 22:03

dat komt omdat I2C op andere pinnen zit,
paul deelen
shooter@home.nl

Berichten: 86
Geregistreerd: 31 Dec 2015, 14:11

Re: timer naar 4digit display

Berichtdoor christiaan » 19 Jan 2016, 22:04

kan ik dat makkelijk aanpassen??

UPDATE:
Kwam hier niet verder mee: https://www.arduino.cc/en/Reference/Wire

Berichten: 4079
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: timer naar 4digit display

Berichtdoor shooter » 20 Jan 2016, 12:14

ja dat is heel makkelijk als jij even kijkt op die wire pagina, staat het nu op D4 (is eigenlijk A4 en A5)
dan zak je eeen regel en goh daar zie je ze staan voor de mega (20 en 21)
niet zo onzeker er kan eigenlijk niks kapot hoor.

en er een timer van maken, ja dat gaat zeker, halfsecond (dat zorgt voor het knipperen van de puntjes) wordt dan halfsecond--
en als deze 0 is dan wordt deze weer 2
zelfde voor seconde van 0 naar 60 en second --
etc
ik ga natuurlijk niet elke regel voorkauwen, jij moet ook een beetje bezig blijven.
overigens nogmaals de vraag wil je uren/minuten of minuten/seconden laten zien?

heb je toevallig ergens een RTC liggen, dan is het allemaal stuk eenvoudiger.
paul deelen
shooter@home.nl

Berichten: 86
Geregistreerd: 31 Dec 2015, 14:11

Re: timer naar 4digit display

Berichtdoor christiaan » 20 Jan 2016, 12:29

Heb zowel de clk als din geprobeerd op pinnen 20 en 21, maar geen resultaat helaas. Scherm blijft nog net zo zwart...

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: timer naar 4digit display

Berichtdoor nicoverduin » 20 Jan 2016, 21:12

heb je de sketch ook aangepast of zet jouw laatste versie ff hier online.
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 4079
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: timer naar 4digit display

Berichtdoor shooter » 20 Jan 2016, 21:15

ja dat wilde ik ook al vragen want de versie die hierboven staat heeft pinnummers die ver boven de UNO zitten.
paul deelen
shooter@home.nl

VorigeVolgende

Terug naar C code

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 6 gasten