Pages: [1] 2 3   Go Down
Author Topic: void value not ignored as it ought to be  (Read 2411 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

getting the error of "void value not ignored as it ought to be"
the culprit is this line of code in the main cpp:
Code:
aValue = letters[0].setCurrentReading(calSenseA);

the code:

Code:
#include <CapacitiveSensor.h>
#include "Letter.h"

const byte capSenseSend = 2;

const byte numberOfSensors = 2;
Letter letters[numberOfSensors]; // a-z


const byte sensorPin[] = { 22, 23 };
const byte ledPin[] = { 8 };

int allValues[numberOfSensors];
int aValue, bValue;

CapacitiveSensor   cs_a = CapacitiveSensor(capSenseSend,sensorPin[0]);
CapacitiveSensor   cs_b = CapacitiveSensor(capSenseSend,sensorPin[1]);

void setup(){
   // setup LEDs
   for(int i = 0; i < numberOfSensors; i++){
     pinMode(ledPin[i],OUTPUT);
     digitalWrite(ledPin[i], LOW); 
   }

   cs_a.set_CS_AutocaL_Millis(0xFFFFFFFF);
   delay(100);
   Serial.begin(19200);
   delay(100);
   
   letters[0].setSampleAmount(5);
   letters[1].setSampleAmount(5);
}

void loop(){
    int calSenseA = cs_a.capacitiveSensor(50);
    aValue = letters[0].setCurrentReading(calSenseA);
   
   
    bValue = (int)letters[1].setCurrentReading((int)cs_b.capacitiveSensor(50));
   
    Serial.print(" A : "); Serial.print(aValue); Serial.print(" , "); Serial.println(bValue);
   
    int allValues[] = { aValue, bValue };
    checkIfisOn(allValues);
    delay(100);
}

void fadeLedIn(int pinNumber){
  for(int j = 0; j <= 255; j+=10){
    analogWrite(pinNumber,j);
    delay(10);
  } 
}

void fadeLedOut(int pinNumber){
  for(int j = 255; j >= 0; j-=10){
    analogWrite(pinNumber,j);
    delay(10);
  } 
  digitalWrite(pinNumber, LOW);
}

void sendSerialData(int buttonTouched){
  delay(10);
  Serial.print("Data,");
  delay(10);
  Serial.print(buttonTouched);
  delay(10);
  Serial.print(",");
  Serial.print("sensors : ");
  Serial.print(allValues[0]); Serial.print(" , "); Serial.print(allValues[1]);
  delay(10);
  Serial.print("\n");
  delay(10);
}

header file:
Code:
//#include "Arduino.h"

#ifndef LETTER_H
#define LETTER_H

class Letter {
  public:
    Letter();
    double getAverage(int sample);
    void setSampleAmount(int changeSampleTo);
    void setLastReading(int theLastReading);
    void setCurrentReading(int theCurrentReading);
    void checkIfisOn(int curentSensorValue[]);
    int getCurrentReading();
    int getLastReading();
    int aValue;
    int lastReading;
    int currentReading;
  private:
   
    int _sampleAmount;
    double _tempAverage,
           _capacitiveAverage;
    double _runningAverage[100];
};

#endif

definitions:
Code:
#include "Letter.h" // "" for files within same folder as sketch...?

Letter::Letter() {
  _sampleAmount = 5;
  _tempAverage = 0;
  _capacitiveAverage = 0; 
  lastReading = 0;
  currentReading = 0;
  //long _runningAverage[_sampleAmount];
};

void Letter::setSampleAmount(int changeSampleTo){
  _sampleAmount = changeSampleTo;
};

void Letter::setLastReading(int theLastReading){
  lastReading = theLastReading;
}

void Letter::setCurrentReading(int theCurrentReading){
  currentReading = theCurrentReading;
}

int Letter::getLastReading(){
  return lastReading;
}

int Letter::getCurrentReading(){
  return currentReading
}

double Letter::getAverage(int sample) {
  for(int i = 0; i < _sampleAmount; i++){
    _runningAverage[i] = sample;
  }
  // reset _tempAverage
  _tempAverage = 0;
  // add all the samples up
  for(int i = 0; i < _sampleAmount; i++){
    _tempAverage += _runningAverage[i];
  }
  // divide by number of samples to get average
  _capacitiveAverage =  _tempAverage/_sampleAmount;
  return _capacitiveAverage;
};


void Letter::checkIfisOn(){
  boolean firstTime = true;
  int firstRelease = 0;
  const int threshold = 55;
  int differenceReading = curentSensorValue-lastReading;
  if(differenceReading > threshold && firstTime == true){
    sendSerialData(i);
    fadeLedIn(ledPin[i]);
  if((currentReading-lastReading) < threshold){
    digitalWrite(ledPin[i], LOW);
    fadeLedOut(ledPin[i]);}
    lastReading[i] = currentReading[i];
    firstTime = false;
  }
}



I am a little confused because the method isn't returning a value, it is simply setting a value so not sure why I would get this error.
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 651
Posts: 50841
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
aValue = letters[0].setCurrentReading(calSenseA);
The function, as you pointed out, does not return a value. Instead of accepting that, and ignoring it, you try to assign it to a variable. Why?
Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
aValue = letters[0].setCurrentReading(calSenseA);
The function, as you pointed out, does not return a value. Instead of accepting that, and ignoring it, you try to assign it to a variable. Why?

trying to set the variable currentReading in the class to that capacitive value I am passing. maybe I am going about this wrong? In JS I think it would be something more like Letter.currentReading = cs_a.capacitiveSensor(50);

I might be going about it wrong in C++ though, I thought you can't directly set variables like that and have to use a method to do so.
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 651
Posts: 50841
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The function name, including set, should give you a clue that it is NOT reading anything. The class has a function with a similar name but with get instead of set. Get methods read. Why are you not using getCurrentReading() to get the current reading?

Edit: I reread what you are trying to do. I don't understand it. You can use setCurrentReading() to set a value, but it doesn't return anything. You can use getCurrentReading() to get the value, and it does return something.

Only when a function returns a value can the value be stored in a variable or passed to another function.
« Last Edit: April 05, 2013, 08:53:31 am by PaulS » Logged

New Jersey
Online Online
Faraday Member
**
Karma: 72
Posts: 3758
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Normally, when using get and set functions, the setter doesn't return anything - there's no point - you already know what the value is because you're passing it in. You could change the setter to return the value it's passed and then use it to populate aValue in the manner you're trying. However, why bother:
Code:
    int calSenseA = cs_a.capacitiveSensor(50);
    aValue = letters[0].setCurrentReading(calSenseA);
You already have the value in another variable - calSenseA . Use that in your serial print.
Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The function name, including set, should give you a clue that it is NOT reading anything. The class has a function with a similar name but with get instead of set. Get methods read. Why are you not using getCurrentReading() to get the current reading?

Edit: I reread what you are trying to do. I don't understand it. You can use setCurrentReading() to set a value, but it doesn't return anything. You can use getCurrentReading() to get the value, and it does return something.

Only when a function returns a value can the value be stored in a variable or passed to another function.

that is what I am saying, the setCurrentReading should set the value, but it does not. that is the line that throws the error, the setCurrentReading is the method that is throwing the error in the main ino file.

I guess I just need to know how to set currentReading in the class using calSenseA variable in the main ino file, not sure how to go about that. if that makes sense? apparently how I am doing it now isnt working. I thought that public variables can be set in this way.
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Normally, when using get and set functions, the setter doesn't return anything - there's no point - you already know what the value is because you're passing it in. You could change the setter to return the value it's passed and then use it to populate aValue in the manner you're trying. However, why bother:
Code:
    int calSenseA = cs_a.capacitiveSensor(50);
    aValue = letters[0].setCurrentReading(calSenseA);
You already have the value in another variable - calSenseA . Use that in your serial print.

I need to set the variable, not print it. It needs to be stored in the class is all. eventually I will read it, but I am getting the error on this line implying I am doing something wrong here.
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

New Jersey
Online Online
Faraday Member
**
Karma: 72
Posts: 3758
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Then all you need is:
Code:
letters[0].setCurrentReading(calSenseA);
Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Then all you need is:
Code:
letters[0].setCurrentReading(calSenseA);

oh wow...duh. yeah you're right. thanks. that worked smiley
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

seem to have a new problem :/ this one really is stumping me even after reading about how to use these, since its using a library it makes it confusing. Seems like capacitive sensor library makes you set the objects value to a function in the library, so I am not sure how to make this work.

I need to populate an array of objects I would expect it to work how I have it here in the setup function:

Code:
  CapacitiveSensor cs_sensor[numberOfSensors];
  
   for(int i = 0; i < numberOfSensors; i++){
     cs_sensor[i] = CapacitiveSensor(capSenseSend,sensorPin[i]);
   }

so I can later set the current reading using this in the loop:

Code:
void setSensorValue(){
  for(int i = 0; i < numberOfSensors; i++){
    letters[i].setCurrentReading(cs_sensor[i].capacitiveSensor(50));  
  }
}

but no luck.
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 651
Posts: 50841
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but no luck.
Programming is not a matter of luck. It is a matter of skill.

What does "but no luck" mean? Does the code not compile? Does it compile but not do what you want?
Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but no luck.
Programming is not a matter of luck. It is a matter of skill.

What does "but no luck" mean? Does the code not compile? Does it compile but not do what you want?

haha, fair enough. apparently skill I am lacking smiley-wink it is giving me the error of "no matching function for call to 'capacitiveSensor::capactiveSensor()"

for the line:

Code:
CapacitiveSensor cs_sensor[numberOfSensors];
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

here is all the updated code if it helps, main ino file:

Code:
#include <CapacitiveSensor.h>
#include "Letter.h"

const byte capSenseSend = 2;

const byte numberOfSensors = 2;
Letter letters[numberOfSensors]; // a-z


const byte sensorPin[] = { 22, 23 };
const byte ledPin[] = { 8 };

int allValues[numberOfSensors];

void setup(){
   Serial.begin(19200);
   delay(100);
  
   CapacitiveSensor cs_sensor[numberOfSensors];
  
   for(int i = 0; i < numberOfSensors; i++){
     cs_sensor[i] = CapacitiveSensor(capSenseSend,sensorPin[i]);
   }
  
   // setup LEDs
   for(int i = 0; i < numberOfSensors; i++){
     pinMode(ledPin[i],OUTPUT);
     digitalWrite(ledPin[i], LOW);  
   }
   //cs_a.set_CS_AutocaL_Millis(0xFFFFFFFF);

   setSampleSize();  
}

void loop(){  
    setSensorValue();
    checkIfButtonPressed();
    delay(100);
}

void setSampleSize(){
  for(int i = 0; i < numberOfSensors; i++){
    letters[i].setSampleAmount(5);
  }
}

void setSensorValue(){
  for(int i = 0; i < numberOfSensors; i++){
    letters[i].setCurrentReading(cs_sensor[i].capacitiveSensor(50));  
  }
}

void checkIfButtonPressed(){
  for(int i = 0; i < numberOfSensors; i++){
    letters[i].checkIfIsOn(cs_sensor[i]);
  }
}

void fadeLedIn(int pinNumber){
  for(int j = 0; j <= 255; j+=10){
    analogWrite(pinNumber,j);
    delay(10);
  }  
}

void fadeLedOut(int pinNumber){
  for(int j = 255; j >= 0; j-=10){
    analogWrite(pinNumber,j);
    delay(10);
  }  
  digitalWrite(pinNumber, LOW);
}

void sendSerialData(int buttonTouched){
  delay(10); Serial.print("Data,");
  delay(10); Serial.print(buttonTouched);
  delay(10); Serial.print(",");
  Serial.print("sensors : ");
  Serial.print(allValues[0]); Serial.print(" , "); Serial.print(allValues[1]); delay(10);
  Serial.print("\n");
  delay(10);
}

Letter.h
Code:
#ifndef LETTER_H
#define LETTER_H

class Letter {
  public:
    Letter();
    double getAverage(int sample);
    void setSampleAmount(int changeSampleTo);
    void setLastReading(int theLastReading);
    void setCurrentReading(int theCurrentReading);
    void checkIfisOn(int curentSensorValue[]);
    int getCurrentReading();
    int getLastReading();
    int aValue;
    int lastReading;
    int currentReading;
  private:
    
    int _sampleAmount;
    double _tempAverage,
           _capacitiveAverage;
    double _runningAverage[100];
};

#endif

Letter.ino
Code:
#include "Letter.h" // "" for files within same folder as sketch...?

Letter::Letter() {
  _sampleAmount = 5;
  _tempAverage = 0;
  _capacitiveAverage = 0;  
  lastReading = 0;
  currentReading = 0;
  //long _runningAverage[_sampleAmount];
};

void Letter::setSampleAmount(int changeSampleTo){
  _sampleAmount = changeSampleTo;
};

void Letter::setLastReading(int theLastReading){
  lastReading = theLastReading;
}

void Letter::setCurrentReading(int theCurrentReading){
  currentReading = theCurrentReading;
}

int Letter::getLastReading(){
  return lastReading;
}

int Letter::getCurrentReading(){
  return currentReading;
}

double Letter::getAverage(int sample) {
  for(int i = 0; i < _sampleAmount; i++){
    _runningAverage[i] = sample;
  }
  // reset _tempAverage
  _tempAverage = 0;
  // add all the samples up
  for(int i = 0; i < _sampleAmount; i++){
    _tempAverage += _runningAverage[i];
  }
  // divide by number of samples to get average
  _capacitiveAverage =  _tempAverage/_sampleAmount;
  return _capacitiveAverage;
}


void Letter::checkIfisOn(){
  boolean firstTime = true;
  int firstRelease = 0;
  const int threshold = 55;
  int differenceReading = curentSensorValue-lastReading;
  if(differenceReading > threshold && firstTime == true){
    sendSerialData(i);
    fadeLedIn(ledPin[i]);
  if((currentReading-lastReading) < threshold){
    digitalWrite(ledPin[i], LOW);
    fadeLedOut(ledPin[i]);}
    lastReading[i] = currentReading[i];
    firstTime = false;
  }
}
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 651
Posts: 50841
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
it is giving me the error of "no matching function for call to 'capacitiveSensor::capactiveSensor()"
Which means that there is no no-argument constructor, which is what is required to create an array of objects the way that you are trying to.

You could create an array of objects using the appropriate constructor:
Code:
CapacitiveSensor cs_sensor[] =
{
   CapacitiveSensor(capSenseSend,sensorPin[0]);
   CapacitiveSensor(capSenseSend,sensorPin[1]);
   CapacitiveSensor(capSenseSend,sensorPin[2]);
   CapacitiveSensor(capSenseSend,sensorPin[3]);
   CapacitiveSensor(capSenseSend,sensorPin[4]);
}
I don't know how many cap sensors you are trying to create, so you need to make sure that the number of entries is correct.

Or, you can use pointers and the new operator.
Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
it is giving me the error of "no matching function for call to 'capacitiveSensor::capactiveSensor()"
Which means that there is no no-argument constructor, which is what is required to create an array of objects the way that you are trying to.

You could create an array of objects using the appropriate constructor:
Code:
CapacitiveSensor cs_sensor[] =
{
   CapacitiveSensor(capSenseSend,sensorPin[0]);
   CapacitiveSensor(capSenseSend,sensorPin[1]);
   CapacitiveSensor(capSenseSend,sensorPin[2]);
   CapacitiveSensor(capSenseSend,sensorPin[3]);
   CapacitiveSensor(capSenseSend,sensorPin[4]);
}
I don't know how many cap sensors you are trying to create, so you need to make sure that the number of entries is correct.

Or, you can use pointers and the new operator.

ah, ok. I would prefer to use pointers since speed is an issue and it has been on to do list. I have never used pointers before though. Is there anything in particular in this context that I should try to look up to read more on it? I am not even sure what to search. "using pointers to populate objects...?"

number of sensors is set by variable: numberOfSensors
I would prefer to use a loop so I can change this as I go, I need to prototype it as I go, so I will start with 4 sensors then 8 then 20, etc.
« Last Edit: April 05, 2013, 06:33:26 pm by hilukasz » Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Pages: [1] 2 3   Go Up
Jump to: