Is that possible to use 1 byte as 8 bits for 8 toggle switches ?
Is that possible to use 1 byte as 8 bits for 8 toggle switches ?
Yes, it is.
Qwseyvnd:
Is that possible to use 1 byte as 8 bits for 8 toggle switches ?
It may be easier on a Mega than on an Uno. Because an Uno has fewer ports none of them has all 8 pins free so, for example digital pins 8 to 13 are bits 0 to 5 in Port B but you can't use bit 6 and 7 because they are connected to the 16Mhz oscillator. You would need to use pins on a different Port to make up the full 8 - perhaps digital pins 6 and 7 which are bits 6 and 7 of Port D.
Then you could read all 8 switches with this code (not tested)
byte bb = PINB;
bb &= 0b00111111; // to clear rubbish from bits 6 and 7
byte dd = PIND;
dd &= 0b11000000; // we are only interested in bits 6 and 7
byte switches = bb & dd; // merges the bits from the 2 ports
...R
if you don't need direct port manipulation, you could have your pick of RelayPins, as in this example.
Uses bitRead() and bitWrite() to toggle LED's based on the the state of 8 inputs.
this example works:
int ledPin[8] = {10,11,12,13,A0,A1,A2,A3};
int buttonPin[8] = {2,3,4,5,6,7,8,9};
byte switchState = 0b00000000;
byte lastByte;
void setup()
{
Serial.begin(115200);
for (byte i = 0; i < 8; i++)
{
pinMode(buttonPin[i], INPUT_PULLUP);
pinMode(ledPin[i], OUTPUT);
}
}
void loop()
{
for (byte i = 0; i < 8; i++)
{
bitWrite(switchState, i, digitalRead(buttonPin[i]));
}
for (byte i = 0; i < 8; i++)
{
digitalWrite(ledPin[i], bitRead(switchState, i));
}
if (lastByte != switchState)
{
Serial.println(switchState, BIN);
}
lastByte = switchState;
}
ABsolutly nothing to do with the OP question, but (for those who like archaic stuff), the OP question reminds me of the IBM 360 (and ICL system 4 clone).
These had a set of switches known as UPSI (User Program Switch Indicator).
You would set the switches before your batch job ran, and depending on the way the switches were set, you could read the byte/bits and alter your code execution accordingly.
I think (and this is the best part of 40-50 years ago), later implementations allowed a special punched card to be read that gave you the UPSI byte.
Blast from the past.
Stan
You could use a 74HC165 8 Bit Parallel Load Shift Register. Connect 8 switches to the 165 inputs, Use 3 pins of the Arduino to control the 165 (cliock, data and latch). ShiftIn() to bring the switch states to a byte variable. The switches will need pullups (10K).
The C compiler can make memory-efficient operations with bits more friendly, though it's not terribly CPU efficient (when it comes to CPU vs RAM, you usually have to trade off one for the other). It's called "packing":
Ah like a good old Altair 8800
Is that possible to use "long" 32-bits for MEGA2560 ?
sure
'unsigned long' gets you all 32 bits
Not deja vu, double post. Someone didn't read the Read Me Before You Post - post.
stan_w_gifford:
ABsolutly nothing to do with the OP question, but (for those who like archaic stuff), the OP question reminds me of the IBM 360 (and ICL system 4 clone).These had a set of switches known as UPSI (User Program Switch Indicator).
You would set the switches before your batch job ran, and depending on the way the switches were set, you could read the byte/bits and alter your code execution accordingly.
I think (and this is the best part of 40-50 years ago), later implementations allowed a special punched card to be read that gave you the UPSI byte.
Blast from the past.
Stan
So if you could get a girl named Daisy to load the cards and give her the deck then you could hand her that last card and say, "Oh, here's the UPSI, Daisy."
GoForSmoke:
stan_w_gifford:
ABsolutly nothing to do with the OP question, but (for those who like archaic stuff), the OP question reminds me of the IBM 360 (and ICL system 4 clone).These had a set of switches known as UPSI (User Program Switch Indicator).
You would set the switches before your batch job ran, and depending on the way the switches were set, you could read the byte/bits and alter your code execution accordingly.
I think (and this is the best part of 40-50 years ago), later implementations allowed a special punched card to be read that gave you the UPSI byte.
Blast from the past.
Stan
So if you could get a girl named Daisy to load the cards and give her the deck then you could hand her that last card and say, "Oh, here's the UPSI, Daisy."
But we digress, surely
Thread locked. Replies go here...
http://forum.arduino.cc/index.php?topic=273049.0
@Qwseyvnd, do not cross-post again.