arduino if statement giving error - c++

I am working on some arduino code and my program keeps on giving me this error,
ISO C++ forbids comparison between pointer and integer [-fpermissive]
I've tried searching on the internet to solve this issue but, either the solution is incorrect, or irrelevant. here is where the arduino software is saying the problem is,
if((millis - incLastDebounce) > debounceDelay) {
and if you need the rest of the code here it is,
#include <LiquidCrystal.h>
int freq = 0;
int change = 0;
const int incPin = 3;
const int setPin = 2;
int incButtonState;
int setButtonState;
int incPreviousState;
int setPreviousState;
int incLastDebounce;
int setLastDebounce;
const int debounceDelay = 50;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// put your setup code here, to run once:
lcd.begin(16, 2);
pinMode(setPin, INPUT);
pinMode(incPin, INPUT);
lcd.print("Frequency: " + freq);
}
void loop() {
// put your main code here, to run repeatedly:
int incReading = digitalRead(incPin);
int setReading = digitalRead(setPin);
if(setReading != setPreviousState) {
setLastDebounce = millis();
}
if(incReading != incPreviousState) {
incLastDebounce = millis();
}
if((millis - setLastDebounce) > debounceDelay) {
if(setReading != setButtonState) {
setButtonState = setReading;
}
if(setButtonState == HIGH) {
//Okay so here you will do your set lcd voodoo
}
}
if((millis - incLastDebounce) > debounceDelay) {
if(incReading != buttonState) {
incButtonState = incReading;
}
if(buttonState == HIGH) {
// here you can put the lcd code
change = change + 500;
if(change == 10500){
change = 0;
}
}
}
incPreviousState = incReading;
setPreviousState = setReading;
}
hopefully you can find the problem and help.

Looks like you're missing parentheses after millis, so instead of calling the function, you're trying to do arithmetic with its memory address.
This will probably work better:
if ((millis() - incLastDebounce) > debounceDelay) {

Related

C++ (Arduino), Can't update object when accessing it as attribute of another object

I have recently bought an Arduino which uses C++ to code on. I am familiar with Java and as C++ allows OO programming I didn't think it would have been that difficult. But...
CODE:
// LEDCLOCK CLASS /////////////////////////////////////////////////////////////////////////
class LedClock{
private:
int hours;
int minutes;
int seconds;
static const long secondInterval = 1000;
unsigned long previousMilliseconds;
unsigned long currentMilliseconds;
public:
LedClock(){
hours = 0;
minutes = 0;
seconds = 0;
previousMilliseconds = 0;
currentMilliseconds = 0;
};
int getHours(){
return hours;
};
int getMinutes(){
return minutes;
};
int getSeconds(){
return seconds;
};
long getSecondInterval(){
return secondInterval;
};
unsigned long getPreviousMilliseconds(){
return previousMilliseconds;
};
void setHours(int h){
if(h < 24 && h >= 0){
hours = h;
}else{
hours = 0;
}
};
void setMinutes(int m){
if(m < 60 && m > 0){
minutes = m;
}else{
minutes = 0;
}
};
void setSeconds(int s){
if(s < 60 && s > 0){
seconds = s;
}else{
seconds = 0;
}
};
void setPreviousMilliseconds(unsigned long ms){
previousMilliseconds = ms;
};
void increaseOneHour(){
// as there is no day counter to increment atm this if-else statement is a bit useless.
// setHour(getHours() + 1) would have sufficed here with the current setter
if(getHours()==23){
setHours(0);
}else{
setHours(getHours() + 1);
}
};
void increaseOneMinute(){
if(getMinutes() == 59){
increaseOneHour();
setMinutes(0);
}else{
setMinutes(getMinutes() + 1);
}
};
void increaseOneSecond(){
if(getSeconds() == 59){
increaseOneMinute();
setSeconds(0);
}else{
setSeconds(getSeconds() + 1);
}
};
void tick(){
currentMilliseconds = millis();
if(currentMilliseconds - getPreviousMilliseconds() >= getSecondInterval()){
setPreviousMilliseconds(currentMilliseconds);
increaseOneSecond();
}
};
};
// LEDCLOCKCONTROLLER CLASS ////////////////////////////////////////////////////////////////
class LedClockController{
private:
LedClock ledClock;
int mode = 2;
public:
LedClockController(LedClock lc){
ledClock = lc;
};
LedClock getLedClock(){
return ledClock;
};
int getMode(){
return mode;
};
void setMode(int newMode){
mode = newMode;
};
};
// ARDUINO CODE /////////////////////////////////////////////////////////////////////////
LedClock lc = LedClock();
LedClockController lcc(lc);
void setup() {
Serial.begin(9600); //Begin serializer to print out value
}
void loop() {
//doesn't give me updated values
if(lcc.getLedClock().getPreviousMilliseconds()<63000){
Serial.println(lcc.getLedClock().getSeconds());
lcc.getLedClock().tick();
}
//does give me updated values
//commented out for now
/*
if(lc.getPreviousMilliseconds()<63000){
Serial.println(lc.getSeconds());
lc.tick();
}
*/
}
Q1:
I have difficulties to update attributes of my LedClock attribute in my Controller class.
When I target the LedClock by itself, everything runs fine but when I would update it via the Controller, then it wouldn't. In short, when getting the seconds in the first case, I could see the increment in values in the output. When I did the same thing but via the controller, the seconds stayed 0.
So I am missing something vital here. Can someone help/explain what I am missing?
Q2: Ideally I would want to create the LedClock object inside the constructor but didn't seem to find how to.
I tried things that could make sense but with the issues I have been having, I was holding off on this:
LedClockController lcc(LedClock lc());
LedClockController lcc(LedClock);
//would make sense to me, I noticed C++ doesn't use the 'new' keyword so have no idea how to do that then
LedClockController lcc(LedClock());
All of those ran into compilation issues so probably another important C++ thing that I haven't taken into account.
P.S. I have been noticing that there are some different views on getter and setters (accessing the attributes directly vs actual functions). I have been using the method I am used to (and were mentioned on W3schools) because I will rely on setting logic in 1 place.

Using an array in the Arduino ide

Hi i'm trying to use an array to make a code for the arduino to play music using a piezo speaker, however it's having trouble receiving data from the array. Please help, thanks!
int y = 0;
int x = 1600;
int song[8]={653,4,494,8,523,8,578,4};
int dur;
int note;
void setup() {
Serial.begin(9600);
}
void loop() {
int n = 0;
while (n<2){
Serial.print(y);
if (n=0){
note = song[y];
Serial.print(song[y]) ;
}
else if(n=1){
dur = song[y+1];
}
n++;
}
Serial.print(note);
tone(11,note);
delay(x/dur);
y+=2;
if (y>7){
y = 0;
}
}
You're not using the proper operator in your if statements. Change your if statement to this...
if (n==0){
note = song[y];
Serial.print(song[y]) ;
}
else if(n==1){
dur = song[y+1];
}

invalid use of void expression, what how? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Don't know why I'm getting this.
The program is a badly coded whack a mole game made to run on a Arduino Uno.
My IED is Arduino IED version 1.6.12
Yes, there's a lot more to it but chose to leave it out, if you want to see all the code just ask.
Error
Arduino: 1.6.12 (Windows 7), Board: "Arduino/Genuino Uno"
C:\Users\Strings\Documents\Arduino\Too_Many_Moles\Too_Many_Moles.ino: In function 'void winOrLost()':
Too_Many_Moles:146: error: invalid use of void expression
timelyCall(current_millis, toggleBuzer(), 1000);
^
C:\Users\Strings\Documents\Arduino\Too_Many_Moles\Too_Many_Moles.ino: In function 'void loop()':
Too_Many_Moles:191: error: invalid use of void expression
timelyCall(current_millis, popAMole(), time_interval);
^
exit status 1
invalid use of void expression
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Main.ino
const int buzer = A0, on_off_pin = A1;
unsigned long current_millis;
const int time_interval = 20;
int currentMole = 0;
int points = 0, lives = 3, level = 0, odds_of_pop_up = 6;
mole moles[6] = {
mole( 2, 8 ),
mole( 3, 9 ),
mole( 4, 10 ),
mole( 5, 11 ),
mole( 6, 12 ),
mole( 7, 13 )
};
int number_of_mole = sizeof(moles) / sizeof(*moles);
void timelyCall(unsigned long currentMillis, void (&f)(), int ms) {
if (millis() - currentMillis > ms) {
f();
currentMillis = millis();
}
}
void popAMole() {
if (moles[currentMole].isReadyToPop()) {
moles[currentMole].popUp(odds_of_pop_up);
}
if (currentMole + 1 <= number_of_mole) {
currentMole = 0;
}
else {
currentMole++;
}
}
mole.h
#ifndef mole_h
#define mole_h
#include "Arduino.h"
class mole
{
public:
mole(int p_out, int p_in);
void resetMole();
void popUp(int odds);
boolean moleGetHit();
boolean isReadyToPop();
void setUpTimer(int up_timer);
int getUpTimer();
void decrementUpTimer();
void setRecoveryTimer(int recovery_timer);
int getRecoveryTimer();
void decrementRecoveryTimer();
void addRecoveryTimer(int add_time);
boolean getSouldBeUp();
int getOddsOfPopinh();
void setOddsOfPopinh(int odds);
int getPinIn();
private:
boolean _sould_be_up = false;
int _up_timer = 0;
int _recovery_timer = 0;
int _pin_out;
int _pin_in;
int _odds_of_poping = 10;
};
#endif
mole.cpp
boolean mole::isReadyToPop() {
if (_recovery_timer == 0 && _sould_be_up = false) {
return true;
}
else {
return false;
}
}
void mole::popUp(int odds) {
if (rand() % odds + 1 == 1) {
digitalWrite(_pin_out, HIGH)
_sould_be_up = true;
}
}
You are calling the functions not passing the addresses to timelyCall!
timelyCall(current_millis, popAMole(), time_interval);
timelyCall(current_millis, toggleBuzer(), 1000);
Should be:
timelyCall(current_millis, popAMole, time_interval);
timelyCall(current_millis, toggleBuzer, 1000);
Notice no brackets on popAMole & toggleBuzer.

Trash on Arduino serial monitor. How to fix it?

i'm working with I2C eeprom and Arduino. For now i tried to create simple keyboard that will start specific functions. I want to write to serial monitor potentiometer value, but i get trash instead it. How to fix it?
My functions:
int *readPot() ///read potentiometer value
{
int tempValue = analogRead(A0);
int *potValue = &tempValue;
return potValue;
}
void keyboardProcess() ///process keyboard input
{
int *potValue = readPot();
for(int i = 0; i < 2; i++)
{
btnReadings[i] = digitalRead(keysPins[i]);
}
if(btnReadings[0] == HIGH)
{
Serial.println("Potentiometer reading:" + *potValue);
}
}
One obvious problem is that you are returning the address to a local variable:
int *readPot() ///read potentiometer value
{
int tempValue = analogRead(A0);
int *potValue = &tempValue;
return potValue;
}
Here, the returned pointer points to the address of tempValue. This ceases to be valid once the function returns. Just use an int:
int readPot() ///read potentiometer value
{
return analogRead(A0);
}
Next, I doubt that this is a valid argument to Serial.println:
Serial.println("Potentiometer reading:" + *potValue);
but this should work:
int potValue = readPot();
Serial.print("Potentiometer reading: ");
Serial.println(potValue);

Function/method definition error in Arduion/C++. Or syntax error?

I'm translating a program I wrote in Ruby to the Arduino/C++. On my first attempt to define a function/method I keep getting the following error: "BreadboardTestFunctions:41: error: a function-definition is not allowed here before '{' token
BreadboardTestFunctions:91: error: expected `}' at end of input"
Hard to understand because the function-definition must be followed by a pair of brackets. This could reflect a syntax error as it has persisted as I've corrected a score of errors in the function as I've tried to solve this problem. But it looks OK to me now.
I'm generating an output after matrix multiplication in the section beginning "// routine to multiply: behavior=brain * stimulus'". Although the problematic method, "mody" (at line 40) is only called once now, once I get it to work all the outputs will call it.
The code:
/* BREADBOARD
Implement program on Arduino + breadboard
*/
// constants
int foodPin = 2; // to provide food
int painPin = 3; // to punish
int ucsPin = 4; // the UCS
int csPin = 5; // the CS
int lightPin = 6; // turn the "light" stim on/off
int thresh = 700;
// variables
int buttonState = 0; // variable for reading the pushbutton status
boolean lighton = false;
unsigned short int energy = 10000;
int stimulus[11] = {0,0,0,0,0,0,0,0,0,0,0};
int brain[7][11] = { {0,0,0,0,99,0,0,0,0,1,0},
{0,0,0,0,0,99,0,0,0,1,0},
{0,0,0,0,0,0,99,0,0,1,0},
{90,0,0,0,0,0,0,1,-1,1,-99},
{0,90,0,0,0,0,0,1,-1,1,1},
{0,0,90,0,0,0,0,1,-1,1,1},
{0,0,0,90,0,0,0,1,-1,1,1} };
int behavior[7] = {0,0,0,0,0,0,0};
void setup() {
// initialize the pushbutton pins as an input:
pinMode(foodPin, INPUT);
pinMode(painPin, INPUT);
pinMode(ucsPin, INPUT);
pinMode(csPin, INPUT);
pinMode(lightPin, INPUT);
Serial.begin(9600);
int ix=0;
// define behavioral methods
void mody (int ix, int brain[], int stimulus[])
{ int psp=20;
int j;
for(j=7;j<11;j++)
{if (brain[ix][j] > 0) brain[ix][j]+= stimulus[j] * (99-brain[ix][j])/psp;
if (brain[ix][j] < 0) brain[ix][j]+= -1*(stimulus[j] * abs(99-brain[ix][j])/psp);}
return;}
} // end void setup
void loop(){
// decay stimulus vector. do this and check inputs for ALL stimulii later
int k;
for(k=0;k<11;k++)
{if (stimulus[k] > 1) stimulus[k]-=2; else stimulus[k]=0;}
//check inputs
buttonState = digitalRead(foodPin);
if (buttonState == HIGH) stimulus[4] = 9;
buttonState = digitalRead(painPin);
if (buttonState == HIGH) stimulus[5] = 9;
buttonState = digitalRead(ucsPin);
if (buttonState == HIGH) stimulus[6] = 9;
buttonState = digitalRead(lightPin);
if (buttonState == HIGH) {stimulus[7] = 9; stimulus[8] = 9;lighton = true;}
else {stimulus[7] = 0; stimulus[8] = 0;lighton = false;}
buttonState = digitalRead(ucsPin);
if (buttonState == HIGH) stimulus[6] = 9;
// routine to multiply: behavior=brain * stimulus'
int i, j;
for(i=0;i<7;i++)
{ behavior[i]=0;
for (j=0;j<11;j++)
{behavior[i]= behavior[i]+stimulus[j]*brain[i][j]; }
} // end for i
if (behavior[0] > thresh) {Serial.println("Positive Fixer");}
if (behavior[1] > thresh) {Serial.println("Negative Fixer");}
if (behavior[2] > thresh) {Serial.println("UCR"); mody (2, brain[], stimulus[]);}
if (behavior[3] > thresh) {Serial.println("Operant one");}
if (behavior[4] > thresh) {Serial.println("Operant two");}
if (behavior[5] > thresh) {Serial.println("Operant three");}
if (behavior[6] > thresh) {Serial.println("Operant four");}
// generate random operant
if (random(energy) < 10) stimulus[random(4)]= 9 + random(3);
energy --;
Serial.println(energy);
} // end void loop
You may not define one function inside another function. It is what you are trying to do in the following code snippet
void setup() {
// initialize the pushbutton pins as an input:
pinMode(foodPin, INPUT);
pinMode(painPin, INPUT);
pinMode(ucsPin, INPUT);
pinMode(csPin, INPUT);
pinMode(lightPin, INPUT);
Serial.begin(9600);
int ix=0;
// define behavioral methods
void mody (int ix, int brain[], int stimulus[])
You are trying to define function mody inside function setup.