Confused about code - how it works?

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone! Firstly, I want to thank you all for making our lives easier. After two or three weeks of struggle, i've found this forum, and saw projects so complicated that I am little bit embarrassed to ask my question.

My school project is simple "fire alarm", which includes flame (uv) sensor, Atmel Atmega 8 and buzzer. It should make noise after flame is detected, and then "reset" option, but from program code, not hardware. First part of program code is working, but I dont know why it works, because on flame sensor, digital pin (D0) should be HIGH when flame is detected, and LOW when there in no flame (or UV light source). On my flame sensor is opposite, so I adjusted program code which works, strange. With second step i have problems, i don't have idea how to achieve "software reset". I tried many different options from internet, even using "watchdog timer", but I am not so experienced yet. If you could help me it would mean a lot to me. In project I used passive buzzers. Program code is below.

Digital pin (flame sensor) is connected to PD1, buzzer is connected to PD0.

#include <avr/io.h>
#include "input_output/AVR_lib.h"
#include <util/delay.h>
#define BUZZER_DELAY 500

void initialization (){

input_port(DDRD,PD1);
set_port(PORTD,PD1,1);

output_port(PORTD,PD0);

}

int main(void){
	
	initialization();
	while(1)
	{
		if(get_pin(PIND,PD1) == 0)
		{
			set_port(PORTD,PD0,1);
			_delay_us(BUZZER_DELAY);
			set_port(PORTD,PD0,0);
			_delay_us(BUZZER_DELAY);
		}
		else
		{
			set_port(PORTD,PD0,0);
			
	    }
		
	}
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The first step is to find technical data for your flame sensor. This should outline how it works and what output you should expect. There should be no magic, so don’t guess.

As for your coding problem, forget about the ‘code’ for the moment. Think about the steps required to achieve your goal. A pencil and paper helps. Some people might use a flow chart. I would probably use simple language to form ‘rules’.
Eg:
If sensor detects uv THEN state = alarm

If state is equal to alarm THEN turn on buzzer else turn off buzzer

If state is equal to alarm AND button pressed THEN state = off

And so on.
You might want to incorporate time into your logic - the buzzer might cycle on and off, or buzz for only 30 seconds. The detector might have to detect fire for 3 seconds before causing an alarm. The button might have to be pressed for 10 seconds to silence the alarm. And so on.

Once you’ve determined all of your rules, then translating that into code is easy. Don’t be afraid to double back and change your rules if you identify a problem.
Start simple and test each step.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

By the way, what Kartman is describing is a simple state machine (or at least part of one)...you should read up a little about those--they are very useful for keeping your code edits untangled & free from slowly wrapping around your neck & chocking your efforts.

Your app only needs the barest-bones "state machine"   Timers & buttons & other conditions can help propel your code from one state to another, like skipping through the neighborhood to get to point A, or later on, B. 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Fri. Nov 8, 2019 - 04:55 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

newbie96 wrote:

#include "input_output/AVR_lib.h"

Just out of interest, what is this?