Right i have written a vb program which ports a bit-masked integer over to the arduino. i have then coded a program that is meant to read each of the bits from the integer separately and then turn on LEDs. the problem im having is that 1. when i attempt to turn on an LED nothing happens 2. I think it is based around the bitread() but because i don't fully understand arduino i am unsure how to fix it. I have looked at the page on the bitread() function but that hasn't helped.
VB code:
intPortresult = intLightsresult
'ports variable to arduino
SerialPort1.Open()
SerialPort1.Write(intPortresult)
SerialPort1.Close()
Arduino code:
void setup (){
Serial.begin(9600); //begins the loop that connects the the pins being used
for (int thisPin = 2; thisPin < 7; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
using namespace std;
//begins loop that checks the integer ported over from VB
void loop(){
int bitval;
if (Serial.available()){
delay(10);
while (Serial.available() > 0) {
int Portresult = Serial.read();
//begins the loops which chek the bit and turn on/off the LED's
for (int thisPin = 2; thisPin < 8; thisPin++) {
for (int counter = 0; counter < 7; counter++){
int val = bitRead(Serial.read(),counter);
//checks if the bit is 1 or 0 and turns LED on or off
if(val=='1') { digitalWrite(thisPin, HIGH) ; }
else if (val=='0') {digitalWrite(thisPin, LOW);
}
}
}
}
}
}
if anyone could offer some help it would be appreciated.
Also, if the bit is not set to 1 then it is certainly set to 0 so the second test (against '0') is redundant.
Anyway,there is no need to read the bit, set a variable to the value found, then set the LED state to the value. Simply set the LED state to the value of the bit.
Also why 2 for loops, one of which tries to output to pins 2 to 7 when only pins 2 to 6 have been defined as OUTPUTs ?
Surely you only need one for loop and the LED corresponding to the bit read to be turned on/off
I have altered the things you mentioned, thank you for noticing the pin error i had made iv fixed that now and i have also got rid of a loop, yet it still isn't working so i think it might be due to what you said about a the bitread function might not work with a byte. So i wondered if you knew of a different method of getting a bit from and integer? I have Googled it but the stuff i found wasn't helpful.
TheSpook:
I have altered the things you mentioned, thank you for noticing the pin error i had made iv fixed that now and i have also got rid of a loop, yet it still isn't working so i think it might be due to what you said about a the bitread function might not work with a byte. So i wondered if you knew of a different method of getting a bit from and integer? I have Googled it but the stuff i found wasn't helpful.
bitRead() works fine with a byte, or an int for that matter.
What I was alluding to was that a bit is either 0 or 1, not '0' or '1'
Hint - change the way that you are testing the value of the bit