Software declaring of hardware connected to arduino pins

I m trying to understand how to declare different hardware connected to Arduino pins
I tried different ways, that i learned from some example codes, as following and compiler pass it without any error it means all types are valid...but in fact i want understand best way to assign pins to hardware

#define IR_SMALLD_RC5
#include <IRsmallDecoder.h>
#include <OneWire.h>
#include <DallasTemperature.h>

int pot_pin(A0);//********
int Button_pin(A1);//*****
int irDecode(2);//*******
const int SENSOR_PIN = 4; // ****** Arduino pin-4 to Temp_sensor "DS18B20" 's DQ pin
int Triac_Gate_opto = 5;//********* Arduino pin-5 to opto for Triac_Gate trigger
int Zero_crossing_opto = 8;// ***** Arduino pin-8 to opto for AC Zero Crossing sense

IRsmallDecoder irDecoder(2); // Arduino pin-2 to IR Decoder s "data pin"

irSmallD_t irData; // IR s' decoded Data store register;

//VARIABLES
// declaring global variables (RAM Bytes) to be used in functions and in the loop
int triacDelay = 0;//a RAM register "triacDelay" , initilize to value 00
int on_Triac = 0;
int last_CH1_state = 0;
int previousPotValue = -1;
int currentPotValue = 0;
int currentButtonValue = 0;
int previousButtonValue = 4;
int counter = 0;
//const int SENSOR_PIN = 4; // Arduino pin-4 to DS18B20 sensor's DQ pin ##########
OneWire oneWire(SENSOR_PIN); // Pass SENSOR_PIN data to OneWire library.....and also...
DallasTemperature tempSensor(&oneWire); // pass oneWire to DallasTemperature library
float tempCelsius; //4-byte(32bit) reg to hold "temperature value" in Celsius

You asked this question already. You might want to ask the mods to merge your topics

Read this

but i have deleted that post on mod s warning?
now what....how to merge deleted post
how to connect moderate??

I didn’t know you deleted the last post. What was the reason for that? You had many pointers in that post to the answers to these questions

some one ask me that your post is irrelevant, and delete it...i did that
then a study how to get best from this forum and then make this post according to rules....it is very hard to get help

No one give a real answer, except advice to learn c first

I posted a link to the arduino reference for declaring a variable int which is how you declare a pin for hardware. I also linked to a basic tutorial on c++ which would give you all the information you need. What other help do you want?

i m following Arduino reference for C-Language and "int"
i m also now looking at that youtube tutorial for c
thanks for that help a lot
but plz check my code and my deceleration in that code and guide if i did it wright way or wrong way..and is there some batter way? if you have time
thanks once again

It is not all about right and wrong I guess. My preference for variables that are used for pins would be:

uint8_t const ledPin = 2;

or if you prefer:

byte const ledPin = 2;

The const qualifier is used to indicate that the value will never change (which is usually the case for pins). I would not use larger types (like int) because functions like pinMode and digitalWrite expect a uint8_t (or byte).

1 Like

excellent, understandable to me and help full too. thanks

well
ledPin(2) and ledPin = 2 same?
an LED is connected to pin 2 why we assign a byte to it while it is just a one bit?

Technically it is a different type of initialisation if I remember correctly, but the result is the same. I rarely see the first form (with the ()) for basic types though and it looks a bit too much like a function prototype to me.

An output pin is controlled by writing one bit (indeed) to a certain register, likewise an input pin is read by looking at one bit. The pin however needs to be configured, which requires additional bits to be set. Manipulating pins this way is too tedious for most users, that is why pin numbers and functions like pinMode and digitalWrite are used to make it easier on the programmer.

[edit]

You might want to have a look at section 5 of the following tutorial if you are interested.

it is not a "just a bit", it bit on the specific controller port, so is quite adequate using byte for it

sure i will follow this tutorial...thanks

Arduino/C++ has several ways to initialise a variable. You may see any of them in programs written by others. After a while, you will develop your own style.

#define LED_PIN 2
uint_8t const LED_PIN = 2;
uint_8t const LED_PIN  (2);
uint_8t const LED_PIN {2};

There are some gotchas:
The first one is the pre-processor literally substituting the literal 2 wherever it finds the word LED_PIN. There is no type checking done at the pre-processor level. Note, no equal sign and no semi-colon.
The 2nd and 3rd have already been discussed above.
The last is uniform initilalisation, with a mild benefit that it will keep you from putting a larger data type into a smaller variable.

1 Like

sir
LED is connected to a PIN that is "just a bit" of a port register.
why we assign an int (16-bit = 2 bytes) to a bit.
To operate LED we have to dealt with that Port (a byte) or Port.bit

solid answer to my issue....thamks

1 Like

Because the number '2' won't fit in one bit. If we had a 2-bit unsigned variable type you could use that, until you needed a pin number greater than 3. The Arduino pin numbers are well under 255 so I use 'const byte'.

Thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.