working with delays on arduino - arduino-uno

I have just started playing with arduino. I am a little curious as to the delay(1000) method. Below is a very simple project, where i was just trying to print int i at the default loop rate, yet have the led on port 13
int led = 13;
int i =0;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
Serial.begin(9600);
Serial.println("Hello");
}
// the loop routine runs over and over again forever:
void loop() {
i++;
Serial.println(i); // wait for a second
blink(1000);
}
void blink(int duration){
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(duration); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(duration);
}
I moved the actual blink code to its own method in hopes that the delay was actually acting like a pause for the method its in, but it slows down my print as well.
Is there a way to have the lights blink at one rate with out effecting the rate of the loop?

Related

Execution time of a while loop in Arduino Due doesn't match what expected

I wrote the following infinite loop consisting principally of five high-level instructions and ran it on an Arduino Due. Assuming one clock cycle per instruction I was expecting the LED to flash about every 5/6 second. Instead the time is above 30 seconds. Is there something slowing down the execution of this code?
#define LED_PIN 13
/////////////////////////////////////////////////////////////////////////
void setup()
{
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
/////////////////////////////////////////////////////////////////////////
noInterrupts();
}
/////////////////////////////////////////////////////////////////////////
void loop()
{
int cnt = 0;
/////////////////////////////////////////////////////////////////////////
noInterrupts();
while(true) // infinite loop
{
if(cnt == 0) // if cnt == 0 turn off led
{
interrupts();
digitalWrite(LED_PIN, LOW);
noInterrupts();
}
// clock rate 84000000 cycles per second
if(cnt == VARIANT_MCK) // if cnt is equal to clock rate flash on led
{
interrupts();
delay(10);
digitalWrite(LED_PIN, HIGH);
delay(10); // turn on led for a small amount of time
noInterrupts();
cnt = - 1; // reset counter
}
cnt ++ ; // increment counter
}
}
/////////////////////////////////////////////////////////////////////////
I were expecting the number of seconds roughly shows, how many clock ticks it takes to do
while (true) {
cnt++;
if (cnt == 0) {/* signalTheStart(); */}
if (cnt == VARIANT_MCK) {/* signalTheEnd(); */}
}
One 32-bit int increment, two failing int comparisons, and a jump back.
Not sure how often the variable is moved to / from memory and how a comparison with a 32 bit constant (?) is actually performed, and I do not know the ARM internals. But I agree, 30 ticks is a bit more than I expected. I guess by 5/6you mean "five or six" and not a bit less than one, but both interpretations are too optimistic.
Michael Zhilin's hint "Check assembler code" from the compiled binary is the way to go...
And, why do you need that? You already noticed that you need to inhibit interrupts, so this measurement is not applicable to real world scenarios at all...
Writing the code in assembler solved the problem. By comparing the on-off period of the led, the compiler generate about ten time the instructions i wrote directly in assembler.

RPM meter and send value with serial communication

I have a question about how to reading RPM and send value with serial
It's my code:
char init(void)
{
UBRRH=(uint8_t) (UBRR_CALC>>8);
UBRRL=(uint8_t) UBRR_CALC;
UCSRB=(1<<RXEN)|(1<<TXEN);
UCSRC=(1<<URSEL)|(3<<UCSZ0);
return 0;
}
void send(unsigned char x)
{
while(!(UCSRA&(1<<UDRE))){}
UDR=x;
}
void sendstring(char *s)
{
while(*s)
{
send(*s);
s++;
}
}
volatile uint16_t count=0; //Main revolution counter
volatile uint16_t rps=0; //Revolution per second
void Wait()
{
uint8_t i;
for(i=0;i<2;i++)
{
_delay_loop_2(0);
}
}
int main(void)
{
Wait();
Wait();
Wait();
Wait();
//Init INT0
MCUCR|=(1<<ISC01); //Falling edge on INT0 triggers interrupt.
GICR|=(1<<INT0); //Enable INT0 interrupt
//Timer1 is used as 1 sec time base
//Timer Clock = 1/1024 of sys clock
//Mode = CTC (Clear Timer On Compare)
TCCR1B|=((1<<WGM12)|(1<<CS12)|(1<<CS10));
//Compare value=976
OCR1A=976;
TIMSK|=(1<<OCIE1A); //Output compare 1A interrupt enable
//Enable interrupts globaly
sei();
while(1)
{
}
}
ISR(INT0_vect)
{
//CPU Jumps here automatically when INT0 pin detect a falling edge
count++;
}
ISR(TIMER1_COMPA_vect)
{
//CPU Jumps here every 1 sec exactly!
rps=count;
send(rps);
count=0;
}
Can we send serial data when interrupt counter and timer, i have error when try it ?
And what is the best methode to read RPM and send value with serial
One potential problem is that rps is defined as int16_t and your send routine is expecting an unsigned char.
Is the range 0-255 high enough for your RPS?
Do you want the RPS to be human readable or are you sending it to another computer or micro-controller?
It is possible to send serial in you interrupt routines but you need to be careful since the serial routines are not usually re-entrant.

ATMega2560 PCINT Interrupts

i am new to Atmel, so maybe the question is quite simple.
I have the following situation:
I have an ATMega2560 and want to get Interrupts on the Pins PK0-PK7. I am interested in the PIN Change from Low to HIGH. (I have connected one photocell to every PIN)
I have read that the interrupt for PCINT[0-2] are fired everytime (pin high and pin low) so i defined an array to ignore the PIN DOWN Interrupt.
So i have the following code:
Note: I do not understand, why i need to set DDRC as input and not DDRK ?
#define HIGH 1
#define LOW 0
volatile uint8_t portbhistory = 0xFF;
uint8_t pinState[8];
void initSystem()
{
int i;
for(i = 0; i < sizeof(pinState) / sizeof(*pinState); i++)
{
pinState[8] = 0;
}
DDRB = 0xff; // set Port B as output
DDRC = 0; // WHY DDRC ?? PCINT == PK0-7, so DDRK ? // all Pins input
PORTC = 0xff; // same question ... // turn on pullup for every pin
PCICR |= _BV(PCIE2); // enable interrupt for PCIE2
PCMSK2 = 0xff; // Interrupt at all pins
sei(); // turn on interrupts
}
int main(void)
{
initSystem();
while(1)
{
}
}
ISR(PCINT2_vect)
{
int i;
uint8_t changedbits;
changedbits = PINC ^ portbhistory;
portbhistory = PINC;
/*
if(pinState[changedbits] == HIGH)
{
pinState[changedbits] = LOW;
return;
} else
{
pinState[changedbits] = HIGH;
}
*/
setDebugLED(1);
_delay_ms(30);
setDebugLED(0);
}
If i connect 5V to one of the PINS (PK0-7) the LED flashes instantly. But if i disconnect the 5V it takes ~2seconds for the LED to flash again. In this time the one pin i connected 5V before does not let the LED flash again if i connect the 5 V again.
Other Ports work in this time.
So you could say, that the PIN HIGH interrupt for all pins work fine and get fired instantly but the PIN LOW interrupts need some time (~2sec.) In this time the port is "disabled" or somethig.
Can anyone help me with that?
EDIT
Just some wrong edit ...
EDIT 2
So, i totally forgot to post my solution here. sorry for that!
The solution is: My external circuit pulls the PIN to Ground or to 5V.
Thank you!

No output on pins in pic24f

I can't get my pic24f04kl100 to turn on an LED at all. The below code is as simple as possible and it still doesn't turn on the LED on pin 6.
Code
#include <xc.h>
#define LED LATBbits.LATB4
#define LEDans ANSBbits.ANSB4
#define LEDtris TRISBbits.TRISB4
/* Setting up configuration bits */
_FOSCSEL(FNOSC_FRCPLL & IESO_OFF); // FRC w/PLL and int./ext. switch disabled
_FOSC(POSCMD_XT & FCKSM_CSECMD); // Pri. OSC XT mode and clk. switch on, fail-safe off
_FWDT(FWDTEN_OFF); // Watchdog timer off
void initialise();
void delay(int i);
void main() { // Main program loop
initialise(); // Intialise PIC
while (1) { // Infinite loop
LED = 1; // Set LED high
LED = 0; // Set LED low
}
}
void initialise() { // Configures the PIC
OSCCONbits.NOSC = 0b111; // Fast RC Oscillator with Postscaler and PLL module
delay(100);
CLKDIVbits.RCDIV = 0b000; // Set clock div 1:1
delay(100);
LEDans = 0;
delay(100);
LEDtris = 0; // Make LED an output
delay(100);
LED = 0; // Set LED low
}
void delay(int i) {
while(i--);
}
PICkit 3 Output
*****************************************************
Connecting to MPLAB PICkit 3...
Firmware Suite Version.....01.27.04
Firmware type..............dsPIC33F/24F/24H
Target detected
Device ID Revision = 0
The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x3ff
configuration memory
Programming...
Programming/Verify complete
By default B4 pin is analog. Configure it as digital by clearing the ANSB register, bit4
NOTE: Although clearing the bit DID NOT fix the problem. Moving to another pin (with less features) did. So I (fossum) made the assumption that this was at least on some level the correct answer.
The LED is blinking, but it's blinking very fast, try to put some delay between LED turn on and LED turn off.
Try this:
void main() { // Main program loop
initialise(); // Intialise PIC
while (1) { // Infinite loop
LED = 1; // Set LED high
delay(50000); //wait LED on time
LED = 0; // Set LED low
delay(50000); //wait LED off time
}
}

ARDUINO Buzzer playing sound when it shouldn't

So I made this kind of a temperature/humidity sensor and decided to add a fire sensor feature. So it all works fine, right? No, I also decided I want a buzzer to that. Tested it, works well, so I thrown it into my project.
Started it up [project], lit up a lighter, works fine, LEDs blink, a text is shown, buzzer plays an alarm. But then, after the fire was out, the buzzer continued to play one of two tones, even though there was no fire. Here's the code, to make it all clear:
#include <LiquidCrystal.h>
#include <dht.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
dht DHT;
#define FLAME 13
#define DHT11_PIN 8
#define ALARM A5
const int a9 = 9, a10 = 10, a11 = 11, a12 = 12;
byte z[8] = {
B00100,
B00000,
B11111,
B00001,
B00010,
B00100,
B11111,
B00000,
};
byte st[8] = {
0b00110,
0b01001,
0b01001,
0b00110,
0b00000,
0b00000,
0b00000,
0b00000,
};
void setup(){
lcd.begin(16, 2);
lcd.createChar(0, st);
lcd.createChar(1, z);
Serial.begin(9600);
pinMode((a9, a10, a11, a12), OUTPUT);
pinMode(FLAME, INPUT);
pinMode(ALARM, OUTPUT);
}
void loop() {
// Flame sensor code for Robojax.com
int fire = digitalRead(FLAME);// read FLAME sensor
if(fire == HIGH)
{
analogWrite(a9, 255);
analogWrite(a10, 255);
lcd.setCursor(5, 0);
lcd.print("Po");
lcd.print(char(1));
lcd.print("ar!");
tone(ALARM, 4300);
delay(150);
analogWrite(a9, 0);
analogWrite(a10, 0);
lcd.clear();
tone(ALARM, 3500);
delay(150);
} else {
lcd.createChar(0, st);
int chk = DHT.read11(DHT11_PIN);
lcd.home();
lcd.print("Temp.: ");
lcd.print(DHT.temperature);
if(DHT.temperature >= 20.00 && DHT.temperature < 25) {
analogWrite(a11, 255);
delay(750);
analogWrite(a11, 0);
delay(750);
} else if(DHT.temperature >= 25 && DHT.temperature < 30) {
analogWrite(a10, 255);
delay(250);
analogWrite(a10, 0);
delay(250);
} else if(DHT.temperature >= 30) {
analogWrite(a9, 255);
}
lcd.print(char(0));
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Wilg.: ");
lcd.print(DHT.humidity);
if(DHT.humidity >= 45.00 && DHT.humidity < 60.00) {
digitalWrite(a12, HIGH);
delay(250);
digitalWrite(a12, LOW);
delay(250);
} else if(DHT.humidity >= 60.00) {
digitalWrite(a12, HIGH);
}
lcd.print(" %");
delay(750);
}
delay(200);
}
So, what I mean is that even though fire is changed from HIGH to LOW and other part of the code is executed, the buzzer continues to play.
What am I doing wrong?
Check the documentation for tone:
A duration can be specified, otherwise the wave continues until a call
to noTone().
So, you probably need to reset your tone on every loop beginning before checking the values of the sensors (or explicitly invoke notone on the LOW branch), or use the 3-arg version of tone.
I have no idea how tone and delay might interact, though.
I'd probably make the actual tone sounding routine its own task. The idea is that you would start the alarm task and then reset it when appropriate. But for now make sure your state is reset at the beginning of every loop, or each branch resets anything it has to explicitly, if tasks sound too advanced.
Make sure you learn the basic "print" debugging in the Arduino world. Simple write logging to the serial port where it can be read on the IDE is invaluable.
Finally: use functions! If you put everything in loop your programs will get unreadable as they grow. For example, if you have some state you want to reset every time (e.g., running tasks like tones, values, flags) then create an init() function that loop calls first so any given loop has good starting state.
Limitations of the Piezo Buzzer
Here are some things to consider when using the piezo buzzer:
You can’t use tone() while also using analogWrite() on pins 3 or 11

Resources