I'm testing out some code to determine how to manipulate the upper byte of a 4 byte number. I created this test but it's not displaying what I would expect.
Displays "FFFFC03F" and I would have expected "807FC03F"
I plan on using this manipulation to pass the combined number to IRRemote. I need to test a series of codes so I need to loop through values of arr[0] (arr[1] is just ~arr[0] and arr[3]=~arr[2]) and then combine the bytes to pass to the IR send command.
I must be missing something obvious but I don't see it!
Thanks for the help. I'm trying the web IDE and perhaps that's causing some unexpected results. I ran your code with the casting and that does work. I was working on this last night and added code to loop through the possible codes but I'm having trouble with that also for some reason.
I added the runXTimes flag so the code would only loop once. The code continually loops instead of only running once.
I would have used a bool, initialized to true, and set to false in the body of the if statement.
I would look at Print::print() to see if there is an overload for uint32_t. I doubt that there is. So, you need to determine which close match method is actually invoked, to see if it is behaving correctly.
The online reference only states "val: the value to print - any data type" so that doesn't help me much to determine if an overload is occurring or not.
I was trying to use Putty (I'm running Win7) because the serial monitor in the Arduino IDE doesn't clear. Sometimes I get non-stop data streaming (in either Putty or Serial Monitor) but most time I don't get any output.
I initially had my board connected to a USB 3.0 hub and I thought that might be an issue so I switched to a USB2.0 port on the PC and am getting the same results. If I reset the board I can see it enumerate initially as COM15 bootloader and then COM14 so that might be causing an issue with Putty?
I also tried chaning runXTimes flag to a boolean but that didn't seem to help as I get the same inconsistent results.
I thought this would only take a little bit to run these tests but I must be jinxed.
What everyone else said: one of the values is being treated as signed and then sign-extended when it is promoted.
I would guess that the culprit is [nobbc]arr[2]<<8[/nobbc]. This gets promoted twice: once to int (to do the shift) and then to unsigned long (for the bitwise or). The Bad Thing is happenning at that second promotion.
@oqibidipo the variable compliment is defined as uint32_t at the top of the program (see latest code below). Sorry, but I'm still confused why the unsigned long definition seemed to work but using uint32_t doesn't work in this code.
#include <IRremote.h>
IRsend irsend;
uint32_t lower_part = 0xC03F;
uint32_t codes[0xFF];
bool runXTimes = true;
uint32_t value = 0;
uint32_t compliment;
void setup()
{
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
int khz = 38; // 38kHz carrier frequency for the NEC protocol
value = 0x80;
compliment = ~value;
codes[value] = (value << 24) | (compliment << 16) | (lower_part);
irsend.sendNEC(codes[value], 32); //ON/OFF
//irsend.sendNEC(0x807FC03F, 32); //ON/OFF
delay(5000); //In this example, the signal will be repeated every 5 seconds, approximately.
}