what do these symbols mean when together << ?
Shift left.
Operators you can program in C or js for years in and never use, but can't program in embedded platforms for more than a few days without.
Get used to them, they're your friends: << >> | & ^ and ~
looking at it for the first time it seems complicated but im sure ill figure it out but i feel i dont need it where it is in this code i have
tx side
void send_data(unsigned char data)
{
digitalWrite(4,(data&0x01));
digitalWrite(5,(data&0x02));
digitalWrite(6,(data&0x04));
digitalWrite(7,(data&0x08));
}
rx side
void RF_VT() // interrupt service function
{
data=(digitalRead(4)<<3)+(digitalRead(5)<<2)+(digitalRead(6)<<1)+(digitalRead(7)<<0);
Serial.print("data=");
Serial.println(data,DEC);
}
data=(digitalRead(4)<<3)+(digitalRead(5)<<2)+(digitalRead(6)<<1)+(digitalRead(7)<<0);
Here you are effectively packing single bit readings into one variable "data", possibly of type byte.
this is where i need help on my tx side if i want to send seperate data when a button is pushed how would i incorperate that
void loop(void)
{
//backward
if ( digitalRead(SW1) == HIGH)
{
data= 1;
Serial.println("Backward");
}
//forward
else if ( digitalRead(SW2) == HIGH)
{
data = 2;
Serial.println("Forward");
}
//left
else if ( digitalRead(SW3) == HIGH)
{
data = 3;
Serial.println("Left");
}
//right
else if ( digitalRead(SW4) == HIGH)
{
data = 4;
Serial.println("Right");
}
else
{
}
}
//====================================
void send_data(unsigned char data)
{
digitalWrite(4,(data&0x01));
digitalWrite(5,(data&0x02));
digitalWrite(6,(data&0x04));
digitalWrite(7,(data&0x08));
}
Something like this:
data = 0;
data =(digitalRead(SW1)<<3)+(digitalRead(SW2)<<2)+(digitalRead(SW3)<<1)+(digitalRead(SW4)<<0);
switch (data)
{
case 8:
Serial.println("Backward");
break;
case 4:
Serial.println("Forward");
break;
case 2:
Serial.println("Left");
break;
case 1:
Serial.println("Right");
break;
//default:
// Serial.println("0 or more than 1 switche pushed");
}
EDIT: corrected some lines.
.
Would you mind taking a look at my other post it has both my tx and rx code
http://forum.arduino.cc/index.php?topic=325938.msg2250636#msg2250636
Operators you can program in C or js for years in and never use
It would be pretty hard to program for more than a few seconds in C++ on a PC without the << operator in the iostream class.
I never quite understood why it's a left-shift for iostream.
AWOL:
I never quite understood why it's a left-shift for iostream.
Both right shift and left shift are implemented. It depends on the direction that the data is going. If using cout, the symbol points from the data to the stream (cout). If using cin, the symbols point from the stream (cin) to the variable that is to hold the data.
Seems logical to me.
PaulS:
Both right shift and left shift are implemented. It depends on the direction that the data is going. If using cout, the symbol points from the data to the stream (cout). If using cin, the symbols point from the stream (cin) to the variable that is to hold the data.Seems logical to me.
You beat me to it! This is one of the few cases where operator overloading has assigned completely different meanings to operators, and still be successful. It's generally frowned upon to overload operators with completely different semantics...
PaulS:
Seems logical to me.
But not to me.
If I write x = y = 2;
that's fine; assign 2 to y, and assign the result of that assignment to x.
but cout << "The " << "quick" << "brown ";
looks to me like "shift "brown" to "quick" and then to "The" before shifting something (what?) to "cout" ".
As I read it, there's no sense of movement or concatenation - it just looks ugly.
(I came very late to C++, so maybe I'm a bit like one of Dijkstra's brain-damaged BASIC programmers, and beyond any help)
Merkzilla,
+1 learning up on them, bit-wise operations can be really helpful!
Use a byte to save 8 booleans etc... sometimes ADT's you dream up for your application are screaming for something like this.
They're also really useful in making serial comms efficient.
Sometimes it's hard not to try to use them: for a man with a hammer everything looks like a nail
The streaming library uses the << operator as in iostream. I find quite convenient. At least a lot simpler than using lots of Serial.print() and Serial.println()
looks to me like "shift "brown" to "quick" and then to "The" before shifting something (what?) to "cout" ".
I can see that interpretation. The intended use of the operator, though, is shift "The " to the cout stream, and return a stream. Then, shift "quick " to the stream, and return a stream. Then, shift "brown " to the stream, and return a stream.
(I came very late to C++, so maybe I'm a bit like one of Dijkstra's brain-damaged BASIC programmers, and beyond any help)
I don't think so. You don't have to like all the "quirks" of a language to be able to use it.
cout.output("The "); cout.output("quick "); cout.output("brown...");
looks worse to me.