#1 Програмування Arduino » ограничения на ввод в lcd » 2018-03-19 22:41:43

angruh
відповідей: 1

Нужно при введении num1 и num2  ограничивать количество символов до 6 но как это сделать

#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(14,15,16,17,18,19); 

long num1,num2 ;
double total;
char operation,button;
int n=1;
const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] = {
  {'1','2','3','+'},
  {'4','5','6','-'},
  {'7','8','9','*'},
  {'C','0','=','/'}
};
byte rowPins[ROWS] = {9,8,7,6};
byte colPins[COLS] = {5,4,3,2};    
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);


void setup() 
{
  
  lcd.begin(16,2);
  Serial.begin(9600);
 

}

void loop()
{
  

 
  while(1)
  {

        
    button = customKeypad.getKey(); 
    if (button=='C') 
    {
      num1=0;
      num2=0;
      total=0;
      operation=0;
      lcd.clear();
    }
        
    if (button >='0' && button <='9')
    {
      num1 = num1*10 + (button -'0'); 
      lcd.setCursor(0,0); 
      lcd.print(num1);    
    }
    
	if (num1 !=0 && (button=='-' || button=='+' || button=='*' || button=='/')) 
    {
      operation = button; 
      lcd.setCursor(7,0); 
	  while ((num1/=10) > 0) n++;
	  Serial.println(n);
      if(n >7)
      {
        num1=0;
        num2=0;
        total=0;
        operation=0;
        lcd.clear();
        lcd.print("Error");
        break;
      }
      n=0;
      lcd.print(operation); 
      break;
    }

  }
    
  while(1) 
  {
    if (button =='C')
      break;
    button = customKeypad.getKey();
    if (button=='C') 
    {
      num1=0;
      num2=0;
      total=0;
      operation=0;
      lcd.clear();
      break;
    }
    if (button >='0' && button <='9') 
    {
      num2 = num2*10 + (button -'0');
      int n=1;
	  while ((num1/=10) > 0) n++;
	  Serial.println(n);
      if(n >6)
      {
        num1=0;
        num2=0;
        total=0;
        operation=0;
        lcd.clear();
        lcd.print("Error");
        break;
      }
      lcd.setCursor(8,0);
      lcd.print(num2);
    }
    if (button == '=' && num2 !=0)
    {       
      domath();             
      break;  
    }
  }
    
  while(1) 
  {
    if (button =='C')
      break;

    button = customKeypad.getKey();
    if (button =='C') 
    {
      lcd.clear();
      lcd.setCursor(0,0);
      num1=0;
      num2=0;
      total=0;
      operation=0;
      break;
    }
  }

}


void domath()
{
  switch(operation)
  {
    case '+': 
      total = num1+num2;
      break;
      
    case '-': 
      total = num1-num2;
      break;
      
    case '/':
      if(num2 == 0)
      {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("ERROR");
      }
      total = (float)num1/(float)num2;
      break;
      
    case '*': 
      total = num1*num2;
      break;
          
  }
    
  lcd.setCursor(0,1);
  lcd.print('=');
  lcd.setCursor(1,1);
  lcd.print(total);   
} 

#2 Програмування Arduino » удержание кнопки Keypad » 2018-03-14 19:47:16

angruh
відповідей: 1
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(14,15,16,17,18,19); 
 
long num1,num2 ;
double total;
char operation,button;
 
const byte ROWS = 4;
const byte COLS = 4;
 
char keys[ROWS][COLS] = {
  {'1','2','3','+'},
  {'4','5','6','-'},
  {'7','8','9','*'},
  {'C','0','=','/'}
};
byte rowPins[ROWS] = {9,8,7,6}; 
byte colPins[COLS] = {5,4,3,2};     
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
 
 
void setup() 
{
  
  lcd.begin(16,2);
  Serial.begin(9600);
 
 
}
 
void loop()
{
  
 
 
  while(1)
  {
     
        
    button = customKeypad.getKey(); 
    if (button=='C') 
    {
      num1=0;
      num2=0;
      total=0;
      operation=0;
      lcd.clear();
    }
        
    if (button >='0' && button <='9') 
    {
      num1 = num1*10 + (button -'0'); 
     
      lcd.setCursor(0,0); 
      lcd.print(num1);    
    }
    
    if (num1 !=0 && (button=='-' || button=='+' || button=='*' || button=='/')) 
    {
     
      operation = button;  
      lcd.setCursor(0,1);  
      lcd.print(operation); 
      break;
    }
 
  }
    
  while(1) 
  {
    
    if (button =='C')
      break;
    button = customKeypad.getKey();
    if (button=='C') 
    {
      num1=0;
      num2=0;
      total=0;
      operation=0;
      lcd.clear();
      break;
    }
    if (button >='0' && button <='9') 
    {
      num2 = num2*10 + (button -'0');
      lcd.setCursor(1,1);
      lcd.print(num2);
    }
    if (button == '=' && num2 !=0)
    {       
      domath();             
      break;  
    }
  }
    
  while(1) 
  {
        
    if (button =='C')
      break;
 
    button = customKeypad.getKey();
    if (button =='C') 
    {
      lcd.clear();
      lcd.setCursor(0,0);
      num1=0;
      num2=0;
      total=0;
      operation=0;
      break;
    }
  }
 
}
 
 
void domath()
{
  switch(operation)
  {
    case '+': 
      total = num1+num2;
      break;
      
    case '-': 
      total = num1-num2;
      break;
      
    case '/': 
      total = (float)num1/(float)num2;
      break;
      
    case '*': 
      total = num1*num2;
      break;
          
  }
    
  lcd.setCursor(0,1);
  lcd.print('=');
  lcd.setCursor(1,1);
  lcd.print(total);   
}

как ето реализовиать
одиночное нажатие на кнопку «удалить»(C) должно удалять один символ;
удержание кнопки в течение двух секунд должно очищать экран;
удержание кнопки «0» должно выводить на дисплей точку, для работы с
десятичными дробями;
не могу понять как в коде задать удержания клавиш

#3 Програмування Arduino » простой калькулятор » 2018-03-13 00:05:01

angruh
відповідей: 1
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7,8,9,10,11,12); 

long num1,num2 ;
double total;
char operation,button;

const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] = {
  {'1','2','3','+'},
  {'4','5','6','-'},
  {'7','8','9','*'},
  {'C','0','=','/'}
};
byte rowPins[ROWS] = {A2,A3,A4,A5}; 
byte colPins[COLS] = {2,3,4,5};     
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);


void setup() 
{
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  lcd.begin(16,2);
 

}

void loop()
{
  
 
  while(1)
  {
     
        
    button = customKeypad.getKey(); 
    if (button=='C') 
    {
      num1=0;
      num2=0;
      total=0;
      operation=0;
      lcd.clear();
    }
        
    if (button >='0' && button <='9') 
    {
      num1 = num1*10 + (button -'0'); 
     
      lcd.setCursor(0,0); 
      lcd.print(num1);    
    }
    
	if (num1 !=0 && (button=='-' || button=='+' || button=='*' || button=='/')) 
    {
     
      operation = button;  
      lcd.setCursor(0,1);  
      lcd.print(operation); 
      break;
    }

  }
    
  while(1) 
  {
    
    if (button =='C')
      break;
    button = customKeypad.getKey();
    if (button=='C') 
    {
      num1=0;
      num2=0;
      total=0;
      operation=0;
      lcd.clear();
      break;
    }
    if (button >='0' && button <='9') 
    {
      num2 = num2*10 + (button -'0');
      lcd.setCursor(1,1);
      lcd.print(num2);
    }
    if (button == '=' && num2 !=0)
    {       
      domath();             
      break;  
    }
  }
    
  while(1) 
  {
        
    if (button =='C')
      break;

    button = customKeypad.getKey();
    if (button =='C') 
    {
      lcd.clear();
      lcd.setCursor(0,0);
      num1=0;
      num2=0;
      total=0;
      operation=0;
      break;
    }
  }

}


void domath()
{
  switch(operation)
  {
    case '+': 
      total = num1+num2;
      break;
      
    case '-': 
      total = num1-num2;
      break;
      
    case '/': 
      total = (float)num1/(float)num2;
      break;
      
    case '*': 
      total = num1*num2;
      break;
          
  }
    
  lcd.setCursor(0,1);
  lcd.print('=');
  lcd.setCursor(1,1);
  lcd.print(total);   
} 

Код не работает, на дисплей ничего не виводить, помогите где ошибка?

918670d1520888410

#5 Re: Програмування Arduino » Генерация десятеричного числа с помощью переключателей » 2018-02-19 13:48:34

int dipPins[] = {1,2,3,4};       
int transAddress;

void setup()
{
 Serial.begin(9600);
 int i;
 for(i = 0; i<=3; i++){
   pinMode(dipPins[i], INPUT);      
   digitalWrite(dipPins[i], HIGH);  
  }
}

void loop()
{
transAddress = address();
Serial.print("Numeric: ");
Serial.println(transAddress);
delay(1000);
}

byte address(){
 int i,j=0;
 

 for(i=0; i<=3; i++){
 j = (j << 1) | digitalRead(dipPins[i]);  
 }
 return j; 
}

4_3lab.JPG?raw=true

Вот зделал но оно не правильно числа генерирует, в чом ошибка?

#6 Програмування Arduino » Генерация десятеричного числа с помощью переключателей » 2018-02-17 23:47:28

angruh
відповідей: 10

Нужно реализувать вывод в последовательный порт десятеричного числа,
задаваемого с помощью тумблеров
4_3lab.jpg

Підвал форуму