Still trying to learn programming

Hello, everyone I have a Trinket 5v, I’m trying to merge to different sketches in to one program. What the Hell is wrong with this code?

[code]
/*
  Button
  Turns on an LED when a switch connected from #0 to ground is pressed

  This example code is in the public domain.

  To upload to your Gemma or Trinket:
  1) Select the proper board from the Tools->Board Menu
  2) Select USBtinyISP from the Tools->Programmer
  3) Plug in the Gemma/Trinket, make sure you see the green LED lit
  4) For windows, install the USBtiny drivers
  5) Press the button on the Gemma/Trinket - verify you see
     the red LED pulse. This means it is ready to receive data
  6) Click the upload button above within 10 seconds
*/

#define SWITCH 0
#define LED 1
#define LED 2
#define listenFORIR 3  // ATTiny85 had Port B pins
#define SPEAKERPIN 4  // Piezo speaker on Trinket/Gemma Pin GPIO #4/D4
#define MAXPULSE    350  // the maximum pulse we'll listen for - 5 milliseconds 
#define NUMPULSES    2  // max IR pulse pairs to sample
#define RESOLUTION     2  // // time between IR measurements

// we will store up to 100 pulse pairs (this is -a lot-, reduce if needed)
uint16_t pulses[2][2];   // pair is high and low pulse
uint16_t currentpulse = 0; // index for pulses we're storing
uint32_t irCode = 0;

void setup() {
  irCode = listenForIR
  // initialize the LED pin as an output.
  pinMode( 1, OUTPUT);
  // initialize the LED pin as an output.
  pinMode( 2, OUTPUT);
  // initialize the SWITCH pin as an input with a pullup
  pinMode(0, INPUT_PULLUP);
  pinMode( listenFORIR 3, INPUT);       // Listen to IR receiver on Trinket/Gemma pin D3
  pinMode( 4, OUTPUT); // Output tones on Trinket/Gemma pin D4
}

// the loop routine runs over and over again forever:
void loop() {

  irCode = listenForIR(); // Wait for an IR Code

  // Process the pulses to get our code
  ; for (int i = 0; i < 32; i++) {
    irCode = irCode << 1;
    if ((pulses[i][0] * RESOLUTION) > 0 && (pulses[i][0] * RESOLUTION) < 500) {
      irCode |= 0;
    } else {
      irCode |= 1;
    }
  }
  if (irCode == 0xe0c8A2DD)  {   // "1" on my remote, USE YOUR CODE HERE
    beep(pinMode 4, 400, 500);
  }  // Make a 400 Hz beep
  else if (irCode == 0xe0c8a3dc) { // "2", USE YOUR OWN HEX CODE HERE
    beep(pinMode 4, 500, 500);
  }  // Make a 500 Hz beep
  else if (irCode == 0xe0c8a41b) { // "3", USE YOUR OWN HEX CODE HERE
    beep(pinMode 4, 600, 500);
  } // Make a 600 Hz beep
  else if (irCode == 0xe0c8a29d) { // "4", USE YOUR OWN HEX CODE HERE
    beep(pinMode 4, 700, 500);  // Make a 700 Hz beep

  }

} // end loop

if (! digitalRead( 0)) {  // if the button is pressed
  digitalWrite( 1, HIGH);    // light up the LED
} else {
  digitalWrite( 1, LOW);     // otherwise, turn it off
}
if (! digitalRead( 0)) {  // if the button is pressed
  digitalWrite( 2, HIGH);    // light up the LED
} else {
  digitalWrite( 2, LOW);     // otherwise, turn it off
}

} // end loop


uint16_t listenForir() {  // IR receive code
  currentpulse = 0;
  while (1) {
    unsigned int highpulse, lowpulse;  // temporary storage timing
    highpulse = lowpulse = 0; // start out with no pulse length

    while (IRpin_PIN & _BV(IRpin)) { // got a high pulse
      highpulse++;
      delayMicroseconds(RESOLUTION);
      if (((highpulse >= MAXPULSE) && (currentpulse != 0)) || currentpulse == NUMPULSES) {
        return currentpulse;
      }
    }
    pulses[currentpulse][0] = highpulse;

    while (! (IRpin_PIN & _BV(IRpin))) { // got a low pulse
      lowpulse++;
      delayMicroseconds(RESOLUTION);
      if (((lowpulse >= MAXPULSE) && (currentpulse != 0)) || currentpulse == NUMPULSES) {
        return currentpulse;
      }
    }
    pulses[currentpulse][1] = lowpulse;
    currentpulse++;
  }

}

// Generate a tone on speakerPin - Trinket/Gemma/ATTiny85 compatible
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
{ // http://web.media.mit.edu/~leah/LilyPad/07_sound_code.html
  int x;
  long delayAmount = (long)(1000000 / frequencyInHertz);
  long loopTime = (long)((timeInMilliseconds * 1000) / (delayAmount * 2));
  for (x = 0; x < loopTime; x++)
  {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(delayAmount);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(delayAmount);
  }


}

[/code]

What errors do you get when it’s compiled?
Use the button in the IDE to cut & paste them into the or tags in your next reply.

IGNORE. At first glance, I’d be looking for unbalanced {curly braces}.

terry546:
What the Hell is wrong with this code?

Most of the comments are rubbish making working out what it's supposed to do difficult. And you haven't said what it is supposed to do or what it does or doesn't do.

Just on a quick glance, you have two lines marked "end loop", one of them is lying. There's plenty of code not in any function. listenForir and listenForIR are not the same thing and you can't leave the () off when calling a function.

Steve

In the context of your Title, I think the common message from Replies #1 and #2 is that successful programming requires meticulous attention to detail.

And it is a very good strategy to "test early and often" so that you catch a problem when there is only one problem.

If a program won't compile then don't add code for any other activity until you have fixed the compile problem.

If the program does compile successfully but does not do what you want then fix that before adding code for any other activity.

...R

Robin2:
And it is a very good strategy to "test early and often" so that you catch a problem when there is only one problem.

If a program won't compile then don't add code for any other activity until you have fixed the compile problem.

If the program does compile successfully but does not do what you want then fix that before adding code for any other activity.

The methodology quoted above is also known as: SSS Strategy which stands for Small Start Strategy -- a method in which you start with a basic (simple and elementary) hardware and software unit; make it work; add another basic unit; make this one work; continue like this of adding the next basic unit until the whole project is built and tested.

GolamMostafa:
The methodology quoted above is also known as: SSS Strategy which stands for Small Start Strategy -- a method in which you start with a basic (simple and elementary) hardware and software unit; make it work; add another basic unit; make this one work; continue like this of adding the next basic unit until the whole project is built and tested.

I did not know it was called Small Start Strategy but I am 100% behind you and @Robin2.
starting with something really simple, just printing "Hello world" even, and then gradually building on that avoids frustration, because you always have a program the works, and leads to robust code.

When I'm about to use a new (or infrequently used) programming technique or a piece of equipment, I invariably hop out of my project and open a New sketch to do some messing around. I usually give it the name of my main project, with a suffix like "_prep_accelstepper" which tells me it's where I tested the use of a new library.

I can make a total balls of what I'm doing in that _prep sketch until I'm happy to take some working code back to the main sketch. Even then, I do a Save As of the main sketch, and add a suffix like "_with_accelstepper" so it's easy to revert to some known-good code if I make a boo-boo.

terry546:
What the Hell is wrong with this code?

} // end loop
.
.
.
} // end loop

The fact that you have two lines marking the end of the loop() function is a problem. Get rid of the first end.

Also, CAPITALIZATION COUNTS!
Function call:

void loop()
{
  irCode = listenForIR(); // Wait for an IR Code

Function declaration:

uint16_t listenForir()    // IR receive code

Those are two different names.

You have this weird partial line in setup():

  irCode = listenForIR

What is the function name 'pinMode' doing in these lines?!??

  if (irCode == 0xe0c8A2DD)      // "1" on my remote, USE YOUR CODE HERE
  {
    beep(pinMode 4, 400, 500);
  }  // Make a 400 Hz beep
  else if (irCode == 0xe0c8a3dc)   // "2", USE YOUR OWN HEX CODE HERE
  {
    beep(pinMode 4, 500, 500);
  }  // Make a 500 Hz beep
  else if (irCode == 0xe0c8a41b)   // "3", USE YOUR OWN HEX CODE HERE
  {
    beep(pinMode 4, 600, 500);
  } // Make a 600 Hz beep
  else if (irCode == 0xe0c8a29d)   // "4", USE YOUR OWN HEX CODE HERE
  {
    beep(pinMode 4, 700, 500);  // Make a 700 Hz beep
  }

We were all new to programming at one time. Some of us more recent than others.

First, do the individual sketches work separately?

The line:

#define listenFORIR 3  // ATTiny85 had Port B pins

is extremely confusing because you have a function named listenForir and calls to listenForIR.
listenForir, listenForIR and listenFORIR are different entities. C++ is case-sensitive.

Also, the first line in setup() is wrong:

irCode = listenForIR

This has three problems.
no () for the function call, and no ending semicolon.
The correct line is

irCode = listenForir();

And the third problem for this line- what the heck is it doing here? You are calling a function that reads an I/O pin before setting the pinmode.

Use CTRL-T to clean up your code. You should not see two consecutive closing braces at the same level:

}
} // end loop

This means that you have an extra closing brace in your code.

You can go through your code by placing the cursor at a closing brace, and the IDE will highlight it's corresponding opening brace:

I suspect that the first

} // end loop

is the extra one.

This line will compile, but the semicolon is superfluous:

 ; for (int i = 0; i < 32; i++) {

Fix all this and you might have more success.

This is your original program

[code]
/*
  Button
  Turns on an LED when a switch connected from #0 to ground is pressed

  This example code is in the public domain.

  To upload to your Gemma or Trinket:
  1) Select the proper board from the Tools->Board Menu
  2) Select USBtinyISP from the Tools->Programmer
  3) Plug in the Gemma/Trinket, make sure you see the green LED lit
  4) For windows, install the USBtiny drivers
  5) Press the button on the Gemma/Trinket - verify you see
     the red LED pulse. This means it is ready to receive data
  6) Click the upload button above within 10 seconds
*/

#define SWITCH 0
#define LED 1
#define LED 2
#define listenFORIR 3  // ATTiny85 had Port B pins
#define SPEAKERPIN 4  // Piezo speaker on Trinket/Gemma Pin GPIO #4/D4
#define MAXPULSE    350  // the maximum pulse we'll listen for - 5 milliseconds
#define NUMPULSES    2  // max IR pulse pairs to sample
#define RESOLUTION     2  // // time between IR measurements

// we will store up to 100 pulse pairs (this is -a lot-, reduce if needed)
uint16_t pulses[2][2];   // pair is high and low pulse
uint16_t currentpulse = 0; // index for pulses we're storing
uint32_t irCode = 0;

void setup() {
  irCode = listenForIR
  // initialize the LED pin as an output.
  pinMode( 1, OUTPUT);
  // initialize the LED pin as an output.
  pinMode( 2, OUTPUT);
  // initialize the SWITCH pin as an input with a pullup
  pinMode(0, INPUT_PULLUP);
  pinMode( listenFORIR 3, INPUT);       // Listen to IR receiver on Trinket/Gemma pin D3
  pinMode( 4, OUTPUT); // Output tones on Trinket/Gemma pin D4
}

// the loop routine runs over and over again forever:
void loop() {

  irCode = listenForIR(); // Wait for an IR Code

  // Process the pulses to get our code
  ; for (int i = 0; i < 32; i++) {
    irCode = irCode << 1;
    if ((pulses[i][0] * RESOLUTION) > 0 && (pulses[i][0] * RESOLUTION) < 500) {
      irCode |= 0;
    } else {
      irCode |= 1;
    }
  }
  if (irCode == 0xe0c8A2DD)  {   // "1" on my remote, USE YOUR CODE HERE
    beep(pinMode 4, 400, 500);
  }  // Make a 400 Hz beep
  else if (irCode == 0xe0c8a3dc) { // "2", USE YOUR OWN HEX CODE HERE
    beep(pinMode 4, 500, 500);
  }  // Make a 500 Hz beep
  else if (irCode == 0xe0c8a41b) { // "3", USE YOUR OWN HEX CODE HERE
    beep(pinMode 4, 600, 500);
  } // Make a 600 Hz beep
  else if (irCode == 0xe0c8a29d) { // "4", USE YOUR OWN HEX CODE HERE
    beep(pinMode 4, 700, 500);  // Make a 700 Hz beep

  }

} // end loop

if (! digitalRead( 0)) {  // if the button is pressed
  digitalWrite( 1, HIGH);    // light up the LED
} else {
  digitalWrite( 1, LOW);     // otherwise, turn it off
}
if (! digitalRead( 0)) {  // if the button is pressed
  digitalWrite( 2, HIGH);    // light up the LED
} else {
  digitalWrite( 2, LOW);     // otherwise, turn it off
}

} // end loop


uint16_t listenForir() {  // IR receive code
  currentpulse = 0;
  while (1) {
    unsigned int highpulse, lowpulse;  // temporary storage timing
    highpulse = lowpulse = 0; // start out with no pulse length

    while (IRpin_PIN & _BV(IRpin)) { // got a high pulse
      highpulse++;
      delayMicroseconds(RESOLUTION);
      if (((highpulse >= MAXPULSE) && (currentpulse != 0)) || currentpulse == NUMPULSES) {
        return currentpulse;
      }
    }
    pulses[currentpulse][0] = highpulse;

    while (! (IRpin_PIN & _BV(IRpin))) { // got a low pulse
      lowpulse++;
      delayMicroseconds(RESOLUTION);
      if (((lowpulse >= MAXPULSE) && (currentpulse != 0)) || currentpulse == NUMPULSES) {
        return currentpulse;
      }
    }
    pulses[currentpulse][1] = lowpulse;
    currentpulse++;
  }

}

// Generate a tone on speakerPin - Trinket/Gemma/ATTiny85 compatible
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
{ // http://web.media.mit.edu/~leah/LilyPad/07_sound_code.html
  int x;
  long delayAmount = (long)(1000000 / frequencyInHertz);
  long loopTime = (long)((timeInMilliseconds * 1000) / (delayAmount * 2));
  for (x = 0; x < loopTime; x++)
  {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(delayAmount);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(delayAmount);
  }


}

This is your program after aligning the {}s and deleting some of the comments. Now, you can go line by line and see what are the extra things (the language elements).

#define SWITCH 0
#define LED 1
#define LED 2
#define listenFORIR 3  // ATTiny85 had Port B pins
#define SPEAKERPIN 4  // Piezo speaker on Trinket/Gemma Pin GPIO #4/D4
#define MAXPULSE    350  // the maximum pulse we'll listen for - 5 milliseconds
#define NUMPULSES    2  // max IR pulse pairs to sample
#define RESOLUTION     2  // // time between IR measurements

uint16_t pulses[2][2];   // pair is high and low pulse
uint16_t currentpulse = 0; // index for pulses we're storing
uint32_t irCode = 0;

void setup() 
{
  irCode = listenForIR
  pinMode( 1, OUTPUT);
  pinMode( 2, OUTPUT);
  pinMode(0, INPUT_PULLUP);
  pinMode( listenFORIR 3, INPUT);       // Listen to IR receiver on Trinket/Gemma pin D3
  pinMode( 4, OUTPUT);                     // Output tones on Trinket/Gemma pin D4
}

void loop() 
{
  irCode = listenForIR(); // Wait for an IR Code
  ; 
  
  for (int i = 0; i < 32; i++) 
  {
    irCode = irCode << 1;
    if ((pulses[i][0] * RESOLUTION) > 0 && (pulses[i][0] * RESOLUTION) < 500) 
    {
      irCode |= 0;
    } 
    else 
    {
      irCode |= 1;
    }
  }
  
  if (irCode == 0xe0c8A2DD)  
  {   
    beep(pinMode 4, 400, 500);
  } 
  
  else if (irCode == 0xe0c8a3dc) 
  { 
    beep(pinMode 4, 500, 500);
  }  
 
  else if (irCode == 0xe0c8a41b) 
  { 
    beep(pinMode 4, 600, 500);
  } 
  
  else if (irCode == 0xe0c8a29d) 
  { 
    beep(pinMode 4, 700, 500);  // Make a 700 Hz beep
  }

} // end loop

if (! digitalRead( 0)) 
{  
  digitalWrite( 1, HIGH);    // light up the LED
} 
else 
{
  digitalWrite( 1, LOW);     // otherwise, turn it off
}

if (! digitalRead( 0)) 
{ 
  digitalWrite( 2, HIGH);    // light up the LED
} 
else 
{
  digitalWrite( 2, LOW);     // otherwise, turn it off
}

} // end loop


uint16_t listenForir() 
{ 
  currentpulse = 0;
  while (1) 
  {
    unsigned int highpulse, lowpulse;  // temporary storage timing
    highpulse = lowpulse = 0; // start out with no pulse length

    while (IRpin_PIN & _BV(IRpin)) 
    { 
      highpulse++;
      delayMicroseconds(RESOLUTION);
      if (((highpulse >= MAXPULSE) && (currentpulse != 0)) || currentpulse == NUMPULSES) 
      {
        return currentpulse;
      }
    }
    pulses[currentpulse][0] = highpulse;

    while (! (IRpin_PIN & _BV(IRpin))) 
    { 
      lowpulse++;
      delayMicroseconds(RESOLUTION);
      if (((lowpulse >= MAXPULSE) && (currentpulse != 0)) || currentpulse == NUMPULSES) 
      {
        return currentpulse;
      }
    }
    pulses[currentpulse][1] = lowpulse;
    currentpulse++;
  }

}

// Generate a tone on speakerPin - Trinket/Gemma/ATTiny85 compatible
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
{ 
  int x;
  long delayAmount = (long)(1000000 / frequencyInHertz);
  long loopTime = (long)((timeInMilliseconds * 1000) / (delayAmount * 2));
  for (x = 0; x < loopTime; x++)
  {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(delayAmount);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(delayAmount);
  }


}

I call my self being adventures and wanting to learn arduino programming, I brought three Uno Nano Trinket 5v,any way one of my goals was to try and make a momentary push button activate pins 1,2 output and pins 3 detect tv ir pulses and trigger a buzzer base on the pulse from my tv remote and the buzzer would stop sounding after 15 seconds. Anyone interest in buying a Uno or Nana? My corrections are below still getting errors.

exit status 1
expression cannot be used as function

/*
  Button
  Turns on an LED when a switch connected from #0 to ground is pressed

  This example code is in the public domain.

  To upload to your Gemma or Trinket:
  1) Select the proper board from the Tools->Board Menu
  2) Select USBtinyISP from the Tools->Programmer
  3) Plug in the Gemma/Trinket, make sure you see the green LED lit
  4) For windows, install the USBtiny drivers
  5) Press the button on the Gemma/Trinket - verify you see
     the red LED pulse. This means it is ready to receive data
  6) Click the upload button above within 10 seconds
*/

#define SWITCH 0
#define LED 1
#define LED 2
#define listenForIR 3  // ATTiny85 had Port B pins
#define SPEAKERPIN 4  // Piezo speaker on Trinket/Gemma Pin GPIO #4/D4
#define MAXPULSE    350  // the maximum pulse we'll listen for - 5 milliseconds
#define NUMPULSES    2  // max IR pulse pairs to sample
#define RESOLUTION     2  // // time between IR measurements

// we will store up to 100 pulse pairs (this is -a lot-, reduce if needed)
uint16_t pulses[2][2];   // pair is high and low pulse
uint16_t currentpulse = 0; // index for pulses we're storing
uint32_t irCode = 0;

void setup() {
  irCode = listenForIR
  // initialize the LED pin as an output.
;  pinMode( 1, OUTPUT);
  // initialize the LED pin as an output.
  pinMode( 2, OUTPUT);
  // initialize the SWITCH pin as an input with a pullup
  pinMode(0, INPUT_PULLUP);
  pinMode( 3, INPUT); // Listen to IR receiver on Trinket/Gemma pin D3
  pinMode( 4, OUTPUT); // Output tones on Trinket/Gemma pin D4
}

// the loop routine runs over and over again forever:
void loop() {

  irCode = listenForIR(); // Wait for an IR Code

  // Process the pulses to get our code
  ; for (int i = 0; i < 32; i++) {
    irCode = irCode << 1;
    if ((pulses[i][0] * RESOLUTION) > 0 && (pulses[i][0] * RESOLUTION) < 500) {
      irCode |= 0;
    } else {
      irCode |= 1;
    }
  }
  if (irCode == 0xe0c8A2DD)  {   // "1" on my remote, USE YOUR CODE HERE
    beep( 400, 500);
  }  // Make a 400 Hz beep
  else if (irCode == 0xe0c8a3dc) { // "2", USE YOUR OWN HEX CODE HERE
    beep( 500, 500);
  }  // Make a 500 Hz beep
  else if (irCode == 0xe0c8a41b) { // "3", USE YOUR OWN HEX CODE HERE
    beep( 600, 500);
  } // Make a 600 Hz beep
  else if (irCode == 0xe0c8a29d) { // "4", USE YOUR OWN HEX CODE HERE
    beep(700, 500);  // Make a 700 Hz beep

  }

if (! digitalRead( 0)) {  // if the button is pressed
  digitalWrite( 1, HIGH);    // light up the LED
} else {
  digitalWrite( 1, LOW);     // otherwise, turn it off
}
if (! digitalRead( 0)) {  // if the button is pressed
  digitalWrite( 2, HIGH);    // light up the LED
} else {
  digitalWrite( 2, LOW);     // otherwise, turn it off
}

} // end loop


uint16_t listenForir() {  // IR receive code
  currentpulse = 0;
  while (1) {
    unsigned int highpulse, lowpulse;  // temporary storage timing
    highpulse = lowpulse = 0; // start out with no pulse length

    while (IRpin_PIN & _BV(IRpin)) { // got a high pulse
      highpulse++;
      delayMicroseconds(RESOLUTION);
      if (((highpulse >= MAXPULSE) && (currentpulse != 0)) || currentpulse == NUMPULSES) {
        return currentpulse;
      }
    }
    pulses[currentpulse][0] = highpulse;

    while (! (IRpin_PIN & _BV(IRpin))) { // got a low pulse
      lowpulse++;
      delayMicroseconds(RESOLUTION);
      if (((lowpulse >= MAXPULSE) && (currentpulse != 0)) || currentpulse == NUMPULSES) {
        return currentpulse;
      }
    }
    pulses[currentpulse][1] = lowpulse;
    currentpulse++;
  }

}

// Generate a tone on speakerPin - Trinket/Gemma/ATTiny85 compatible
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
{ // http://web.media.mit.edu/~leah/LilyPad/07_sound_code.html
  int x;
  long delayAmount = (long)(1000000 / frequencyInHertz);
  long loopTime = (long)((timeInMilliseconds * 1000) / (delayAmount * 2));
  for (x = 0; x < loopTime; x++)
  {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(delayAmount);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(delayAmount);
  }


}

terry546:
My corrections are below still getting errors.

It does not seem as if you have carefully read through and considered each of the Replies you have already received.

...R