Incorrect values being returned from an Array of Struct - Arduino












0















I am having a problem that I asked here but as the problem has changed somewhat, as has the code, I am starting a new question



I am trying to read and write values from an array of struct via the function



void BreezeMapPixels(struct_displayType& mDisplayUnit, int breezeStrength)


The values I am getting from the Serial.print are not those stored in the array. Specifically the one for endLed, which should never be 0



Output from Serial




Breeze Module
Start 8 End 0
Breeze Module
Start 1 End 0
Breeze Module
Start 1 End 0
Breeze Module
Start 4 End 7




These are defined as



// Define each display's LED range
{
displayUnit[0].startLed=0;
displayUnit[0].endLed=3;

displayUnit[1].startLed=4;
displayUnit[1].endLed=7;

displayUnit[2].startLed=8;
displayUnit[2].endLed=11;

displayUnit[3].startLed=12;
displayUnit[3].endLed=15;
}


Also the program seems to freze after a while.

Any help as to why I am reading the wrong values would be appreciated.





Full Code



/* **********************************
Breeze
* *********************************/
// Designed to illuminate a 'map' of pixels, each of which randomly
// sometimes twinkles brighter and then back down to it's base color again.
//



/* **********************************
Start of Code
* *********************************/
enum { SteadyDim, GettingBrighter, GettingDimmerAgain };


void InitPixelStates(struct_displayType& mDisplayUnit)
{
for ( uint16_t i = mDisplayUnit.startLed; i <= mDisplayUnit.endLed; i++) {
leds[i]=BASE_COLOR;
}
}

void BreezeMapPixels(struct_displayType& mDisplayUnit, int breezeStrength)
{
Serial.println("Breeze Module");
Serial.print("Start ");
Serial.print(mDisplayUnit.startLed);
Serial.print(" End " );
Serial.println(mDisplayUnit.endLed);


for ( uint16_t i = mDisplayUnit.startLed; i <= mDisplayUnit.endLed; i++) {
Serial.print(" pixel state " );
Serial.println(mDisplayUnit.pixelState[i] );
Serial.println(GettingBrighter);
Serial.println(GettingDimmerAgain);
Serial.println(SteadyDim);
if ( mDisplayUnit.pixelState[i] == SteadyDim) {
// this pixels is currently: SteadyDim
// so we randomly consider making it start getting brighter
leds[i]=BASE_COLOR;
if ( random8() < breezeStrength) {
mDisplayUnit.pixelState[i] = GettingBrighter;

}

} else if ( mDisplayUnit.pixelState[i] == GettingBrighter ) {
Serial.println("Brighter " + i);
// this pixels is currently: GettingBrighter
// so if it's at peak color, switch it to getting dimmer again
if ( leds[i] >= PEAK_COLOR ) {

mDisplayUnit.pixelState[i] = GettingDimmerAgain;
} else {
// otherwise, just keep brightening it:
leds[i] += DELTA_COLOR_UP;
}

} else { // getting dimmer again
Serial.println("Dimmer " + i);
// this pixels is currently: GettingDimmerAgain
// so if it's back to base color, switch it to steady dim
if ( leds[i] <= BASE_COLOR ) {

leds[i] = BASE_COLOR; // reset to exact base color, in case we overshot
mDisplayUnit.pixelState[i] = SteadyDim;
} else {
// otherwise, just keep dimming it down:
leds[i] -= DELTA_COLOR_DOWN;

}
}
}
}









share|improve this question























  • this is not the full code. in the other question pixelState array is not allocated in memory

    – Juraj
    Nov 28 '18 at 7:19













  • this is the full code

    – TimmyD
    Nov 28 '18 at 9:39











  • It isn't, not even code from the other question can be compiled.

    – KIIV
    Nov 29 '18 at 9:28
















0















I am having a problem that I asked here but as the problem has changed somewhat, as has the code, I am starting a new question



I am trying to read and write values from an array of struct via the function



void BreezeMapPixels(struct_displayType& mDisplayUnit, int breezeStrength)


The values I am getting from the Serial.print are not those stored in the array. Specifically the one for endLed, which should never be 0



Output from Serial




Breeze Module
Start 8 End 0
Breeze Module
Start 1 End 0
Breeze Module
Start 1 End 0
Breeze Module
Start 4 End 7




These are defined as



// Define each display's LED range
{
displayUnit[0].startLed=0;
displayUnit[0].endLed=3;

displayUnit[1].startLed=4;
displayUnit[1].endLed=7;

displayUnit[2].startLed=8;
displayUnit[2].endLed=11;

displayUnit[3].startLed=12;
displayUnit[3].endLed=15;
}


Also the program seems to freze after a while.

Any help as to why I am reading the wrong values would be appreciated.





Full Code



/* **********************************
Breeze
* *********************************/
// Designed to illuminate a 'map' of pixels, each of which randomly
// sometimes twinkles brighter and then back down to it's base color again.
//



/* **********************************
Start of Code
* *********************************/
enum { SteadyDim, GettingBrighter, GettingDimmerAgain };


void InitPixelStates(struct_displayType& mDisplayUnit)
{
for ( uint16_t i = mDisplayUnit.startLed; i <= mDisplayUnit.endLed; i++) {
leds[i]=BASE_COLOR;
}
}

void BreezeMapPixels(struct_displayType& mDisplayUnit, int breezeStrength)
{
Serial.println("Breeze Module");
Serial.print("Start ");
Serial.print(mDisplayUnit.startLed);
Serial.print(" End " );
Serial.println(mDisplayUnit.endLed);


for ( uint16_t i = mDisplayUnit.startLed; i <= mDisplayUnit.endLed; i++) {
Serial.print(" pixel state " );
Serial.println(mDisplayUnit.pixelState[i] );
Serial.println(GettingBrighter);
Serial.println(GettingDimmerAgain);
Serial.println(SteadyDim);
if ( mDisplayUnit.pixelState[i] == SteadyDim) {
// this pixels is currently: SteadyDim
// so we randomly consider making it start getting brighter
leds[i]=BASE_COLOR;
if ( random8() < breezeStrength) {
mDisplayUnit.pixelState[i] = GettingBrighter;

}

} else if ( mDisplayUnit.pixelState[i] == GettingBrighter ) {
Serial.println("Brighter " + i);
// this pixels is currently: GettingBrighter
// so if it's at peak color, switch it to getting dimmer again
if ( leds[i] >= PEAK_COLOR ) {

mDisplayUnit.pixelState[i] = GettingDimmerAgain;
} else {
// otherwise, just keep brightening it:
leds[i] += DELTA_COLOR_UP;
}

} else { // getting dimmer again
Serial.println("Dimmer " + i);
// this pixels is currently: GettingDimmerAgain
// so if it's back to base color, switch it to steady dim
if ( leds[i] <= BASE_COLOR ) {

leds[i] = BASE_COLOR; // reset to exact base color, in case we overshot
mDisplayUnit.pixelState[i] = SteadyDim;
} else {
// otherwise, just keep dimming it down:
leds[i] -= DELTA_COLOR_DOWN;

}
}
}
}









share|improve this question























  • this is not the full code. in the other question pixelState array is not allocated in memory

    – Juraj
    Nov 28 '18 at 7:19













  • this is the full code

    – TimmyD
    Nov 28 '18 at 9:39











  • It isn't, not even code from the other question can be compiled.

    – KIIV
    Nov 29 '18 at 9:28














0












0








0








I am having a problem that I asked here but as the problem has changed somewhat, as has the code, I am starting a new question



I am trying to read and write values from an array of struct via the function



void BreezeMapPixels(struct_displayType& mDisplayUnit, int breezeStrength)


The values I am getting from the Serial.print are not those stored in the array. Specifically the one for endLed, which should never be 0



Output from Serial




Breeze Module
Start 8 End 0
Breeze Module
Start 1 End 0
Breeze Module
Start 1 End 0
Breeze Module
Start 4 End 7




These are defined as



// Define each display's LED range
{
displayUnit[0].startLed=0;
displayUnit[0].endLed=3;

displayUnit[1].startLed=4;
displayUnit[1].endLed=7;

displayUnit[2].startLed=8;
displayUnit[2].endLed=11;

displayUnit[3].startLed=12;
displayUnit[3].endLed=15;
}


Also the program seems to freze after a while.

Any help as to why I am reading the wrong values would be appreciated.





Full Code



/* **********************************
Breeze
* *********************************/
// Designed to illuminate a 'map' of pixels, each of which randomly
// sometimes twinkles brighter and then back down to it's base color again.
//



/* **********************************
Start of Code
* *********************************/
enum { SteadyDim, GettingBrighter, GettingDimmerAgain };


void InitPixelStates(struct_displayType& mDisplayUnit)
{
for ( uint16_t i = mDisplayUnit.startLed; i <= mDisplayUnit.endLed; i++) {
leds[i]=BASE_COLOR;
}
}

void BreezeMapPixels(struct_displayType& mDisplayUnit, int breezeStrength)
{
Serial.println("Breeze Module");
Serial.print("Start ");
Serial.print(mDisplayUnit.startLed);
Serial.print(" End " );
Serial.println(mDisplayUnit.endLed);


for ( uint16_t i = mDisplayUnit.startLed; i <= mDisplayUnit.endLed; i++) {
Serial.print(" pixel state " );
Serial.println(mDisplayUnit.pixelState[i] );
Serial.println(GettingBrighter);
Serial.println(GettingDimmerAgain);
Serial.println(SteadyDim);
if ( mDisplayUnit.pixelState[i] == SteadyDim) {
// this pixels is currently: SteadyDim
// so we randomly consider making it start getting brighter
leds[i]=BASE_COLOR;
if ( random8() < breezeStrength) {
mDisplayUnit.pixelState[i] = GettingBrighter;

}

} else if ( mDisplayUnit.pixelState[i] == GettingBrighter ) {
Serial.println("Brighter " + i);
// this pixels is currently: GettingBrighter
// so if it's at peak color, switch it to getting dimmer again
if ( leds[i] >= PEAK_COLOR ) {

mDisplayUnit.pixelState[i] = GettingDimmerAgain;
} else {
// otherwise, just keep brightening it:
leds[i] += DELTA_COLOR_UP;
}

} else { // getting dimmer again
Serial.println("Dimmer " + i);
// this pixels is currently: GettingDimmerAgain
// so if it's back to base color, switch it to steady dim
if ( leds[i] <= BASE_COLOR ) {

leds[i] = BASE_COLOR; // reset to exact base color, in case we overshot
mDisplayUnit.pixelState[i] = SteadyDim;
} else {
// otherwise, just keep dimming it down:
leds[i] -= DELTA_COLOR_DOWN;

}
}
}
}









share|improve this question














I am having a problem that I asked here but as the problem has changed somewhat, as has the code, I am starting a new question



I am trying to read and write values from an array of struct via the function



void BreezeMapPixels(struct_displayType& mDisplayUnit, int breezeStrength)


The values I am getting from the Serial.print are not those stored in the array. Specifically the one for endLed, which should never be 0



Output from Serial




Breeze Module
Start 8 End 0
Breeze Module
Start 1 End 0
Breeze Module
Start 1 End 0
Breeze Module
Start 4 End 7




These are defined as



// Define each display's LED range
{
displayUnit[0].startLed=0;
displayUnit[0].endLed=3;

displayUnit[1].startLed=4;
displayUnit[1].endLed=7;

displayUnit[2].startLed=8;
displayUnit[2].endLed=11;

displayUnit[3].startLed=12;
displayUnit[3].endLed=15;
}


Also the program seems to freze after a while.

Any help as to why I am reading the wrong values would be appreciated.





Full Code



/* **********************************
Breeze
* *********************************/
// Designed to illuminate a 'map' of pixels, each of which randomly
// sometimes twinkles brighter and then back down to it's base color again.
//



/* **********************************
Start of Code
* *********************************/
enum { SteadyDim, GettingBrighter, GettingDimmerAgain };


void InitPixelStates(struct_displayType& mDisplayUnit)
{
for ( uint16_t i = mDisplayUnit.startLed; i <= mDisplayUnit.endLed; i++) {
leds[i]=BASE_COLOR;
}
}

void BreezeMapPixels(struct_displayType& mDisplayUnit, int breezeStrength)
{
Serial.println("Breeze Module");
Serial.print("Start ");
Serial.print(mDisplayUnit.startLed);
Serial.print(" End " );
Serial.println(mDisplayUnit.endLed);


for ( uint16_t i = mDisplayUnit.startLed; i <= mDisplayUnit.endLed; i++) {
Serial.print(" pixel state " );
Serial.println(mDisplayUnit.pixelState[i] );
Serial.println(GettingBrighter);
Serial.println(GettingDimmerAgain);
Serial.println(SteadyDim);
if ( mDisplayUnit.pixelState[i] == SteadyDim) {
// this pixels is currently: SteadyDim
// so we randomly consider making it start getting brighter
leds[i]=BASE_COLOR;
if ( random8() < breezeStrength) {
mDisplayUnit.pixelState[i] = GettingBrighter;

}

} else if ( mDisplayUnit.pixelState[i] == GettingBrighter ) {
Serial.println("Brighter " + i);
// this pixels is currently: GettingBrighter
// so if it's at peak color, switch it to getting dimmer again
if ( leds[i] >= PEAK_COLOR ) {

mDisplayUnit.pixelState[i] = GettingDimmerAgain;
} else {
// otherwise, just keep brightening it:
leds[i] += DELTA_COLOR_UP;
}

} else { // getting dimmer again
Serial.println("Dimmer " + i);
// this pixels is currently: GettingDimmerAgain
// so if it's back to base color, switch it to steady dim
if ( leds[i] <= BASE_COLOR ) {

leds[i] = BASE_COLOR; // reset to exact base color, in case we overshot
mDisplayUnit.pixelState[i] = SteadyDim;
} else {
// otherwise, just keep dimming it down:
leds[i] -= DELTA_COLOR_DOWN;

}
}
}
}






arrays struct arduino






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 28 '18 at 5:09









TimmyDTimmyD

66657




66657













  • this is not the full code. in the other question pixelState array is not allocated in memory

    – Juraj
    Nov 28 '18 at 7:19













  • this is the full code

    – TimmyD
    Nov 28 '18 at 9:39











  • It isn't, not even code from the other question can be compiled.

    – KIIV
    Nov 29 '18 at 9:28



















  • this is not the full code. in the other question pixelState array is not allocated in memory

    – Juraj
    Nov 28 '18 at 7:19













  • this is the full code

    – TimmyD
    Nov 28 '18 at 9:39











  • It isn't, not even code from the other question can be compiled.

    – KIIV
    Nov 29 '18 at 9:28

















this is not the full code. in the other question pixelState array is not allocated in memory

– Juraj
Nov 28 '18 at 7:19







this is not the full code. in the other question pixelState array is not allocated in memory

– Juraj
Nov 28 '18 at 7:19















this is the full code

– TimmyD
Nov 28 '18 at 9:39





this is the full code

– TimmyD
Nov 28 '18 at 9:39













It isn't, not even code from the other question can be compiled.

– KIIV
Nov 29 '18 at 9:28





It isn't, not even code from the other question can be compiled.

– KIIV
Nov 29 '18 at 9:28












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53512532%2fincorrect-values-being-returned-from-an-array-of-struct-arduino%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53512532%2fincorrect-values-being-returned-from-an-array-of-struct-arduino%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Lallio

Futebolista

Jornalista