So I just started to use an LCD S3O1C3TR which is has a small 3-digit display. Is there any way I can assign 'x' amount of pins (from my arduino) to ONE variable and then send a signal to that variable which in turn sends a signal to all the pins it was assigned under?
there is no such things as assigning a pin to a variable.
there is no magic happening, if you want int x; to take the value of the pin Digital 3 then you need to do x = digitalRead(3); (of course better if you gave that pin a name before).
const byte motorPin = 3;
int motorState;
...
motorState = digitalRead(motorPin);
if you need another variable to be the same as motorState, then do
const byte motorPin = 3;
int motorState, anotherState;
...
motorState = digitalRead(motorPin);
anotherState = motorState;
and you don't send signal to a variable, you set PINS to a specific value.
motorState = HIGH; won't make pin 3 HIGH (just the variable) but
motorState = HIGH;
digitalWrite(motorPin, motorState);
will set that PIN high (and the variable)
makes sense?
Yes this does make sense. Sorry for the bad communication but here is what I was trying to say. I'll try and guide you step by step.
int a1 = 2, b1 = 3, c1 = 4, d1 = 5, e1 = 6, f1 = 7, g1 = 8;
//Pins 2-8 are given values that correspond with the segments on LCD display ex: Pin 2 = a1(segment 1), Pin 3 = b1(segment 2) etc...
[code]
int nine = a1, b1, c1, d1, f1, g1;
//In order to show the number 9 on the display, all these segments need to receive a high signal (except e1)
int a1 = 2, b1 = 3, c1 = 4, d1 = 5, e1 = 6, f1 = 7, g1 = 8;
int nine = a1, b1, c1, d1, f1, g1;
void setup(){
}
void loop (){
digitalWrite(nine, HIGH);
delay(500);
digitalWrite(nine, LOW);
delay(500);
}
//This is what I thought would make number 9 blink on the display but clearly its wrong
Sorry if this may sound confusing, i'm new to using the arduino and have little experience
sketch_sep10_1.ino (195 Bytes)
int nine = a1, b1, c1, d1, f1, g1;
No that won't do what you want. technically a variable can only hold 1 value. a variable is a location in memory and the variable is the address of that location.
when you say int nine = 10; you declare an int, 2 bytes somewhere in memory, and you then write the binary representation of 10 into those 2 bytes.
what you need is an array (read the doc)
int nine[] = {a1, b1, c1, d1, f1, g1};
and if you need to find out how many pins are "attached" into that array it would be
int nbOfPinsForNine = (sizeof(nine) / sizeof(nine[0]));
Syncmaster:
So I just started to use an LCD S3O1C3TR which is has a small 3-digit display. Is there any way I can assign 'x' amount of pins (from my arduino) to ONE variable and then send a signal to that variable which in turn sends a signal to all the pins it was assigned under?
Sort of but not really since you don't "send a signal to a variable". You can assign values to a variable, but signals are what you send to pins.
yourVariable =
pinsOfInterest[] = {3, 5, 6, 8, 11, etc}
for (byte i = 0; i < ; i++)
doTheThing (pinsOfInterest[ i ], yourVariable, etc);
will keep writing whatever yourVariable is to all of those pins.
Ah yes an array, I know what that is. This helped me so much, ive been stuck on this problem for a few hours now. Thank you guys so much for the help
![]()
Now whenever i run this
int a1 = 2, b1 = 3, c1 = 4, d1 = 5, e1 = 6, f1 = 7, g1 = 8;
int myInts[7];
int nine[] = {a1,b1,c1,d1,f1,g1};
void setup() {
Serial.begin(9600);
for(int i = 2; i < 9; i++){
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
}
void loop() {
digitalWrite(nine, HIGH);
delay(500);
digitalWrite(nine, LOW);
delay(500);
}
It says this as an error
exit status 1
invalid conversion from 'int*' to 'uint32_t {aka long unsigned int}' [-fpermissive]
Ok nevermind it woked
![]()
Just for the future, always post the full error, not just the last couple lines. That will usually (but not always) have some information about what line the problem is on.
The other way to do this is with bit-fiddling on the ports.
Ports vary from board to board, and so it's usually not a good way to go about things. But it will work.
On the UNO, PORTD corresponds to pins 0-7 and PORTB corresponds to pins 8-13.
Lets say that you are using pins 6 to 12 for your seven-segment output. If you put
0b11000000 into PORTD, and 0b11111 into PORTB, this will turn on all your seven segment pins. Unhapilly, it will also turn off all the other pins in the range 0-13. What you need to do is to turn on some bits without turning off others. To do this, you use the C++ bitwise operators. To turn off a bit, you AND a value with something that has all the bits on except the one you want. To turn on a bit, you OR it with the bits you want.
So to turn off all the bits of your seven segment display, you need to
PORTD = PORTD & ~0b11000000;
PORTB = PORTB & ~0b11111;
Remember - this is only correct on a UNO.
So how to you get the thing to display a '1'? Well, lets say that to display a '1' you want pins 6 and 11 to be on, and all the rest off. Pin 6 is the fifth bit of PORTD, and pin 11 is the fourth bit of PORTB. The handy way to get these two values is by left-shifting a single bit. 1<<4 and 1<<3 are the two numbers you need.
Putting it altogether
const byte PORTD_MASK = 0b11000000;
const byte PORTB_MASK = 0b11111;
const byte PORTD_MASK_ONE = 1<<4;
const byte PORTB_MASK_ONE = 1<<3;
void display_ONE() {
PORTD = (PORTD & PORTD_MASK) | PORTD_MASK_ONE;
PORTB = (PORTB & PORTB_MASK) | PORTB_MASK_ONE;
}
Now, you may feel that this is more trouble than it's worth. And you'd be right. This is why everyone uses digitalWrite() unless there is a pressing need for your port writes to be microsecond fast. But - yes, it's possible to write multiple pins simultaneously.
Glad it worked
PaulMurrayCbr:
The other way to do this is with bit-fiddling on the ports.
Hi,
I am new to Arduino. Have been using PIC mostly.
One question regarding this: If i use a complete port for input and output, which is useful in my case, how do i define the 'direction' of the complete port?
did you check the documentation?
DDRx is used for direction. But the logic is inverse from PIC: writing 1 to a bit in DDRx makes the pin OUTPUT.