I changed, but still has an error, do i have to have it do: return _rbits; ?
Did you initialise _rbits?
how?
as in int _rbits?
No, apart from the missing semicolon, that looks more like a declaration, not an initialisation.
The clue is, you did initialise a variable, but it wasn't _rbits.
so, how should i do it?
I'd probably forget about _rbits altogether, and use (I mean really use) "rv".
ok, ill try it.
By the way, the spelling is "indeterminate", not "intermediate".
this is what i have, what should i change?
int AM9111::read(unsigned int address)
{
int rv = 0;
_read() = rv;
_setAddress(address);
return rv;
}
int AM9111::_read()
{
int rv = 0;
digitalWrite(_odp,0);
digitalWrite(_wep,1);
bitWrite(rv,0,digitalRead(_IO[0]));
bitWrite(rv,1,digitalRead(_IO[1]));
bitWrite(rv,2,digitalRead(_IO[2]));
bitWrite(rv,3,digitalRead(_IO[3]));
_read() = rv;
}
_read() = rv;
I thought we'd already decided that this was a no-no?
int AM9111::read(unsigned int address)
{
int rv = 0;
_read() = rv;
_setAddress(address);
return rv;
}
Is that really the correct order of events?
farlepet:
int AM9111::_read()
{
digitalWrite(_odp,0);
digitalWrite(_wep,1);
int rv = 0;
bitWrite(_rbits,0,digitalRead(_IO[0]));
bitWrite(_rbits,1,digitalRead(_IO[1]));
bitWrite(_rbits,2,digitalRead(_IO[2]));
bitWrite(_rbits,3,digitalRead(_IO[3]));
_read() = _rbits;
return rv;
}
Someone's been doing too much Visual Basic.
Instead of:
_read() = _rbits;
return rv;
You want:
return _rbits;
Although I'm not sure what rv does. Did you put it there to make compiler errors go away?
farlepet:
int AM9111::_read()
{
digitalWrite(_odp,0);
digitalWrite(_wep,1);
int rv = 0;
bitWrite(_rbits,0,digitalRead(_IO[0]));
bitWrite(_rbits,1,digitalRead(_IO[1]));
bitWrite(_rbits,2,digitalRead(_IO[2]));
bitWrite(_rbits,3,digitalRead(_IO[3]));
_read() = _rbits;
return rv;
}
I still don't totally get this. Are you trying to change _rbits? In which case why return anything from the function? In that case you want:
void AM9111::_read()
{
digitalWrite(_odp,0);
digitalWrite(_wep,1);
_rbits = 0;
bitWrite(_rbits,0,digitalRead(_IO[0]));
bitWrite(_rbits,1,digitalRead(_IO[1]));
bitWrite(_rbits,2,digitalRead(_IO[2]));
bitWrite(_rbits,3,digitalRead(_IO[3]));
}
But if you want to return stuff, and not affect things such as _rbits (have side-effects) then you actually want:
int AM9111::_read()
{
digitalWrite(_odp,0);
digitalWrite(_wep,1);
int rv = 0;
bitWrite(rv,0,digitalRead(_IO[0]));
bitWrite(rv,1,digitalRead(_IO[1]));
bitWrite(rv,2,digitalRead(_IO[2]));
bitWrite(rv,3,digitalRead(_IO[3]));
return rv;
}
Thanks, ill try it
Yes! It finally compiled! I will upload it tomorrow morning and try it.
Thanks for all your help
And what do you mean by "Someone's been doing too much Visual Basic"?
Well, have you done Visual Basic?
In VB, a function returns its results by assigning to the function name. For example:
function foo (a, b)
foo = a + b ' return result of adding a to b
end function
So when I saw you write:
_read() = _rbits;
I thought you might have thought this was how you returned the function value.
Well i have done a little visual basic, but not much, i did more visual c++.
and i have made an arduino sketch thad did, for example:
boolean yn(int pin)
{
yn = digitalRead(pin);
}
Not one that compiled, right?
sketch_apr23c.cpp: In function 'boolean yn(int)':
sketch_apr23c:2: error: assignment of function 'boolean yn(int)'
sketch_apr23c:2: error: cannot convert 'int' to 'boolean ()(int)' in assignment
hmm, well i know it was something like that, mabey i used if statements or something, i don't know. it was a while ago. Well ill test the lib tomorrow...
Now the _read() always returns 0
here is the code:
#include "AM9111.h"
#include "WProgram.h"
/////////////////////////////////////////
//
// PUBLIC
//
AM9111::AM9111(uint8_t outputDisablePin, uint8_t writeEnablePin)
{
_odp = outputDisablePin;
_wep = writeEnablePin;
}
int AM9111::setIO(uint8_t IO0, uint8_t IO1, uint8_t IO2, uint8_t IO3)
{
_IO[0] = IO0;
_IO[1] = IO1;
_IO[2] = IO2;
_IO[3] = IO3;
}
int AM9111::read(unsigned int address)
{
int rv = 0;
_setAddress(address);
return rv;
}
int AM9111::write(unsigned int address, uint8_t value)
{
int rv = 0;
_setAddress(address);
rv = _write(value);
return rv;
}
/////////////////////////////////////////
//
// PRIVATE
//
void AM9111::_setAddress(unsigned int address)
{
for(int p=0; p<8; p++)
{
pinMode(p, OUTPUT);
digitalWrite(p, bitRead(address, p));
}
}
int AM9111::_read()
{
digitalWrite(_odp,0);
digitalWrite(_wep,1);
int rv = 0;
bitWrite(rv,0,digitalRead(_IO[0]));
bitWrite(rv,1,digitalRead(_IO[1]));
bitWrite(rv,2,digitalRead(_IO[2]));
bitWrite(rv,3,digitalRead(_IO[3]));
return rv;
}
int AM9111::_write(uint8_t value)
{
digitalWrite(_odp,1);
digitalWrite(_wep,0);
int rv = 0;
digitalWrite(_IO[0],bitRead(0,value));
digitalWrite(_IO[1],bitRead(1,value));
digitalWrite(_IO[2],bitRead(2,value));
digitalWrite(_IO[3],bitRead(3,value));
return rv;
}
is it because of the first read statement rv = 0?