# Smart home automation in c programming

#### anukalp

Joined Jul 28, 2018
135
Hello all

It may be stupid idea but I am trying to learn programming by writing my own programs to achieve the requirement.

Program requirement - When the person is detected in the front door, ring the bell and open the door.

Code:
#include<stdio.h>

#define  High  1
#define  Low  0

#define  Open  0
#define  Close 1

#define ringing  0
#define not_ringing 1

int main (void)
{
int doorbell_sensor;

int bell;

int camera;

int House_Door;

if (doorbell_sensor == High )
{
House_Door = Open ;
bell = ringing;

}

else
{
House_Door = Close;
bell = not_ringing ;
}

return 0;
}
In my opinion above code fulfill the requirement but I would like to know your opinions for reliable code

Joined Apr 16, 2011
375
I see five obvious issues:

1) You have not initialised any of your variables. (maybe your compiler initialises them, but best to initialise them specifically)

2) Your code does absolutely nothing in the real world, as you seem to have no interface to the doorbell, the house door, or the bell.

3) The code has no comments. We have no clue as to what CPU/MCU you use, what the program is supposed to do, or why you are doing it

4) There is no means for doorbell_sensor variable to change from initial value, so performing an "if" test is pointless.

5) Your code executes once, then returns. A while(1) loop is probably required.

Last edited:

#### anukalp

Joined Jul 28, 2018
135
I see five obvious issues:
1) You have not initialised any of your variables. (maybe your compiler initialises them, but best to initialise them specifically)
That's the good point but I don't think need to initialises them,
2) Your code does absolutely nothing in the real world, as you seem to have no interface to the doorbell, the house door, or the bell.
3) The code has no comments. We have no clue as to what CPU/MCU you use, what the program is supposed to do, or why you are doing it
I know this code does nothing in the real world but it would me help to understand programming for embedded system

4) There is no means for doorbell_sensor variable to change from initial value, so performing an "if" test is pointless.

5) Your code executes once, then returns. A while(1) loop is probably required.
I wrote this program on PC. When complies program It doesn't give any error

Joined Apr 16, 2011
375
You avoided the question of why there is no commenting

What value is each variable initialised to?

Your code fails to meet any of the stated requirements IMHO

Last edited:

#### Picbuster

Joined Dec 2, 2013
982
Hello all

It may be stupid idea but I am trying to learn programming by writing my own programs to achieve the requirement.

Program requirement - When the person is detected in the front door, ring the bell and open the door.

Code:
#include<stdio.h>

#define  High  1
#define  Low  0

#define  Open  0
#define  Close 1

#define ringing  0
#define not_ringing 1

int main (void)
{
int doorbell_sensor;

int bell;

int camera;

int House_Door;

if (doorbell_sensor == High )
{
House_Door = Open ;
bell = ringing;

}

else
{
House_Door = Close;
bell = not_ringing ;
}

return 0;
}
In my opinion above code fulfill the requirement but I would like to know your opinions for reliable code
The remarks are valid however;
This type of control requires a state machine
state one ; bel ringing
state two ; is that valid yes next state else ignore and leaf state machine.
next state pen door leaf state machine
next
next close door

Picbuster

do in a C program loop the following;

switch (state)
{
case 0:
if (bel ring) { state ++; // go to next state} // keeps testing the bell during state =0. when active it move next state
break;
case 1:
if (!valid) {state=10;} // when invalid pop to 10 close door
state++;
break;
case 2:
open_door=yes;
break;

// place, when needed, some other condition here.

case 10;
close_door=Yes;
state=0;
break;
default: // door closed at all unknown states
close_door=Yes;
state=0;
break;
{

#### AlbertHall

Joined Jun 4, 2014
8,358
When the person is detected in the front door, ring the bell and open the door.
Makes it easy for burglars

#### MrSoftware

Joined Oct 29, 2013
1,562
Just to zero in on one of the issues described above; You always need to initialize variables in C. When you declare a variable on the stack, C will assign some space to your variable. Until your variable is explicitly assigned a value, its value will be whatever just happened to be in that memory at the time the space was reserved for your variable. The same goes for space allocated on the heap.

So when you create an int called doorbell_sensor, until you explicitly assign it a value, it can be any value between the minimum and maximum value for int on your compiler. So when you then immediately call if(doorbell_sensor == High), the result will be random based on whatever was in memory when doorbell_sensor was allocated.

#### nsaspook

Joined Aug 27, 2009
6,278
Initializing uninitialized statics to semantic zero is determined by the C run-time (linker) at startup. The default with most small embedded C compilers is zeroed but there are usually optional C run-times without zeroing that can be set with options.
https://en.wikipedia.org/wiki/Crt0

XC8 example: -Wl,-[no-]data-init Clearing and initialization of C objects at run-time startup

#### MrSoftware

Joined Oct 29, 2013
1,562
If I'm not mistaken variable initialization is not specified by the C language itself, as it is with some other languages (for example I think C# specifies ints shall be initialized to 0). For user application level coding (desktop and server applications), in my experience it is not common for memory to be automatically initialized by the runtime, or compiler.

One of my pet peeves is that some compilers will automatically zero memory for debug builds, but not for optimized builds. It's not consistent and can lead to difficult debugging when the release build has a problem that the debug (non-optimized) build does not replicate.

#### nsaspook

Joined Aug 27, 2009
6,278
If I'm not mistaken variable initialization is not specified by the C language itself, as it is with some other languages (for example I think C# specifies ints shall be initialized to 0). For user application level coding (desktop and server applications), in my experience it is not common for memory to be automatically initialized by the runtime, or compiler.

One of my pet peeves is that some compilers will automatically zero memory for debug builds, but not for optimized builds. It's not consistent and can lead to difficult debugging when the release build has a problem that the debug (non-optimized) build does not replicate.
From section 3.5.7 of the C89 standard

If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 and every member that has pointer type were assigned a null pointer constant.
The ISO C standard mandates that all static/global variables that aren't otherwise initialized are initialized to 0.
Initializing uninitialized statics is part of a standard C linkable (when all static/global variables are allocated space) library that can be modified or selected for the behavior desired in most cases.

Last edited:

#### MrSoftware

Joined Oct 29, 2013
1,562
A wise man once said, the nice thing about standards is there are so many to choose from.

Edit --> There is a key phrase there that I initially missed; static or global. Memory on the stack is neither static nor global.

A quick real world example from a free online compiler:

https://www.onlinegdb.com/online_c_compiler

Code:
#include <stdio.h>

int main()
{
int a[1000];
for(int i = 0; i < 1000; i++)
{
printf("%i, ", a[i]);
}

return 0;
}
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 121254556, 32590, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1826816, 0, 1825552, 0, 182555
2, 0, 0, 0, 5, 0, 3923968, 0, 3948544, 0, 3946656, 0, 3965632, 0, 1826816, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 121322484, 32590, 0, 0, 32, 0, 47, 0, 121273913, 32590, 0, 0, 123473344, 32590, 32, 0, 419
5097, 0, 4, 1, 10, 0, 0, 0, 1, 0, 1649759136, 32765, 121273033, 32590, 0, 0, 2147479968, 3, 1649759136, 32
765, 121254241, 32590, 0, 0, 121295524, 32590, 0, 0, 117347764, 32590, 0, 0, 121261258, 32590, 0, 0, 12122
1824, 32590, 1649758800, 32765, 1, 0, 1649759272, 32765, 4195097, 0, 123470240, 32590, 196608, 3, 3965632,
0, 121202848, 32590, 6, 0, 1649758360, 32765, 1048700, 0, 776414, 0, 1, 0, 33261, 0, 0, 0, 0, 0, 1857312,
0, 4096, 0, 3632, 0, 1553690964, 0, 0, 0, 1553690964, 0, 0, 0, 1557205492, 0, 987759529, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 4195097, 0, 123470280, 32590, 0, 0, 123465824, 32590, 0, 0, 0, 0, 121261513, 32590, 0, 0, 16497
59264, 32765, 0, 0, 1, 0, 10, 0, 1649759233, 32765, 123452167, 32590, 0, 32590, 32, 0, 3, 0, 0, 0, 0, 0, 0
, 0, 123473344, 32590, 1649761376, 32765, 832, 0, 1179403647, 65794, 0, 0, 4063235, 1, 139344, 0, 64, 0, 1
852960, 0, 0, 3670080, 4194314, 4390980, 6, 5, 64, 0, 64, 0, 64, 0, 560, 0, 560, 0, 8, 0, 3, 4, 1619984, 0
, 1619984, 0, 1619984, 0, 28, 0, 28, 0, 16, 0, 1, 5, 0, 0, 0, 0, 0, 0, 1825552, 0, 1825552, 0, 2097152, 0,
1, 6, 1828672, 0, 3925824, 0, 3925824, 0, 20832, 0, 39808, 0, 2097152, 0, 2, 6, 1842080, 0, 3939232, 0, 3
939232, 0, 480, 0, 480, 0, 8, 0, 4, 4, 624, 0, 624, 0, 624, 0, 68, 0, 68, 0, 4, 0, 7, 4, 1828672, 0, 39258
24, 0, 3925824, 0, 16, 0, 176, 0, 16, 0, 1685382480, 4, 1620012, 0, 1620012, 0, 1620012, 0, 26436, 0, 2643
6, 0, 4, 0, 1685382481, 6, 0, 0, 0, 0, 1649760744, 32765, 1649760832, 32765, 0, 0, 1649760208, 32765, 1212
65980, 32590, 117347764, 32590, -1340356096, 0, 0, 0, 1, 0, 123471752, 32590, 121268406, 32590, 0, 0, 1649
759968, 32765, 1649906088, 32765, 1649760240, 32765, 1649906160, 32765, 46165800, 0, 1649760224, 32765, 16
49760736, 32765, 0, 0, 121265980, 32590, 123471712, 32590, 2090266759, 0, 1, 0, 3, 0, 123393288, 32590, 12
1268406, 32590, 117347764, 32590, 1649760096, 32765, 117271856, 32590, 1649760368, 32765, 117324968, 32590
, 32660418, 0, 1649760352, 32765, 121268785, 32590, 0, 0, 123394128, 32590, 123392000, 32590, 121227132, 3
2590, 117325256, 32590, 121226272, 32590, 0, 1, 2213, 1, 1, 0, 1649760704, 32765, 1649760512, 32765, 12339
4128, 32590, 1, 0, 123471136, 32590, 123468280, 32590, 121268785, 32590, 0, 0, 123394128, 32590, 1, 32765,
0, 0, 1, 0, 123468280, 32590, 1649760728, 32765, 1649760720, 32765, 1649760260, 32765, 0, 0, 0, 0, 123471
136, 32590, 1649760368, 32765, 1649760352, 32765, 2090266759, 0, 121227132, 32590, -1, 0, 117384240, 32590
, 117324968, 32590, 123392000, 32590, 1649760832, 32765, 118542861, 32590, 0, 32590, 0, 0, 0, 0, 164990616
0, 32765, 0, 32, 0, 0, 0, 0, 0, 0, 1, 0, 123468280, 32590, 121227888, 32590, 7, 0, 121226272, 32590, 12122
5216, 32590, 1649760832, 32765, 121277215, 32590, 1, 32765, 0, 0, 0, 0, 121265980, 32590, 1649760688, 3276
5, 1700966438, 0, 0, 0, 1, 0, 123471752, 32590, 121268406, 32590, 0, 0, 1649760672, 32765, 1649906088, 327
65, 1649760944, 32765, 1649906304, 32765, 26577600, 0, 1649760928, 32765, 121265980, 32590, 0, 0, -1637544
50, 0, 1, 0, 3, 0, 123393288, 32590, 121268406, 32590, 0, 0, 1649760784, 32765, 117271856, 32590, 16497610
56, 32765, 117322232, 32590, 64550200, 0, 1649761040, 32765, 123472616, 32590, 0, 0, 123393360, 32590, 123
392000, 32590, 4195114, 0, 117325256, 32590, 4195048, 0, 0, 1, 2099, 1, 0, 0, 1649761240, 32765, 164976120
0, 32765, 123393360, 32590, 1, 0, 123471136, 32590, 123470280, 32590, 121268785, 32590, 0, 0, 123393360, 3
2590, 1, 0, 0, 0, 1, 32765, 123470280, 32590, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 123471136, 32590, 1649761056,
32765, 1649761040, 32765, -163754450, 0, 4195114, 0, -1, 0, 1649761400, 32765, 117322232, 32590, 123392000
, 32590, 123470280, 32590, 0, 0, 1, 0, 4195805, 0, 0, 0, 0, 0, 4195728, 0, 4195392, 0, 1649761376, 32765,
0, 0,
...Program finished with exit code 0
Press ENTER to exit console.
But now if I make the array global:
Code:
#include <stdio.h>

int a[1000];
int main()
{
for(int i = 0; i < 1000; i++)
{
printf("%i, ", a[i]);
}

return 0;
}
The result is different:
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
...Program finished with exit code 0
Press ENTER to exit console.

Last edited:

#### BobaMosfet

Joined Jul 1, 2009
795
Hello all

It may be stupid idea but I am trying to learn programming by writing my own programs to achieve the requirement.

Program requirement - When the person is detected in the front door, ring the bell and open the door.

Code:
#include<stdio.h>

#define  High  1
#define  Low  0

#define  Open  0
#define  Close 1

#define ringing  0
#define not_ringing 1

int main (void)
{
int doorbell_sensor;

int bell;

int camera;

int House_Door;

if (doorbell_sensor == High )
{
House_Door = Open ;
bell = ringing;

}

else
{
House_Door = Close;
bell = not_ringing ;
}

return 0;
}
In my opinion above code fulfill the requirement but I would like to know your opinions for reliable code
learning to program by writing your own programs is how we all did it. Starting simple, getting complex. Not stupid _at all_.

What you've done above is not a program. It is an algorithm in the syntax of a c program. Instead, it is a flow-chart in written form. You could instead draw a simple flow-chart to display the same logic as above, and then write code to match the flow-chart. This serves two purposes- documents your algorithm, and gives you a logic reference (which is not what code is). Code is a syntactic representation of logic designed to satisfy both a compiler and an end processor.

#### MrChips

Joined Oct 2, 2009
19,273
Exactly.

Flow-chart is design.
Code is implementation.

Get the design right in the first place before writing code.