Hello everyone , I need help on my project! I need to do a calculator with 5 leds on arduino , but I don't know anything about the codes and the eletronic layout.. someone have any idea about doing it ? It might make additions 0-20 , degrees too 20-0 , thanks by the way!
How are the operands to be entered?
AWOL:
How are the operands to be entered?
Simple operands, like one by one!
How?
1+1 , 2+1 , 3+1 , and 3-1 , 2-1 , like that.. simple addition one by one, and simple subtraction.
OK let us know when you've thought it through.
pvzika:
Hello everyone , I need help on my project! I need to do a calculator with 5 leds on arduino , but I don't know anything about the codes and the eletronic layout.. someone have any idea about doing it ? It might make additions 0-20 , degrees too 20-0 , thanks by the way!
5 LEDs could be interpreted as binary bits, and with a 5-bit accumulator you could display numbers from 0 to 25-1= 31 (decimal).
Why do you think the range of 5-bit is 20 only?
What about:
5 LEDs and 5 resistors connected to 5 Arduino OUTPUT pins?
And 4 buttons connected to 4 input pins?
button-1: counter (count number of button presses for entering numbers)
button-2: + Addition
button-3: x Multiplication
button-4: = Calculate result (press two times == clear all)
Calculator range 0...31, binary LED signalling. Such like that?
Yes ; but instead of multiply , we need subtraction!
pvzika:
Yes ; but instead of multiply , we need subtraction!
So "addition" and "subtraction"?
Only positive results? Or negative results as well?
If you'd need negative results as well, the highest bit would be needed to distinguish positive and negative, and the range would possibly reduce to 4 bits as 24-1 = 15, total range for results -15...+15.
Or perhaps use "blinking" for signalling positive and negative results:
LEDs lighting steadily ==> positive result
LEDs flashing ==> negative result
(for result 0 = no LED it wouldn't matter if flashing or not)
The you could use 5 LEDs for a range of -31...+31 calculations easily.
-16 to +15 if you use 2's compliment.
Of course, there's still the unanswered question of how operands and operators are to be entered. . . .
No just positive results
AWOL:
-16 to +15 if you use 2's compliment
Perhaps we better save our compliments for the ladies.
I think you refer to 2's complement?
;D
pvzika:
No just positive results
So calculation range is 0...31 decimal with 5 LEDs.
And perhaps you do some "range error" signalling like "all LEDs blinking" in case of underflow or overflow results.
Here are some idea on how other do it
// basic stuff to start working in AVRstudio
#include <avr/io.h>
#include <util/delay.h>
//function prototypes(not needed in the arduini IDE)
void getKeyPress(void);
void keyData(int keyWord);
void shiftout(unsigned char myDataOut);
void pinMode(int position, int value);
int keyRow,columnData,ledRow,resulte,reset_loop,time_to_check = 0;;
unsigned char matrix_data[]={0,0,0,0};
unsigned char word_1=0,word_2=0;
int main(void){
DDRB = 0x0F;//pins PB0-PB3 outputs and pins PB4-PB7 inputs
DDRD = 0xFF;//pins PD0-PD6 are all outputs
while(1){// keeps the program running all the time
for(ledRow=0;ledRow<4;ledRow++){// this is the loop for scanning the LED display
time_to_check ++;//counts the time until the next key press check(to prevent flickering of the matrix)
PORTD = 1<<ledRow;//sets only one pin from PD0-PD3 HIGH
pinMode(5,0);//latch off
shiftout(matrix_data[ledRow]);//writes the data to the shift register
pinMode(5,1);//latch on
if(time_to_check == 125){//looks when the time is right to check for a key press
getKeyPress();// goes to the key press function
time_to_check =0;//resets the "counter"
}
else
_delay_ms(1);// a delay for each LED row
pinMode(5,0);//latch off
shiftout(0x00);// sets the shift registers outputs to low
pinMode(5,1);//latch on
}
}
return 1;
}
void getKeyPress(){ // checks what key is being pressed
for(keyRow=0;keyRow<4;keyRow++){ // scans the switch matrix(4 rows)
PORTB = (1<<keyRow); // sends a HIGH to one row at a time
columnData = (PINB & 0xF0);// reads the state of each column
_delay_us(100);// a small delay
if(columnData != 0){// if something is pressed columndata is bigger then zero
keyData(columnData | (1<<keyRow));//goes to the data analysis function
_delay_us(400);//a small delay like in the LED scanning loop
}
}
}
void keyData(int keyInput){//function for determining what each button does
switch (keyInput){
case 0b10000100:// first input row "1"
word_1 = word_1 << 1;
word_1 = word_1 | 0x01;
matrix_data[3] = word_1;
break;
case 0b10000010://first input row "0"
word_1 = word_1 << 1;
matrix_data[3] = word_1;
break;
case 0b10001000://first input row "DEL"
word_1 = 0;
matrix_data[3] = word_1;
break;
case 0b01000100://second input row "1"
word_2 = word_2 << 1;
word_2 = word_2 | 0x01;
matrix_data[2] = word_2;
break;
case 0b01000010://second input row "0"
word_2 = word_2<<1;
matrix_data[2] = word_2;
break;
case 0b01001000://second input row "DEL"
word_2 = 0;
matrix_data[2] = word_2;
break;
case 0b00010010://multiplication
resulte = word_1 * word_2;
matrix_data[1] = resulte & 0x00FF;
matrix_data[0] = (resulte >> 8) & 0x00FF;
break;
case 0b00010100://subtraction
resulte = word_1 - word_2;
matrix_data[1] = resulte & 0x00FF;
matrix_data[0] = (resulte >> 8) & 0x00FF;
break;
case 0b00011000://division
resulte = word_1 / word_2;
matrix_data[1] = resulte & 0x00FF;
matrix_data[0] = (resulte >> 8) & 0x00FF;
break;
case 0b00010001://addition
resulte = word_1 + word_2;
matrix_data[1] = resulte & 0x00FF;
matrix_data[0] = (resulte >> 8) & 0x00FF;
break;
case 0b00100010://modulo
resulte = word_1 % word_2;
matrix_data[1] = resulte & 0x00FF;
matrix_data[0] = (resulte >> 8) & 0x00FF;
break;
case 0b00100100://XOR
resulte = word_1 ^ word_2;
matrix_data[1] = resulte & 0x00FF;
matrix_data[0] = (resulte >> 8) & 0x00FF;
break;
case 0b00101000://OR
resulte = word_1 | word_2;
matrix_data[1] = resulte & 0x00FF;
matrix_data[0] = (resulte >> 8) & 0x00FF;
break;
case 0b00100001://AND
resulte = word_1 & word_2;
matrix_data[1] = resulte & 0x00FF;
matrix_data[0] = (resulte >> 8) & 0x00FF;
break;
case 0b10000001://NOT
resulte = ~resulte;
matrix_data[1] = resulte & 0x00FF;
matrix_data[0] = (resulte >> 8) & 0x00FF;
break;
case 0b01000001://DEL ALL
word_1 = 0;
word_2 = 0;
resulte =0;
for(reset_loop=0;reset_loop<4;reset_loop++)
matrix_data[reset_loop] = 0;
break;
default:// do nothing at all if more then 1 button is pressed
break;
}
}
void shiftout(unsigned char myDataOut) { // the function for sending the data to the shift register
int i;
int pinState=0;
pinMode(4,0);
pinMode(6,0);
for (i=7; i>=0; i--) {
pinMode(6,0);
if ( myDataOut & (1<<i) )
pinState = 1;
else
pinState = 0;
pinMode(4,pinState);
pinMode(6,1);
pinMode(4,0);
}
pinMode(6,0);
}
void pinMode(int position, int value)// simple function like the digitalWrite in the arduini IDE
{
if (value == 0)
PORTD &= ~(1 << position);
else
PORTD |= (1 << position);
}