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?
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.
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.