arduino decoder – einfacher Methode über analoge Eingänge

2. Mai 2016 at 21:36
Print Friendly, PDF & Email

Die Programmwahl erfolgt mit nur einem Bauteil. Der Kodierschalter ist hier direkt auf den Nano aufgelötet.

Somit brauche ich nur ein passives Bauteil, keine weitere Beschaltung wie Widerstände oder Schieberegister.

DMX_V2_3

 

[codesyntax lang=”cpp”]

// coder: 4 switches linked to ground; values 16 (0..15)
#define DEC_CH1 A3 // Decoder Channel 1 = 1 * 2^0 = 1 * 1
#define DEC_CH2 A2 // Decoder Channel 2 = 1 * 2^1 = 1 * 2
#define DEC_CH3 A1 // Decoder Channel 3 = 1 * 2^2 = 1 * 4
#define DEC_CH4 A0 // Decoder Channel 4 = 1 * 2^3 = 1 * 8

void setup(void)
{
 digitalWrite(DEC_CH1 , HIGH); // set pullup on analog pin 0
 digitalWrite(DEC_CH2 , HIGH); // set pullup on analog pin 1
 digitalWrite(DEC_CH3 , HIGH); // set pullup on analog pin 2
 digitalWrite(DEC_CH4 , HIGH); // set pullup on analog pin 3

 // read analog input to determine RF Channel
 code_prog = read_coder4(DEC_CH1,DEC_CH2,DEC_CH3,DEC_CH4);
 
} // end of setup()

uint8_t read_coder4(uint8_t DEC_PIN_0,uint8_t DEC_PIN_1,uint8_t DEC_PIN_2,uint8_t DEC_PIN_3){
	int cod_ret,cod1, cod2, cod3, cod4;

	cod1 = analogRead(DEC_PIN_0);
	if (cod1 > 512){cod1 = 0;} else {cod1 = 1;}
	delay(2);

	cod2 = analogRead(DEC_PIN_1);
	if (cod2 > 512){cod2 = 0;} else {cod2 = 1;}
	delay(2);

	cod3 = analogRead(DEC_PIN_2);
	if (cod3 > 512){cod3 = 0;} else {cod3 = 1;}
	delay(2);

	cod4 = analogRead(DEC_PIN_3);
	if (cod4 > 512){cod4 = 0;} else {cod4 = 1;}

	cod_ret = cod1 + cod2 * 2 + cod3 * 4 + cod4 * 8;

#ifdef DEBUG
	printf("> code %d = %1d-%1d-%1d-%1d \n", cod_ret, cod1, cod2, cod3, cod4);
#endif
	return(cod_ret);
} // end of function read_coder4()

[/codesyntax]