RTC is not running - timer

I tried to use the RTC DS1307 in my Arduino project, every time I ran my code I get this error: RTC is not running. The code should light up a LED every minutes:10sc and turn off every minutes:20s.
This is the code that I wrote:
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
RTC_DS1307 rtc;
void setup ()
{
Serial.begin(57600);
pinMode(0, OUTPUT);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
}
if (! rtc.isrunning()){
Serial.println("RTC is not running");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop () {
DateTime now = rtc.now();
lcd.setCursor(0, 2);
Serial.println(now.month(),DEC);
lcd.print(now.year(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.day(), DEC);
lcd.print(' ');
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
if (now.second() == 10 )
{
digitalWrite (0, HIGH);
Serial.println("high");
}
else if (now.second() == 20 )
{
digitalWrite (0, LOW);
Serial.println("low");
}
delay(1000);
}
I used the RTClib.h lib from Arduino site. Any idea please.

I have just had the same sort of problem with a program that had been working fine - using the same sort of code. I think the battery connection was lost, probably briefly.
I found that it definitely needs initialisation code , e.g. ...
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
to be sent after battery failure of removal. Went back to working after that.

Related

how to know where my program is reading my int

I am learning again after some years I stopped and I have a problem
the code is Supposed to to light a LED with changed time of lighting the LED just lighting non-stop without being off for a sec
int x = 0;
void setup()
{
pinMode(12, OUTPUT);
}
void loop()
{
x = 0;
while (x < 1000) {
digitalWrite(12, HIGH);
delay(1000 - x);
x = x + 100;
}
}
In the Arduino Studio there is an example sketch called "blink".
If you want to blink the built in led, use that.
If you have an external led connected to a pin, you can just replace the LED_BUILTIN macro in that example with your pin number.
Keep in mind that the reason the sketch is using a "macro" is because the pin for the built in led is different for different models of Arduino so the macro is automatically replaced with the correct pin number when you build the sketch for your specific board.
As for your code, the biggest issue is that you never turn off the led.
digitalWrite(12, LOW);
delay(1000 - x);
You need to "write low" to the pin (basically dropping pin voltage to zero) and then wait for some time before turning the led back on by writing high.
I am not sure why you added the while loop, but you probably don't need it.
The loop function already runs in a loop all the time, so if what you want is to blink the led in decreasing intervals, your code should look like this:
int x = 0;
void loop()
{
if (x < 1000) {
digitalWrite(12, HIGH);
delay(1000 - x);
digitalWrite(12, LOW);
delay(1000 - x);
x = x + 100;
} else {
x = 0;
}
}
}
Note that at some point this code will be blinking too fast for you to notice.
You may want to change the condition in if to something smaller like x < 800 so that the minimum interval is never shorter than 1/5 of a second.
byte x; // 100 ms units of blink speed ( 1000ms, 900ms, 800ms, ...100ms )
void loop() {
x = 10;
while (x > 0) {
digitalWrite(12, HIGH);
delay(100*x);
digitalWrite(12, LOW);
delay(100*x);
x--;
}
}
Blinking requires to switch the LED ON and OFF and to wait in both cases :)
BTW: each loop() run takes about 11 sec. That's fine if the arduino isn't supposed to do anything else in parallel.

How to read more than one ADC input and print in C for STM32F4

I'm using the MCU STM32F4 and I want to read the ADC inputs using Hal Library and show these on the Terminal. My ADCs inputs are running in continuous convertion mode and the convertion is called by tick of a Timer.
ADC_HandleTypeDef hadc1;
TIM_HandleTypeDef htim2;
char buffer[10];
uint32_t adc1, adc2, adc3;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
{
if(htim->Instance == TIM2)
{
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1,300);
adc1 = HAL_ADC_GetValue(&hadc1);
sprintf(buffer,"A1: %d ",(int)adc1);
CDC_Transmit_FS((uint8_t*)buffer,9);
HAL_ADC_PollForConversion(&hadc1,300);
adc2 = HAL_ADC_GetValue(&hadc1);
sprintf(buffer,"A2: %d ",(int)adc2);
CDC_Transmit_FS((uint8_t*)buffer,5);
HAL_ADC_Stop(&hadc1);
if(adc1 > 1000)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,1);
}
else
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,0);
}
}
}
This code above works if I have a single channel to monitorate. But, if I use more than one channel, the output show only the channel 2 values, like the image shown below.
I dont know what is happening. Do can you help me?

How to run multiple processes on Arduino

I'm working with an Arduino for the first time in my life for a school project.
I want to make a coffee reminder alarm, so you don't forget you made a cup of coffee for yourself.
I'm working with an FSR sensor which measures the presence of the coffee cup. When the cup is placed the LED should turn on to show you a timer has started ending with a reminding buzzer.
Right now I'm using a delay, but this isn't working as I want to. Because of the delay you can remove the cup and the timer still goes on with the buzzer at the end.
I want the LED and the timer/buzzer to go off when the cup gets removed midway the process.
I never ever programmed before in my life, and I find it quite hard to get my head around it.
Here's my code:
int fsrPin = 0;
int fsrReading;
int LEDpin = 11;
const int buzzer = 7;
void setup(void) {
Serial.begin(9600);
pinMode(LEDpin, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop(void) {
fsrReading = analogRead(0);
Serial.print("Analog reading = ");
Serial.print(fsrReading);
if (fsrReading < 10) {
Serial.println(" - No Cup");
digitalWrite(LEDpin, LOW);
noTone(buzzer);
}
else if (fsrReading > 10) {
Serial.println(" - Cup pressent");
digitalWrite(LEDpin, HIGH);
noTone(buzzer);
delay(10000);
tone(buzzer, 700);
delay(1000);
}
delay(500);
}
The Arduino library has a millis() and a micros() functions that you should be able to make use of. They report the number of milli-seconds or micro-seconds since boot.
You can set a variable to record what time one thing happened, then later see if the the current millis() is more than say 30,000 (30 seconds) after the previous recorded time.
Adafruit has a nice tutorial on this subject, which can be found here.

window Watchdog Timer STM32F4

Edited
DONE NOW .. I'll reconstruct the code, but now it's done and tested
I need to implement a timer that checks for conditions every x sec .. the problem I face that the program doesn't reset when it enters infinite loop ( away for check like if the system has been halted) ...
these links helped me .. manual from page 74 http://www2.st.com/content/ccc/resource/technical/document/reference_manual/3d/6d/5a/66/b4/99/40/d4/DM00031020.pdf/files/DM00031020.pdf/jcr:content/translations/en.DM00031020.pdf ..
and this link http://www.programmershare.com/3518407/
thanks in advance
I currently have this code :
#include "stm32f4xx.h"
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_wwdg.h>
void setup_Periph(void);
void Delay(unsigned long ms);
void Delay(unsigned long ms)
{ unsigned long i,j;
for(i=0;i<ms;i++)
for(j=0;j<1450;j++);
}
void setup_Periph(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
//port initialization
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_Init(GPIOD,&GPIO_InitStructure);
}
void ResetWatchdog(void)
{ WWDG_SetCounter(80);}
void WWDG_IRQHandler(void)
{
if (WWDG_GetFlagStatus())
{
WWDG_SetCounter(0x7f);
WWDG_ClearFlag();
}
}
void FeedDog(float round)
{
while(round)
{ Delay (65);
WWDG_SetCounter(127);
round--;}
}
int main(void)
{
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);
//System Clock auf Watchdog
RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);
WWDG_SetPrescaler(WWDG_Prescaler_8);
//WWDG clock counter = (PCLK1(30MHz)/4096)/1 = 7324 Hz (~137 us)
WWDG_SetCounter(80); //Werte 0x40 und 0x7F
WWDG_SetWindowValue(80); //0x80
//Reset < 120 > 64
WWDG_Enable(127); //WWDG timeout = ~137 us * (127-64) = 8.6ms
WWDG_ClearFlag();
WWDG_EnableIT();
setup_Periph();
//make sure the clk is stable
RCC_HSEConfig(RCC_HSE_ON);
while(!RCC_WaitForHSEStartUp());
GPIO_SetBits(GPIOD, GPIO_Pin_1);
Delay(10000); //10 ms
GPIO_ResetBits(GPIOD, GPIO_Pin_1);
Delay(10000); //100 ms
while (1)
{
GPIO_SetBits(GPIOD, GPIO_Pin_0);
Delay(10000); //10 ms
GPIO_ResetBits(GPIOD, GPIO_Pin_0);
Delay(10000); //100 ms
void ResetWatchdog(void);
WWDG_SetCounter(80);
FeedDog(8);
for(;;) {}
}
}
There are several things very obviously wrong here. Most troubling among them are:
Your Delayms() function does not implement any kind of delay. It appears to configure one of the LEDs to flash.
You are never calling InitWatchdog(). (Instead, you are declaring its prototype within main() for some reason.)
I don't want this to sound too harsh, but: do you know C? This code reads as though it's been put together by copying and pasting pieces from examples without understanding them. If you do not know C, attempting to develop software for an embedded system is not an effective way to learn it, especially without guidance.

how to interrupt arduino when data recieved via rx pin

what am I doing wrong ? I have the RX of the Bluetooth HC-07 wired into pin2 looking for a change in voltage when data is received to start a interrupt.
I'm trying to cause an interrupt when data is received via the Bluetooth.
#include <SoftwareSerial.h>// import the serial library
#include <PinChangeInt.h>
SoftwareSerial Genotronex(10, 11); // RX, TX
int ledpin=13; // led on D13 will show blink on / off
int BluetoothData; // the data given from Computer
void doSomething(void);
void setup() {
// wil run once
Genotronex.begin(9600);// data rate for comunicating
Genotronex.println("Bluetooth On please press 1 or 0 blink LED ..");
pinMode(ledpin,OUTPUT);
interrupts();
pinMode(2,INPUT);
attachInterrupt(0,doSomething, CHANGE);
}
void loop() {
// Main body of code
}
void doSomething(){
Genotronex.println("INTERRUPT!!!!!!!!!!!!");
digitalWrite(ledpin,1);
delay(1000);
digitalWrite(ledpin,0);
delay(1000);
detachInterrupt(0);
}
////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
This is a repost of the code ,, I've taken out what the comments said and added what you recommended,, for the most part. The code runs but change state never changes back it only changes once? I've tried taking out DetachInterrupt but the actual interrupt stops working then ? Any more ideas? Thanks for the help btw
#include <SoftwareSerial.h>// import the serial library
SoftwareSerial Genotronex(10, 11); // RX, TX
int ledpin=13; // led on D13 will show blink on / off
int BluetoothData; // the data given from Computer
void doSomething(void);
volatile int state = LOW;
void setup() {
// wil run once
Genotronex.begin(9600);// data rate for comunicating
Genotronex.println("Bluetooth On please press 1 or 0 blink LED ..");
pinMode(ledpin,OUTPUT);
pinMode(2,INPUT);
attachInterrupt(0,doSomething, CHANGE);
}
void loop() {
// Main body of code
digitalWrite(ledpin, state);
}
void doSomething(){
Genotronex.println("INTERRUPT!!!!!!!!!!!!");
state = !state;
detachInterrupt(0);
}
delete:
detachInterrupt(0);
If you can change interrupt from CHANGE to HIGH/LOW and add something like 'debouncing'. My version here (HIGH state interrupts):
void interruptfunction() {
//your interrupt code here
while(1){
if (digitalRead(2)==LOW){
delay(50);
if (digitalRead(2)==LOW) break;
}
}
}
Or this: (not tested)
void interruptfunction() {
//your interrupt code here
if (digitalRead(2)==HIGH) state=true;
else state=false;
while(1){
if (digitalRead(2)!=state){
delay(50);
if (digitalRead(2)!=state) break;
}
}
}
If you have to react on CHANGE try leaving detachInterrupt(0); but reattach it somewhere in your main loop.

Resources