Expression cannot be used as a function error

I'm writing a program using the Arduino Pulsesensor and I keep getting the "expression cannot be used as a function" error. This is my code:

#include <Arduino.h>
#include <avr/interrupt.h>
#include <stdint.h>
//  Variables
int PulseSensorPurplePin = 0;        // Pulse Sensor PURPLE WIRE connected to ANALOG PIN 0
int LED = LED_BUILTIN;   //  The on-board Arduion LED


int Signal;                // holds the incoming raw data. Signal value can range from 0-1024
int Threshold = 510;       // Determine which Signal to "count as a beat", and which to ingore.

volatile int bPM;
volatile int beatCount = 0;
volatile int timesThrough = 0;

ISR(TIMER0_COMPA_vect)
{
  timesThrough += 1;

  if (timesThrough == 5)
  {
    bPM = (beatCount * 12);
    timesThrough = 0;
    beatCount = 0;
  }

  TCNT0 = 0;
  OCR0A = 15625;
}
// The SetUp Function:
void setup() {
  pinMode(LED,OUTPUT);         // pin that will blink to your heartbeat!
  Serial.begin(9600);         // Set's up Serial Communication at certain speed.
  TCCR0A |= (1 << COM0A0); // SET TO TOGGLE OC0A ON COMPARE MATCH
  TCCR0B |= (1 << CS02 & 1 << CS00) // SET TO PRESCALER 1024
  OCR0A = 15625;
  TIMSK0 |= (1 << OCIE0A);
  TCNT0 = 0;

}

// The Main Loop Function
void loop() {

  Signal = analogRead(PulseSensorPurplePin);  // Read the PulseSensor's value.
                                              // Assign this value to the "Signal" variable.

   Serial.println(Signal);                    // Send the Signal value to Serial Plotter.


   if(Signal > Threshold){                          // If the signal is above "550", then "turn-on" Arduino's on-Board LED.
     digitalWrite(LED,HIGH);
     beatCount += 1;
   } else {
     digitalWrite(LED,LOW);                //  Else, the sigal must be below "550", so "turn-off" this LED.
   }


delay(20);


}

This is the error message I've been getting:
OCR0A = 15625;
^

exit status 1

Compilation error: expression cannot be used as a function

The code isn't complete but that's not really the point. I'm not sure why this is happening as I've called this macro before in a similar manner and didn't get an error. Does anyone know why this is happening?

You need a semicolon here.

Also, that expression doesn't look right, just be sure. Is it the logical AND of two distinct bits?

a7

After running a tools → autoformat, part of setup looks like

  TCCR0B |= (1 << CS02 & 1 << CS00)  // SET TO PRESCALER 1024
    OCR0A = 15625;

The fact that the second line is indented indicates that the error is on the first line; your missing a semi-colon. Fixing that will fix the error.

There is a warning as well on the below line (you might have to set "compiler warnings" in file → preferences to "All" to see it).

C:\Users\Wim\AppData\Local\Temp\.arduinoIDE-unsaved20231024-12296-1azpaki.pyld\sketch_nov24a\sketch_nov24a.ino:37:11: warning: large integer implicitly truncated to unsigned type [-Woverflow]
   OCR0A = 15625;
           ^~~~~

OCR0A is an 8-bit register, 15625 does not fit in 8 bits. In this case it's a warning that can't be ignored if you want de code to behave as you expect.

It's a bitwise AND. Logical is &&

THX, haha, that's what I meant to type. If it is the bitwise AND of two distinct bits it will be zero.

The logical AND of two distinct bits would be… true.

So either way it seems wrong, w/o even knowing anything else.

a7

t only does it need to be OR, but it also needs some parenthesis.

If you mean it needs parentheses like

  TCCR0B |= (1 << CS02) | (1 << CS00);

I might agree. You need them to keep people from needing to wonder if the writer dared to write it without them.

Bit shift operators have a higher precedence than bitwise OR, so as far as the code is concerned, there is no need for those extra parentheses.

  TCCR0B |= 1 << CS02 | 1 << CS00;

Despite my lifetime habit of using as little ink as possible, this is probably a place where I would use parentheses, and maybe even not be quite sure I did not need to.

a7

1 Like

Good catch it was just missing a semicolon I don't know how I missed that lol. Appreciate you guys.

Ok thank you, was programming without glasses on and must've missed it. It's not a logical and I just wanted to set both bits on at the same time.

Ok I didn't see that. If I use Timer 1 instead of Timer 0 would 15625 work considering that Timer 1 is a 16-bit timer?

To my knowledge, yes.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.