for (int p = 0; p < 21; p++){ // 21 pulses at ~37 KHz
digitalWrite(9, HIGH);
delayMicroseconds(9);
digitalWrite(9, LOW);
delayMicroseconds(9);}
}
No matter what I try, IDE stops compiling on that line with the error message:
"Expected initializer before 'for'. It doesn't help that I haven't the slightest idea what an initializer is.
One possible reason is the additional } at the end of the presented code. Another reason is that the error is somewhere else. But nobody will be able to tell you without seeing the complete code.
This happily compiles
void setup()
{
for (int p = 0; p < 21; p++) { // 21 pulses at ~37 KHz
digitalWrite(9, HIGH);
delayMicroseconds(9);
digitalWrite(9, LOW);
delayMicroseconds(9);
}
}
void loop()
{
}
Not this time. The given snippet pasted into a bare minimum sketch, and with <ctrl> T applied gives an error of
void setup() {
Serial.begin(115200);
}
void loop() {
for (int p = 0; p < 21; p++) { // 21 pulses at ~37 KHz
digitalWrite(9, HIGH);
delayMicroseconds(9);
digitalWrite(9, LOW);
delayMicroseconds(9);
}
}
}
The correct formatting easily shows the dangling curly brace noted by @sterretje at the end of the sketch. Remove the offending brace and the sketch compiles.
"Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" ... I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
OK, here's the sketch minimized to just the portion that transmits an IR signal each time a trigger signal is detected at D3. It loads and runs perfectly, even with what appears to be a dangling curly bracket at the end. Before I integrate this into my application for it I'm hoping someone can show me how to reduce the code from the two functions I'm now using ('zerobit' and 'onebit") by putting the 32 data bits into an array of "1"s and "0"s and using one of those tricky "for" loops to ripple thru the array and transmit the bits in sequence.
// Samsung_MUTE_Signal_Generator_Minimum_07-24-2022
void setup() {
pinMode(3, INPUT); // HIGH on this pin triggers xmission
pinMode(9, OUTPUT); // pin 9 drives IR LED xmitter
}
void loop() {
while (digitalRead(3) == LOW) {} // wait for start signal to go high
for (int p = 0; p < 171; p++) { // xmit carrier burst of lead-in
digitalWrite(9, HIGH);
delayMicroseconds(9);
digitalWrite(9, LOW);
delayMicroseconds(9);
}
delayMicroseconds(4500); // OFF time of lead-in
onebit(); // xmit 32 data bits of IR signal
onebit();
onebit();
zerobit();
zerobit();
zerobit();
zerobit();
zerobit();
onebit();
onebit();
onebit();
zerobit();
zerobit();
zerobit();
zerobit();
zerobit();
onebit();
onebit();
onebit();
onebit();
zerobit();
zerobit();
zerobit();
zerobit();
zerobit();
zerobit();
zerobit();
zerobit();
onebit();
onebit();
onebit();
onebit();
onebit();
while (digitalRead(3) == HIGH) {} // wait for D3 to return low
}
void zerobit() { // function to xmit "0" bit
for (int p = 0; p < 21; p++){ // 21 cycles of 37 KHz carrier
digitalWrite(9, HIGH);
delayMicroseconds(9);
digitalWrite(9, LOW);
delayMicroseconds(9);}
delayMicroseconds(590); // 590 uSec low = "0" bit
}
void onebit() { // function to xmit "1" bit
for (int p = 0; p < 21; p++){ // 21 cycles of 37 KHz carrier
digitalWrite(9, HIGH);
delayMicroseconds(9);
digitalWrite(9, LOW);
delayMicroseconds(9);}
delayMicroseconds(1690); // 1690 uSec low = "1" bit
}
// Samsung_MUTE_Signal_Generator_Minimum_07-24-2022
// fedcba9876543210fedcba9876543210
//0b111110000000011110000011100000111 // based on the original sketch
// 0b11110000000011110000011100000111 // but only used 32 bit in this sketch
const uint32_t data = 0b11110000000011110000011100000111;
void setup() {
Serial.begin(115200);
pinMode(3, INPUT); // HIGH on this pin triggers xmission
pinMode(9, OUTPUT); // pin 9 drives IR LED xmitter
}
void loop() {
while (digitalRead(3) == LOW) {} // wait for start signal to go high
for (int p = 0; p < 171; p++) { // xmit carrier burst of lead-in
digitalWrite(9, HIGH);
delayMicroseconds(9);
digitalWrite(9, LOW);
delayMicroseconds(9);
}
delayMicroseconds(4500); // OFF time of lead-in
sendLSBfirst(data);
while (digitalRead(3) == HIGH) {} // wait for D3 to return low
}
void sendLSBfirst(uint32_t content)
{
for (byte i = 0; i < 32; i++)
{
if (content & 1UL << i)
{
onebit();
//Serial.print('1'); // debug only
}
else
{
zerobit();
//Serial.print('0'); // debug only
}
}
//Serial.println(); // debug only
}
void anybit(uint16_t us)
{
for (int p = 0; p < 21; p++) { // 21 cycles of 37 KHz carrier
digitalWrite(9, HIGH);
delayMicroseconds(9);
digitalWrite(9, LOW);
delayMicroseconds(9);
}
delayMicroseconds(us);
}
void zerobit() { // function to xmit "0" bit
anybit(590);
}
void onebit() { // function to xmit "1" bit
anybit(1690);
}
noiasca, your sketch loads and runs just fine, but your encoding is off, probably because of the 32/33 bit mixup. See attached comparison between your signal and mine. I tried messing with that 32 bit code string at start of your sketch, but nothing I tried worked.
Note that at the far right end my signal ends with four "1" bits, and the next burst is just to get back to baseline. Your signal ends with just three"1" bits. I don't know if that's the only way to end it, but I did what was necessary to make my signal look exactly like one from a Samsung OEM remote control. I'll try anything you send me.