4 bits bcd input en Neopixel output

Arduino shields
Berichten: 10
Geregistreerd: 29 Dec 2014, 15:35
Woonplaats: Oosterwolde (Gld)

4 bits bcd input en Neopixel output

Berichtdoor Gert848 » 13 Okt 2015, 22:13

Hallo forumleden,

Ik ben redelijk nieuw op het forum en ben bezig met een Pick to Light systeem met weinig bekabeling dankzij de geweldige Neopixel WS2812 Leds. Deze zijn individueel adresseerbaar.
Nu wil ik 4 bits BCD code input omzetten naar de waarde maar dan decimaal de desbetreffende led aansturen. Ik heb het programma al deels werken maar ben nu op zoek hoe ik de serieel signaal aan de desbetreffende led kan krijgen. Wie weet de mogelijke oplossing?

[code]

// TEST For "Profile switch" currently set to read BCD switch
// once during setup; move code as noted below for repeated reads

// BCD Switch inputs attached to pins D2-5, common to Ground
// (use internal pullups)

#include <Adafruit_NeoPixel.h>
//#include <avr/power.h>


// The Neopixels will be attached to pin 6
#define PIN 6

// How many Neopixels are attached to the Arduino?
#define NUMPIXELS 16

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS,PIN, NEO_GRB + NEO_KHZ800);


// Define data bit pins
int profile_BIT1 = 2;
int profile_BIT2 = 3;
int profile_BIT4 = 4;
int profile_BIT8 = 5;

// Define profile variable, set to 0

int profile = 0;

void setup() {
// initialize serial communication at 9600 bits per second (only needed
// for debugging:

Serial.begin(9600);

pixels.begin(); // This initializes the NeoPixel library.


// make the profile switch's pins inputs, with internal pullups on:
pinMode(profile_BIT1, INPUT_PULLUP);
pinMode(profile_BIT2, INPUT_PULLUP);
pinMode(profile_BIT4, INPUT_PULLUP);
pinMode(profile_BIT8, INPUT_PULLUP);

// to make read on each pass move all below this to "loop",
// uncomment profile reset


// read the input pins: NOTE - CLOSED switch (binary 1) returns "0"
int val_profile_BIT1 = digitalRead(profile_BIT1);
int val_profile_BIT2 = digitalRead(profile_BIT2);
int val_profile_BIT4 = digitalRead(profile_BIT4);
int val_profile_BIT8 = digitalRead(profile_BIT8);



int profile = 0;
// needed in loop to reset to 0 for each pass

//turn bit values into single profile value by adding decimal value
//of each bit to profile variable

if ( val_profile_BIT1 == 0) { profile = profile + 1; }
if ( val_profile_BIT2 == 0) { profile = profile + 2; }
if ( val_profile_BIT4 == 0) { profile = profile + 4; }
if ( val_profile_BIT8 == 0) { profile = profile + 8; }

// DEBUG - prints out the state of the switch bits and profile value

{Serial.print("[profile_BIT1: ");
Serial.print(val_profile_BIT1);
Serial.print("] ");
Serial.print("[profile_BIT2: ");
Serial.print(val_profile_BIT2);
Serial.print("] ");
Serial.print("[profile_BIT4: ");
Serial.print(val_profile_BIT4);
Serial.print("] ");
Serial.print("[profile_BIT8: ");
Serial.print(val_profile_BIT8);
Serial.print("] ");
Serial.print("[profile: ");
Serial.print(profile);
Serial.println("] ");}
}



void loop() {

// delay in between reads for stability, uncomment if in loop
// delay(1000);}
pixels.setPixelColor(8, 0,0,100);
pixels.show();
}

Advertisement

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: 4 bits bcd input en Neopixel output

Berichtdoor nicoverduin » 16 Okt 2015, 07:42

Eerst ff formatteren:
cpp code
// TEST For "Profile switch" currently set to read BCD switch
// once during setup; move code as noted below for repeated reads

// BCD Switch inputs attached to pins D2-5, common to Ground
// (use internal pullups)

#include <Adafruit_NeoPixel.h>
//#include <avr/power.h>

// The Neopixels will be attached to pin 6
#define PIN 6

// How many Neopixels are attached to the Arduino?
#define NUMPIXELS 16

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN,
NEO_GRB + NEO_KHZ800);

// Define data bit pins
int profile_BIT1 = 2;
int profile_BIT2 = 3;
int profile_BIT4 = 4;
int profile_BIT8 = 5;

// Define profile variable, set to 0

int profile = 0;

void setup() {
// initialize serial communication at 9600 bits per second (only needed
// for debugging:

Serial.begin(9600);

pixels.begin(); // This initializes the NeoPixel library.

// make the profile switch's pins inputs, with internal pullups on:
pinMode(profile_BIT1, INPUT_PULLUP);
pinMode(profile_BIT2, INPUT_PULLUP);
pinMode(profile_BIT4, INPUT_PULLUP);
pinMode(profile_BIT8, INPUT_PULLUP);

// to make read on each pass move all below this to "loop",
// uncomment profile reset

// read the input pins: NOTE - CLOSED switch (binary 1) returns "0"
int val_profile_BIT1 = digitalRead(profile_BIT1);
int val_profile_BIT2 = digitalRead(profile_BIT2);
int val_profile_BIT4 = digitalRead(profile_BIT4);
int val_profile_BIT8 = digitalRead(profile_BIT8);

int profile = 0;
// needed in loop to reset to 0 for each pass

//turn bit values into single profile value by adding decimal value
//of each bit to profile variable

if (val_profile_BIT1 == 0) {
profile = profile + 1;
}
if (val_profile_BIT2 == 0) {
profile = profile + 2;
}
if (val_profile_BIT4 == 0) {
profile = profile + 4;
}
if (val_profile_BIT8 == 0) {
profile = profile + 8;
}

// DEBUG - prints out the state of the switch bits and profile value

{
Serial.print("[profile_BIT1: ");
Serial.print(val_profile_BIT1);
Serial.print("] ");
Serial.print("[profile_BIT2: ");
Serial.print(val_profile_BIT2);
Serial.print("] ");
Serial.print("[profile_BIT4: ");
Serial.print(val_profile_BIT4);
Serial.print("] ");
Serial.print("[profile_BIT8: ");
Serial.print(val_profile_BIT8);
Serial.print("] ");
Serial.print("[profile: ");
Serial.print(profile);
Serial.println("] ");
}
}

void loop() {

// delay in between reads for stability, uncomment if in loop
// delay(1000);}
pixels.setPixelColor(8, 0, 0, 100);
pixels.show();
}


Op het printen van de gelezen bitjes dezelfde code nu vereenvoudigd

cpp code
// TEST For "Profile switch" currently set to read BCD switch
// once during setup; move code as noted below for repeated reads

// BCD Switch inputs attached to pins D2-5, common to Ground
// (use internal pullups)

#include <Adafruit_NeoPixel.h>

//#include <avr/power.h>

// The Neopixels will be attached to pin 6
#define PIN 6

// How many Neopixels are attached to the Arduino?
#define NUMPIXELS 16

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN,
NEO_GRB + NEO_KHZ800);

//
// DATA BIT PINS
//
#define profile_BIT1 2
#define profile_BIT2 3
#define profile_BIT4 4
#define profile_BIT8 5
//
// omzetten BCD pinnern in een tabel
//
int profileBitArray[4] = { profile_BIT1, profile_BIT2, profile_BIT4, profile_BIT8 };

#define DEBUG // als we niet willen debuggen dan deze regel commentarieren

// Define profile variable, set to 0

int profile = 0;

void setup() {

#ifdef DEBUG
// initialize serial communication at 9600 bits per second (only needed
// for debugging:

Serial.begin(9600);

#endif

pixels.begin(); // This initializes the NeoPixel library.

for (uint8_t i = 0; i < sizeof(profileBitArray); i++) {
pinMode(profileBitArray[i], INPUT_PULLUP);
}
// to make read on each pass move all below this to "loop",
// uncomment profile reset

uint8_t profile = 0; // Binair resultaat van BCD
//
// omdat BCD en binair voor 0-9 identiek zijn
// kunnen we de bits er gewoon in schuiven
//
for (int8_t i = sizeof(profileBitArray) - 1; i >=0; i--) {
//
// vorige waarde een bit opschuiven naar links
//
profile << 1;
//
// inverteer het bitje dat gelezen wordt (pull down = 1) en tel deze op bij resultaat
//
uint8_t bitje = !digitalRead(profileBitArray[i]);

#ifdef DEBUG
Serial.print("[profile_BIT");
Serial.print((((i+1)*2) / 2), 1);
Serial.print(": ");
Serial.print(bitje);
Serial.print("] ");
#endif
}
#ifdef DEBUG
Serial.println(" ");
#endif

}

void loop() {

// delay in between reads for stability, uncomment if in loop
// delay(1000);}
pixels.setPixelColor(8, 0, 0, 100);
pixels.show();
}
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: 4 bits bcd input en Neopixel output

Berichtdoor nicoverduin » 16 Okt 2015, 08:24

Dan jouw vraag kan op verschillende manieren geïnterpreteerd worden. Is het de bedoeling dat:
a) de binaire waarde (want je kan met de schakelaars 0-15 aangeven en dat is HEX en geen BCD) de led in de string aanzet?. Dat correspondeert ook met het aantal LEDS
b) of moet de kleur aangepast worden

setPixelColor kent 4 parameters:
a) pixel nummer
b) rood
c) groen
d) blauw

In genoemde volgorde. Dus als je die 8 vervangt voor profile, dan zul je vermoedelijk (heb ze ff niet bij de hand) alle maal oplichten na dat je alle binaire waarden hebt bijgewerkt. Je zet namelijk maar een kleur en niet de anderen.
Wat ik ooit deed in een programma (met > 300 RGB leds) was eerst de array in een specifieke achtergrond kleur zetten (van mij part zwart) en daarna de juiste pixel(s) aanzetten in de juiste kleur.
Dus als je in de loop eerst een loopje maakt die alle kleuren in een specifieke achterrond kleur zet, daarna het juiste pixel aanzet en dan show() uitvoert dan gaat het goed
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 10
Geregistreerd: 29 Dec 2014, 15:35
Woonplaats: Oosterwolde (Gld)

Re: 4 bits bcd input en Neopixel output

Berichtdoor Gert848 » 16 Okt 2015, 09:25

Hallo Nico

Ik heb een 4 bits duimwielschakelaar aan de input
Aan de uitgang wil ik graag het getal wat uit de serieelmonitor komt dus bv getal 7 dan wil ik dat ik de 6e led groen laat branden.
Aan het eind van mijn programma zie je 8.255.0.0 staan en nu zou hier getal 8 de variabele van de uitkomst in de serieel monitor moeten zijn.
Ook moeten de ingangswaarde ververst worden. Als er iets aan de status veranderd dan moet dit weer naar de uitgang gestuurd worden.
Zijn de vragen dan duidelijk?
Alvast bedankt voor je inspanning ik ga het zeker proberen vandaag of morgen.

M vr groet Gerhard

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: 4 bits bcd input en Neopixel output

Berichtdoor nicoverduin » 16 Okt 2015, 13:55

Gerhard
a)Je kan dat hele stuk in de setup() waar je de duim wiel leest naar een aparte functie zetten.
b) profile als variabele naar boven verplaatsen zodat hij in het gehele programma bekend is (is nu nog fout)
c) In de loop doe je dan een lees functie naar punt a
d) Daarna vul je het display.

Dus zoiets

cpp code
// TEST For "Profile switch" currently set to read BCD switch
// once during setup; move code as noted below for repeated reads

// BCD Switch inputs attached to pins D2-5, common to Ground
// (use internal pullups)

#include <Adafruit_NeoPixel.h>

//#include <avr/power.h>

// The Neopixels will be attached to pin 6
#define PIN 6

// How many Neopixels are attached to the Arduino?
#define NUMPIXELS 16

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN,
NEO_GRB + NEO_KHZ800);

//
// DATA BIT PINS
//
#define profile_BIT1 2
#define profile_BIT2 3
#define profile_BIT4 4
#define profile_BIT8 5
//
// omzetten BCD pinnern in een tabel
//
int profileBitArray[4] = { profile_BIT1, profile_BIT2, profile_BIT4, profile_BIT8 };

#define DEBUG // als we niet willen debuggen dan deze regel commentarieren

// Define profile variable, set to 0

int profile = 0;

void setup() {

#ifdef DEBUG
// initialize serial communication at 9600 bits per second (only needed
// for debugging:

Serial.begin(9600);

#endif

pixels.begin(); // This initializes the NeoPixel library.

for (uint8_t i = 0; i < sizeof(profileBitArray); i++) {
pinMode(profileBitArray[i], INPUT_PULLUP);
}


}

void loop() {
//
// lezen duimwiel
//
// to make read on each pass move all below this to "loop",
// uncomment profile reset

profile = 0; // Binaire resultaat van BCD
//
// omdat BCD en binair voor 0-9 identiek zijn
// kunnen we de bits er gewoon in schuiven
//
for (int8_t i = sizeof(profileBitArray) - 1; i >=0; i--) {
//
// vorige waarde een bit opschuiven naar links
//
profile << 1;
//
// inverteer het bitje dat gelezen wordt (pull down = 1) en tel deze op bij resultaat
//
uint8_t bitje = !digitalRead(profileBitArray[i]);

#ifdef DEBUG
Serial.print("[profile_BIT");
Serial.print((((i+1)*2) / 2), 1);
Serial.print(": ");
Serial.print(bitje);
Serial.print("] ");
#endif
}
#ifdef DEBUG
Serial.println(" ");
#endif
//
// wissen achtergrond
//
for (uint8_t i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, 0,0,0);
}
//
// display juiste pixel
//
pixels.setPixelColor(profile, 0, 0, 100);
pixels.show();
}
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 10
Geregistreerd: 29 Dec 2014, 15:35
Woonplaats: Oosterwolde (Gld)

Re: 4 bits bcd input en Neopixel output

Berichtdoor Gert848 » 16 Okt 2015, 15:14

Hallo Nico,

Ik heb het programma getest maar alleen de eerste led blijft branden en als ik de bcd teller verhoog gebeurd er niks.( De pinnen worden aan de - gelegd. De bcd heeft gemeenschappelijke min.
De serieel monitor scanned nu wel de status maar deze heb ik straks niet meer nodig.
Het belangrijkste is dat de BCD teller als input de juiste Neopixel aan de uitgang laat branden.
Ik vind het allemaal nog wel ingewikkeld hoe het programma in elkaar steekt. Je legd het wel goed uit door die regels tekst erin te zetten. Zou je nog eens kunnen kijken wat ik mogelijk nog fout doe?

Alvast weer bedankt

Berichten: 10
Geregistreerd: 29 Dec 2014, 15:35
Woonplaats: Oosterwolde (Gld)

Re: 4 bits bcd input en Neopixel output

Berichtdoor Gert848 » 29 Okt 2015, 23:29

Hallo Nico,

Ik heb dit programma werken zoals ik ongeveer wil.

Alleen moet hij nu nog als de input verandert het ververst worden.

Dus als profile verandert dat dit ook de juiste neopixel aanpast

Kun jij mij zeggen wat ik nog moet wijzigen?

Waarschijnlijk de void loop veranderen?

Hier de code:



/ TEST For "Profile switch" currently set to read BCD switch
// once during setup; move code as noted below for repeated reads

// BCD Switch inputs attached to pins D2-5, common to Ground
// (use internal pullups)

#include <Adafruit_NeoPixel.h>
//#include <avr/power.h>


// The Neopixels will be attached to pin 6
#define PIN 6

// How many Neopixels are attached to the Arduino?
#define NUMPIXELS 16

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS,PIN, NEO_GRB + NEO_KHZ800);


// Define data bit pins
int profile_BIT1 = 2;
int profile_BIT2 = 3;
int profile_BIT4 = 4;
int profile_BIT8 = 5;

// Define profile variable, set to 0

int profile = 0;

void setup() {
// initialize serial communication at 9600 bits per second (only needed
// for debugging:

Serial.begin(9600);

pixels.begin(); // This initializes the NeoPixel library.


// make the profile switch's pins inputs, with internal pullups on:
pinMode(profile_BIT1, INPUT_PULLUP);
pinMode(profile_BIT2, INPUT_PULLUP);
pinMode(profile_BIT4, INPUT_PULLUP);
pinMode(profile_BIT8, INPUT_PULLUP);

// to make read on each pass move all below this to "loop",
// uncomment profile reset


// read the input pins: NOTE - CLOSED switch (binary 1) returns "0"
int val_profile_BIT1 = digitalRead(profile_BIT1);
int val_profile_BIT2 = digitalRead(profile_BIT2);
int val_profile_BIT4 = digitalRead(profile_BIT4);
int val_profile_BIT8 = digitalRead(profile_BIT8);



// int profile = 0;
// needed in loop to reset to 0 for each pass

//turn bit values into single profile value by adding decimal value
//of each bit to profile variable

if ( val_profile_BIT1 == 0) { profile = profile + 1; }
if ( val_profile_BIT2 == 0) { profile = profile + 2; }
if ( val_profile_BIT4 == 0) { profile = profile + 4; }
if ( val_profile_BIT8 == 0) { profile = profile + 8; }

// DEBUG - prints out the state of the switch bits and profile value

{Serial.print("[profile_BIT1: ");
Serial.print(val_profile_BIT1);
Serial.print("] ");
Serial.print("[profile_BIT2: ");
Serial.print(val_profile_BIT2);
Serial.print("] ");
Serial.print("[profile_BIT4: ");
Serial.print(val_profile_BIT4);
Serial.print("] ");
Serial.print("[profile_BIT8: ");
Serial.print(val_profile_BIT8);
Serial.print("] ");
Serial.print("[profile: ");
Serial.print(profile);
Serial.println("] ");}
}



void loop() {

// delay in between reads for stability, uncomment if in loop
delay(1000);
pixels.setPixelColor(profile, 0,0,100);
pixels.show();
}

Berichten: 10
Geregistreerd: 29 Dec 2014, 15:35
Woonplaats: Oosterwolde (Gld)

Re: 4 bits bcd input en Neopixel output

Berichtdoor Gert848 » 31 Okt 2015, 20:18

nicoverduin schreef:Gerhard
a)Je kan dat hele stuk in de setup() waar je de duim wiel leest naar een aparte functie zetten.
b) profile als variabele naar boven verplaatsen zodat hij in het gehele programma bekend is (is nu nog fout)
c) In de loop doe je dan een lees functie naar punt a
d) Daarna vul je het display.

Dus zoiets

cpp code
// TEST For "Profile switch" currently set to read BCD switch
// once during setup; move code as noted below for repeated reads

// BCD Switch inputs attached to pins D2-5, common to Ground
// (use internal pullups)

#include <Adafruit_NeoPixel.h>

//#include <avr/power.h>

// The Neopixels will be attached to pin 6
#define PIN 6

// How many Neopixels are attached to the Arduino?
#define NUMPIXELS 16

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN,
NEO_GRB + NEO_KHZ800);

//
// DATA BIT PINS
//
#define profile_BIT1 2
#define profile_BIT2 3
#define profile_BIT4 4
#define profile_BIT8 5
//
// omzetten BCD pinnern in een tabel
//
int profileBitArray[4] = { profile_BIT1, profile_BIT2, profile_BIT4, profile_BIT8 };

#define DEBUG // als we niet willen debuggen dan deze regel commentarieren

// Define profile variable, set to 0

int profile = 0;

void setup() {

#ifdef DEBUG
// initialize serial communication at 9600 bits per second (only needed
// for debugging:

Serial.begin(9600);

#endif

pixels.begin(); // This initializes the NeoPixel library.

for (uint8_t i = 0; i < sizeof(profileBitArray); i++) {
pinMode(profileBitArray[i], INPUT_PULLUP);
}


}

void loop() {
//
// lezen duimwiel
//
// to make read on each pass move all below this to "loop",
// uncomment profile reset

profile = 0; // Binaire resultaat van BCD
//
// omdat BCD en binair voor 0-9 identiek zijn
// kunnen we de bits er gewoon in schuiven
//
for (int8_t i = sizeof(profileBitArray) - 1; i >=0; i--) {
//
// vorige waarde een bit opschuiven naar links
//
profile << 1;
//
// inverteer het bitje dat gelezen wordt (pull down = 1) en tel deze op bij resultaat
//
uint8_t bitje = !digitalRead(profileBitArray[i]);

#ifdef DEBUG
Serial.print("[profile_BIT");
Serial.print((((i+1)*2) / 2), 1);
Serial.print(": ");
Serial.print(bitje);
Serial.print("] ");
#endif
}
#ifdef DEBUG
Serial.println(" ");
#endif
//
// wissen achtergrond
//
for (uint8_t i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, 0,0,0);
}
//
// display juiste pixel
//
pixels.setPixelColor(profile, 0, 0, 100);
pixels.show();
}


Hallo Nico,

Ik heb dit programma werken zoals ik ongeveer wil.

Alleen moet hij nu nog als de input verandert het ververst worden.

Dus als profile verandert dat dit ook de juiste neopixel aanpast

Kun jij mij zeggen wat ik nog moet wijzigen?

Waarschijnlijk iets in de void loop veranderen?

Tevens wil ik hem daarna uitbreiden naar 6 bits en 64 leds aan de output.

Ook heb ik het probleem dat hij de eerste led brand als de bits allemaal laag zijn.

Wellicht kan ik de formule voor de profile -1 doen of iets dergelijks.

Alvast bedankt voor je reactie

Hier de code:



/ TEST For "Profile switch" currently set to read BCD switch
// once during setup; move code as noted below for repeated reads

// BCD Switch inputs attached to pins D2-5, common to Ground
// (use internal pullups)

#include <Adafruit_NeoPixel.h>
//#include <avr/power.h>


// The Neopixels will be attached to pin 6
#define PIN 6

// How many Neopixels are attached to the Arduino?
#define NUMPIXELS 16

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS,PIN, NEO_GRB + NEO_KHZ800);


// Define data bit pins
int profile_BIT1 = 2;
int profile_BIT2 = 3;
int profile_BIT4 = 4;
int profile_BIT8 = 5;

// Define profile variable, set to 0

int profile = 0;

void setup() {
// initialize serial communication at 9600 bits per second (only needed
// for debugging:

Serial.begin(9600);

pixels.begin(); // This initializes the NeoPixel library.


// make the profile switch's pins inputs, with internal pullups on:
pinMode(profile_BIT1, INPUT_PULLUP);
pinMode(profile_BIT2, INPUT_PULLUP);
pinMode(profile_BIT4, INPUT_PULLUP);
pinMode(profile_BIT8, INPUT_PULLUP);

// to make read on each pass move all below this to "loop",
// uncomment profile reset


// read the input pins: NOTE - CLOSED switch (binary 1) returns "0"
int val_profile_BIT1 = digitalRead(profile_BIT1);
int val_profile_BIT2 = digitalRead(profile_BIT2);
int val_profile_BIT4 = digitalRead(profile_BIT4);
int val_profile_BIT8 = digitalRead(profile_BIT8);



// int profile = 0;
// needed in loop to reset to 0 for each pass

//turn bit values into single profile value by adding decimal value
//of each bit to profile variable

if ( val_profile_BIT1 == 0) { profile = profile + 1; }
if ( val_profile_BIT2 == 0) { profile = profile + 2; }
if ( val_profile_BIT4 == 0) { profile = profile + 4; }
if ( val_profile_BIT8 == 0) { profile = profile + 8; }

// DEBUG - prints out the state of the switch bits and profile value

{Serial.print("[profile_BIT1: ");
Serial.print(val_profile_BIT1);
Serial.print("] ");
Serial.print("[profile_BIT2: ");
Serial.print(val_profile_BIT2);
Serial.print("] ");
Serial.print("[profile_BIT4: ");
Serial.print(val_profile_BIT4);
Serial.print("] ");
Serial.print("[profile_BIT8: ");
Serial.print(val_profile_BIT8);
Serial.print("] ");
Serial.print("[profile: ");
Serial.print(profile);
Serial.println("] ");}
}



void loop() {

// delay in between reads for stability, uncomment if in loop
delay(1000);
pixels.setPixelColor(profile, 0,0,100);
pixels.show();
}

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: 4 bits bcd input en Neopixel output

Berichtdoor nicoverduin » 31 Okt 2015, 20:54

Volgens mij had ik je een voorbeeld programma gegeven waar je alleen de volgende regel hoef toe te voegen na deze regel:
cpp code
uint8_t bitje = !digitalRead(profileBitArray[i]);

deze regel erachter toevoegen
cpp code
profile += bitje;


Dus als je toch je eigen programma wilt voortzetten... dan moet je het hele stuk wat je nu doet in de setup waar je dus die bitjes leest, verplaatsen naar de loop.
Om er 6 bits van te maken heb je in jouw geval nog wel wat regels te dupliceren en aan te passen
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 10
Geregistreerd: 29 Dec 2014, 15:35
Woonplaats: Oosterwolde (Gld)

Re: 4 bits bcd input en Neopixel output

Berichtdoor Gert848 » 31 Okt 2015, 21:05

Gert848 schreef:
nicoverduin schreef:Gerhard
a)Je kan dat hele stuk in de setup() waar je de duim wiel leest naar een aparte functie zetten.
b) profile als variabele naar boven verplaatsen zodat hij in het gehele programma bekend is (is nu nog fout)
c) In de loop doe je dan een lees functie naar punt a
d) Daarna vul je het display.

Dus zoiets

cpp code
// TEST For "Profile switch" currently set to read BCD switch
// once during setup; move code as noted below for repeated reads

// BCD Switch inputs attached to pins D2-5, common to Ground
// (use internal pullups)

#include <Adafruit_NeoPixel.h>

//#include <avr/power.h>

// The Neopixels will be attached to pin 6
#define PIN 6

// How many Neopixels are attached to the Arduino?
#define NUMPIXELS 16

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN,
NEO_GRB + NEO_KHZ800);

//
// DATA BIT PINS
//
#define profile_BIT1 2
#define profile_BIT2 3
#define profile_BIT4 4
#define profile_BIT8 5
//
// omzetten BCD pinnern in een tabel
//
int profileBitArray[4] = { profile_BIT1, profile_BIT2, profile_BIT4, profile_BIT8 };

#define DEBUG // als we niet willen debuggen dan deze regel commentarieren

// Define profile variable, set to 0

int profile = 0;

void setup() {

#ifdef DEBUG
// initialize serial communication at 9600 bits per second (only needed
// for debugging:

Serial.begin(9600);

#endif

pixels.begin(); // This initializes the NeoPixel library.

for (uint8_t i = 0; i < sizeof(profileBitArray); i++) {
pinMode(profileBitArray[i], INPUT_PULLUP);
}


}

void loop() {
//
// lezen duimwiel
//
// to make read on each pass move all below this to "loop",
// uncomment profile reset

profile = 0; // Binaire resultaat van BCD
//
// omdat BCD en binair voor 0-9 identiek zijn
// kunnen we de bits er gewoon in schuiven
//
for (int8_t i = sizeof(profileBitArray) - 1; i >=0; i--) {
//
// vorige waarde een bit opschuiven naar links
//
profile << 1;
//
// inverteer het bitje dat gelezen wordt (pull down = 1) en tel deze op bij resultaat
//
uint8_t bitje = !digitalRead(profileBitArray[i]);

#ifdef DEBUG
Serial.print("[profile_BIT");
Serial.print((((i+1)*2) / 2), 1);
Serial.print(": ");
Serial.print(bitje);
Serial.print("] ");
#endif
}
#ifdef DEBUG
Serial.println(" ");
#endif
//
// wissen achtergrond
//
for (uint8_t i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, 0,0,0);
}
//
// display juiste pixel
//
pixels.setPixelColor(profile, 0, 0, 100);
pixels.show();
}


Hallo Nico,

Ik heb dit programma werken zoals ik ongeveer wil.

Alleen moet hij nu nog als de input verandert het ververst worden.

Dus als profile verandert dat dit ook de juiste neopixel aanpast

Kun jij mij zeggen wat ik nog moet wijzigen?

Waarschijnlijk iets in de void loop veranderen?

Tevens wil ik hem daarna uitbreiden naar 6 bits en 64 leds aan de output.

Ook heb ik het probleem dat hij de eerste led brand als de bits allemaal laag zijn.

Wellicht kan ik de formule voor de profile -1 doen of iets dergelijks.

Alvast bedankt voor je reactie

Hier de code:



/ TEST For "Profile switch" currently set to read BCD switch
// once during setup; move code as noted below for repeated reads

// BCD Switch inputs attached to pins D2-5, common to Ground
// (use internal pullups)

#include <Adafruit_NeoPixel.h>
//#include <avr/power.h>


// The Neopixels will be attached to pin 6
#define PIN 6

// How many Neopixels are attached to the Arduino?
#define NUMPIXELS 16

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS,PIN, NEO_GRB + NEO_KHZ800);


// Define data bit pins
int profile_BIT1 = 2;
int profile_BIT2 = 3;
int profile_BIT4 = 4;
int profile_BIT8 = 5;

// Define profile variable, set to 0

int profile = 0;

void setup() {
// initialize serial communication at 9600 bits per second (only needed
// for debugging:

Serial.begin(9600);

pixels.begin(); // This initializes the NeoPixel library.


// make the profile switch's pins inputs, with internal pullups on:
pinMode(profile_BIT1, INPUT_PULLUP);
pinMode(profile_BIT2, INPUT_PULLUP);
pinMode(profile_BIT4, INPUT_PULLUP);
pinMode(profile_BIT8, INPUT_PULLUP);

// to make read on each pass move all below this to "loop",
// uncomment profile reset


// read the input pins: NOTE - CLOSED switch (binary 1) returns "0"
int val_profile_BIT1 = digitalRead(profile_BIT1);
int val_profile_BIT2 = digitalRead(profile_BIT2);
int val_profile_BIT4 = digitalRead(profile_BIT4);
int val_profile_BIT8 = digitalRead(profile_BIT8);



// int profile = 0;
// needed in loop to reset to 0 for each pass

//turn bit values into single profile value by adding decimal value
//of each bit to profile variable

if ( val_profile_BIT1 == 0) { profile = profile + 1; }
if ( val_profile_BIT2 == 0) { profile = profile + 2; }
if ( val_profile_BIT4 == 0) { profile = profile + 4; }
if ( val_profile_BIT8 == 0) { profile = profile + 8; }

// DEBUG - prints out the state of the switch bits and profile value

{Serial.print("[profile_BIT1: ");
Serial.print(val_profile_BIT1);
Serial.print("] ");
Serial.print("[profile_BIT2: ");
Serial.print(val_profile_BIT2);
Serial.print("] ");
Serial.print("[profile_BIT4: ");
Serial.print(val_profile_BIT4);
Serial.print("] ");
Serial.print("[profile_BIT8: ");
Serial.print(val_profile_BIT8);
Serial.print("] ");
Serial.print("[profile: ");
Serial.print(profile);
Serial.println("] ");}
}



void loop() {

// delay in between reads for stability, uncomment if in loop
delay(1000);
pixels.setPixelColor(profile, 0,0,100);
pixels.show();
}
Hallo Nico,
Het gaat mij nog steeds ver boven de pet.
Ik krijg met jou programma de leds niet werken en ik begrijp niet goed hoe alles werkt.
Mijn programma werkt redelijk alleen als ik de duimwiel verander en de Arduino reset springt hij op de goede led.Ik hoop dat je het begrijpt wat ik graag wil maken. Ook de cpp code en de verwerking begrijp ik nog niet. Ik vind het enorm interressant maar begrijp er nog niet zoveel van.
Hopenlijk wil je nog eens naar mijn programma kijken. Alvast bedankt

Volgende

Terug naar Shields

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 5 gasten