I am writing a code for Arduino calculator and I would be thankful for your help. I have defined a 2x7 keypad, consisting of numbers from 0 - 9 and 4 math operations (+,-,*,/). I have included Keypad.h library. I am using LATCHING buttons for input. Everytime I press a button it stays locked in. If i want to turn a button OFF, I have to press it again to release it from ON state. I get input displayed on OLED Arduino display. There lies a problem in my code, because when I press a button I get number displayed on display, but then I have to turn OFF the same button and press another one to get the next number/operation displayed. How can I change the code so that I don't need to turn off the button to press another one and get its value displayed on the screen? Thank you for your help in advance!
See the code bellow:
#include <Keypad.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const byte numRows= 2; //number of rows on the keypad
const byte numCols= 7 ; //number of columns on the keypad
//keymap defines the key pressed according to the row and columns just as appears on the keypad
char customKey = 0;
double first = 0;
long second = 0;
double total = 0;
char Operator;
bool equalPressed=false;
bool showFirst = false;
char keymap[numRows][numCols]=
{
{'0', '1', '2', '3', '4', '5', '6'},
{'7', '8', '9', '+', '-', 'x', '/'}
};
//initializes an instance of the Keypad class
byte rowPins[numRows] = {2,3}; // definiraj pine ko zvezeš gumbe!!
byte colPins[numCols]= {4,5,6,7,8,9,10}; // definiraj pine ko zvezeš gumbe!!
Keypad customKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
void showSplash() {
String splashString="WELCOME";
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.setCursor(64-(splashString.length()*3),0);
display.print(splashString);
display.setTextSize(2);
splashString="MATEJ";
display.setCursor(64-(splashString.length()*6),16);
display.print(splashString);
display.setTextSize(2);
splashString="ALEN";
display.setCursor(64-(splashString.length()*6),40);
display.print(splashString);
display.display();
delay(3000);
}
void setup()
{
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
showSplash();
display.setTextSize(2);
display.clearDisplay();
display.display();
}
void loop(){
customKey = customKeypad.getKey();
switch(customKey)
{
case '0' ... '9': // This keeps collecting the first value until a operator is pressed "+-*/"
showFirst=true;
first = first * 10 + (customKey - '0');
showDisplay();
break;
case '+':
showFirst=true;
Operator='+';
showDisplay();
second = SecondNumber(); // get the collected the second number
total = first + second;
showDisplay();
first = total, // reset values back to zero for next use
second = 0;
break;
case '-':
showFirst=true;
Operator='-';
showDisplay();
second = SecondNumber();
total = first - second;
showDisplay();
first = total, second = 0;
break;
case 'x':
showFirst=true;
Operator='x';
showDisplay();
second = SecondNumber();
total = first * second;
showDisplay();
first = total, second = 0;
break;
case '/':
showFirst=true;
Operator='/';
showDisplay();
second = SecondNumber();
second == 0 ? display.print("Invalid") : total = (float)first / (float)second;
showDisplay();
first = total, second = 0;
break;
}
}
void showDisplay()
{
display.clearDisplay();
display.setCursor(110,0);
display.println(Operator);
if (showFirst) {
Serial.print(first);
display.println(first);
} else
{
display.println("");
}
if (second>0) {
display.println(second);
} else
{
display.println("");
}
if (equalPressed) {
display.println(total);
equalPressed=false;
}
display.display();
}
long SecondNumber()
{
while( 1 )
{
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
second = second * 10 + (customKey - '0');
showDisplay();
}
}
return second;
}
Related
I am having trouble with a 16x2 I2C LCD screen using a 4x3 keyboard.
The following are the steps I need to do:
1.Take a numeric value through the keyboard onto the top line of the screen. This numeric value should not exceed 3 digits.
2. The keyboard should only be active (and the screen editable) when * key is pressed once.
3. The keyboard should again become inactive when # key is pressed once.
4. Whatever value has been placed in the screen should be remembered so when a sensor runs it should be able to compare its value to the screen value, achieving equality to which the core process stops.
Below is my code which is not doing the job. Can someone suggest a solution? Thanks.
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 4);
#include<stdio.h>
const int ROW_NUM = 4;
const int COLUMN_NUM = 3;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'},
};
byte pin_rows[ROW_NUM] = {8,7,6,5};
byte pin_column[COLUMN_NUM] = {4,3,2};
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
int counter = 0;
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.clear();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Volume. ml");
lcd.setCursor(0, 1);
lcd.print("Filled ml");
}
void loop() {
// put your main code here, to run repeatedly:
keypadfunction();
}
void keypadfunction()
{
char key = keypad.getKey();
if (key)
{
}
if (key == '*')
{
while (key != '#' || counter <=3)
{
lcd.setCursor(8, 1);
lcd.setCursor(8+counter, 1);
lcd.print(key);
counter = counter+1;
}
}
}
changes after suggestions from #C1sc0:
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 4);
#include<stdio.h>
const int ROW_NUM = 4;
const int COLUMN_NUM = 3;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'},
};
byte pin_rows[ROW_NUM] = {8,7,6,5};
byte pin_column[COLUMN_NUM] = {4,3,2};
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column,
ROW_NUM, COLUMN_NUM );
char volume[3];
byte volumeCount = 0;
bool edit = 0;
int finalValue = 0;
void setup()
{
// put your setup code here, to run once:
lcd.init();
lcd.clear();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Volume ml");
lcd.setCursor(0, 1);
lcd.print("Filled ml");
}
void loop()
{
char key = keypad.getKey();
if(key) // check if any key was pressed
{
if(key == '*') // if * was pressed switch to edit mode
{
lcd.setCursor(0,0); // set your cursor at columnt 0, row 0
lcd.clear();
lcd.print("Enter volume: ");
edit = true;
lcd.setCursor(0,1); // set your cursor to second row
volumeCount = 0;
volume[0] = '0';
volume[1] = '0';
volume[2] = '0';
}
if(edit && volumeCount < 3) // enter edit mode
{
volume[volumeCount] = key; // save key to a char array
lcd.setCursor(volumeCount,1); // set your cursor to the
next position
lcd.print(volume[volumeCount]); // print the pressed button to lcd
volumeCount++; // increment the array index (cursor position)
}
if(volumeCount == 3 || key == '#') // array.length == 3 OR you pressed #
{
edit = false; // disable edit mode
volumeCount = 0; // reset your counter
lcd.setCursor(0,0);
lcd.clear();//new
lcd.print(volume);
finalValue = atoi(volume); // save your entered value for further usage
//volume[0] = '0';
//volume[1] = '0';
//volume[2] = '0';
}
}
buttonState1 = digitalRead(buttonPin1);
if (buttonState1 == HIGH)
{
//do process basis 'finalValue'
}
}
Screen output
Create a bool (or byte) variable, for signaling the "edit" state
if (key == '*') { edit= true; }
in your loop (this will be your first if condition).
Create another if and here check the edit flag (if it is true, then you are waiting for the 3 values. You can read the pressed keys to a char array
char values[3];
// create this variable, before setup()
Here for the index you can use a counter (which is already present in your code as I saw. So, put all these lines in your second if condition, and increment your counter by one here. And you can display the entered character here too:
lcd.setCursor(8+counter, 1);
lcd.print(key);
Create a third if block, and check the counter is # key or counter == 3. If this condition will be satisfied you have to do 3 things reset the counter to zero, reset the edit state to false, and finally convert your value to an int (with atoi function).
After it works, you can refactor your code to make it more efficient/readable.
You need something like this:
char volume[3];
byte volumeCount = 0;
bool edit = 0;
int finalValue = 0;
void loop()
{
char key = keypad.getKey();
if(key) // check if any key was pressed
{
if(key == '*') // if * was pressed switch to edit mode
{
lcd.setCursor(0,0); // set your cursor at columnt 0, row 0
lcd.print("Enter volume: ");
edit = true;
lcd.setCursor(0,1); // set your cursor to second row
volumeCount = 0;
volume[0] = '0';
volume[1] = '0';
volume[2] = '0';
}
if(edit && volumeCount < 3 && key != '*' && key != '#')
{
volume[volumeCount] = key; // save key to a char array
lcd.setCursor(volumeCount,1); // set your cursor to the next position
lcd.print(volume[volumeCount]); // print the pressed button to lcd
volumeCount++; // increment the array index (cursor position)
}
if(volumeCount == 3 || key == '#') // array.length == 3 OR you pressed #
{
edit = false; // disable edit mode
volumeCount = 0; // reset your counter
finalValue = atoi(volume); // save your entered value for further usage
volume[0] = '0';
volume[1] = '0';
volume[2] = '0'; # ty for the fix!!!
}
}
}
I am using an Arduino 2560, NEMA23 Stepper motor with a TB6600 driver. The driver is set to 1/32 step division. The motor by default is 200 steps per revolution.
I want to input step size, number of steps and settling time.
Once the loop completes the number of steps i want to return to the starting point.
The plan is to take multiple images and stack them in Photoshop.
So far everything works except for the return to starting point ...some of the time.
If I don't step too far, meaning a combination of step size and number of steps, the motor returns to the starting point. If I exceed "X" distance the last step continues to move forward instead of backwards. I haven't fully tested what "X" distance is.
Example: If I use 5 steps with a step size of 5000 then the code returns to the starting point. If I change the steps to 7 and keep the step size at 5000 it does not return but moves forward.
Here is the complete code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Stepper.h>
LiquidCrystal_I2C lcd(0x27,20,4);
//parameters for keypad
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] =
{
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {3,4,5,6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7,8,9,10}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int j;
long stepSize=0;
long numSteps=0;
int settleTime=0;
int stepsPR=6400;
int DelayCamClose=10;
int DelayAfterCam=500;
int relayCam=A0; //pin to relay to trigger camera
Stepper focusStack(stepsPR, 13,12);
//get keypad entry
int GetNumber()
{
int num = 0;
char key = keypad.getKey();
while(key != '#')
{
switch (key)
{
case NO_KEY:
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
lcd.print(key);
num = num * 10 + (key - '0');
break;
case '*':
num = 0;
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
break;
}
key = keypad.getKey();
}
return num;
}
void setup()
{
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
pinMode(relayCam, OUTPUT);
digitalWrite (relayCam,HIGH);
}
void loop()
{
{
lcd.clear();
lcd.setCursor (0,0);
lcd.print("Set Settle Time");
lcd.setCursor (0,1);
settleTime = GetNumber();
}
{
lcd.clear();
lcd.setCursor (0,0);
lcd.print("Set Step Size");
lcd.setCursor (0,1);
stepSize = GetNumber();
}
{
lcd.clear();
lcd.setCursor (0,0);
lcd.print("Set Number Of Steps");
lcd.setCursor (0,1);
numSteps = GetNumber();
}
j=1;
while (j<=numSteps)
{
focusStack.step(stepSize);
delay(settleTime);
digitalWrite (relayCam,LOW); //opens the camera shutter
delay (DelayCamClose); //wait time to close camera shutter
digitalWrite (relayCam,HIGH); //close camera shutter
delay (DelayAfterCam);
j=j+1;
}
{
focusStack.step(-stepSize*numSteps); //returns to start
}
}
The issue is with the math in the last line bit I'm not sure what it is. I've tried setting
long stepSize=0;
long numSteps=0;
as int and float but that did not solve the problem.
I also changed
focusStack.step(-stepSize*numSteps); //returns to start
to
focusStack.step(stepSize*numSteps); //returns to start
but that only reverses the problem, as expected it might.
I have also tried using the AccelStepper Library but that introduced a whole different set of issues.
Any help would be greatly appreciated. I am still fairly new to programming.
Found the problem. Since stepSize and numSteps are integers the math flips any result over 32767 (16 bit). Anything over 32767 results in an overflow and is converted to a negative.
I now just need to set the stepper microsteps to a value that will allow enough travel and stay under the 32767 limitation.
i need to add millis in my code but i dont know exactly where should be ok to add millis or where is it acctually needed.Because i have used only delay till now can you help me and tell me where can millis be implemented in this piece of code.If it is possible to add millis somewhere in or after the button modes or switch case because it would be nicer if the modes changed properly because for now their changing either when the button is pressed normal or somtimes you need to pres the button twice to change to the next mode.I would apriciate all the help.
const int BUTTON_SWITCH = 8;
const int BUTTON_ALARM = 9;
const int KNOB = A0;
const int TEMP = A1;
const int tempRES = 10000; // the resistance of the NTC at 25'C is 10k ohm
const int NTC_MATERIAL_CONSTANT = 3950;
const int RED_LED = 4;
const int BUZZER = 3;
int index = 1;
double value;
int state = 0;
unsigned long time_now = 0;
int period = 1000;
char incomingOption;
#include "Display.h"
void setup() {
Serial.begin(9600);
pinMode(BUTTON_SWITCH, INPUT_PULLUP);
pinMode(BUTTON_ALARM, INPUT_PULLUP);
pinMode(RED_LED, OUTPUT);
}
float get_temperature()
{
float temperature, resistance;
int value;
value = analogRead(TEMP);
resistance = (float)value * tempRES / (1024 - value); // Calculate resistance
/* Calculate the temperature according to the following formula. */
temperature = 1 / (log(resistance / tempRES) / NTC_MATERIAL_CONSTANT + 1 / 298.15) - 273.15;
return temperature;
}
void loop() {
if (digitalRead(BUTTON_SWITCH) == LOW) { // button for switching the 3 modes
index = index + 1;
}
if (digitalRead(BUTTON_ALARM) == LOW) { // button for the alarm
displayAlarm();
}
if (Serial.available()) {
// Read entire buffer up to newline character
// Since on the C# side, serialPort1.WriteLine appends a newline character
String respond = Serial.readStringUntil('\n');
if (respond == "RESET") {
digitalWrite(RED_LED, LOW);
digitalWrite(BUZZER, LOW);
}
}
if (index > 3) { // when the code is on the last mode press the button to turn back to the first mode
index = 1;
}
get_temperature();
float celcius;
celcius = get_temperature();
if(celcius<16 || celcius>27){ // if the temperature becomes less than 16 degrees or goes higher than 27 degrees turn on the alarm
displayAlarm();
}
switch (index) {
case 1: displayTime(); break; // switch between the 3 different modes
case 2: displayTemp(); break;
case 3: displayAngle(); break;
}
}
void displayTime() {
float timer = Serial.parseFloat(); // take the current time from the c# application and display it on the arduino board
Display.show(timer);
}
void displayTemp() {
float celcius;
celcius = get_temperature(); // mode for displaying the current temperature
Display.show(celcius);
}
void displayAngle() {
int value = analogRead(KNOB); // read and save analog value from the potentionmeter
value = map(value, 0, 1023, 0, 30); // Map value 0-1023 to 0-30
Display.show(value);
}
void displayAlarm() {
Serial.println("Alarm");
digitalWrite(RED_LED, HIGH);
tone(BUZZER, 1500, 700);
}
I'm having troubles trying to use two 595 shift registers to output numbers on a 4 digit 7seg display.
I've gotten to the point of displaying numbers correctly, but I'm now having the issue that the output is flashing some garbage between the digits being displayed. How do I prevent this from happening?
I'm pretty sure the issue is that as I'm using bytes to send to the registers it is latching between bytes being displayed.
Here is my code
int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
int i = 0;
int waitTime = 500;
// digits from the right
byte colDig[4] =
{
B00001000, // digit 1
B00000100, // digit 2
B00000010, // digit 3
B00000001, // digit 4
};
const byte digit[10] = //seven segment digits in bits
{
B11000000, // 0
B11111001, // 1
B10100100, // 2
B10110000, // 3
B10011001, // 4
B10010010, // 5
B10000010, // 6
B11111000, // 7
B10000000, // 8
B10011000, // 9
};
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop()
{
// step through each digit then increment
// the counter by one, until nine
for(int j = 0;j<9;j++){
updateShiftRegister(0, j);
delay(waitTime);
updateShiftRegister(1, j);
delay(waitTime);
updateShiftRegister(2, j);
delay(waitTime);
updateShiftRegister(3, j);
delay(waitTime);
}
}
void updateShiftRegister(int col, int num)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, colDig[col]);
shiftOut(dataPin, clockPin, MSBFIRST, digit[num]);
digitalWrite(latchPin, HIGH);
}
So it looks like I was sort of correct,
The shiftOut function sets the clock pin to low at the end of the function, effectively forcing a latch.
By slightly modifying the code on this page I was able to stop this and it works perfect now.
http://arduino.cc/en/Tutorial/ShftOut23
// the heart of the program
void shiftItOut(int myDataPin, int myClockPin, byte myDataOut) {
// This shifts 8 bits out MSB first,
//on the rising edge of the clock,
//clock idles low
//internal function setup
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
//clear everything out just in case to
//prepare shift register for bit shifting
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
//for each bit in the byte myDataOut
//NOTICE THAT WE ARE COUNTING DOWN in our for loop
//This means that %00000001 or "1" will go through such
//that it will be pin Q0 that lights.
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
//if the value passed to myDataOut and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDataPin, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, 0);
}
//stop shifting
//digitalWrite(myClockPin, 0);
}
Simply commenting out that last digitalWrite fixed it all.
I have an application where I am using a MCP3421 18bit ADC to read analog data. The setup is Xbee+Xbee Sheild+Arduino + MCP3421 as Transmitter. This I am reading and transmitting to a remote xbee+arduino module with LCD. The data is displayed fine on the LCD. however I want to receive the data on the Serial port. When i try tp Do a Serial.println(s); on the receiving code the data which i get on serial port is garbled. Would appreciate any help
Here is my Code
Transmitting
#include <Wire.h>
#include <LiquidCrystal.h>
#define TRUE 1
#define FALSE 0
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup(void)
{
Serial.begin(9600);
Wire.begin();
delay(100);
Serial.println(">>>>>>>>>>>>>>>>>>>>>>>>"); // just to be sure things are working
lcd.begin(16, 2);
}
void loop(void)
{
byte address, Hi, Lo, Config;
int ADVal;
while(1)
{
address = 0x68;
Wire.beginTransmission(address);
Wire.write(0x88); // config register %1000 1000
// /RDY = 1, One Conversion, 15 samples per, PGA = X1
Wire.endTransmission();
delay(1);
Wire.requestFrom((int)address, (int) 3);
Hi = Wire.read();
Lo = Wire.read();
Config = Wire.read();
Wire.endTransmission();
ADVal = Hi;
ADVal = ADVal * 256 + Lo;
// Serial.print(ADVal, DEC);
//Serial.print(" ");
//Serial.println(Config, DEC);
Serial.print("<");
Serial.print(ADVal);
Serial.print(">");
//lcd.setCursor(0,0);
//lcd.print(ADVal);
lcd.setCursor(0,1);
//float val = ADVal * 0.00006244087;
//lcd.print(val,3);
delay(1);
}
}
and this is the receiving code
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
bool started = false;
bool ended= false;
char inData[10]; // Leave plenty of room
byte index;
float i;
//char inData[24]; // Or whatever size you need
//byte index = 0;
void setup(){
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// initialize the serial communications:
Serial.begin(9600);
}
void loop()
{
//Serial.println(s);
while(Serial.available() > 0)
{
char aChar = Serial.read();
if(aChar == '<')
{
// Start of packet marker read
index = 0;
inData[index] = '\0'; // Throw away any incomplete packet
started = true;
ended = false;
}
else if(aChar == '>')
{
// End of packet marker read
ended = true;
break; // Done reading serial data for now
}
else
{
if(index < 10) // Make sure there is room
{
inData[index] = aChar; // Add char to array
index++;
inData[index] = '\0'; // Add NULL to end
}
}
}
// When we get here, there is no more serial data to read,
// or we have read an end-of-packet marker
if(started && ended)
{
// We've seen both markers - do something with the data here
lcd.setCursor(0,0);
i = atoi (inData);
float s = (i * 0.3051851); //multiplying with calibration factor
float value = ( s / 1000 );
lcd.setCursor(1,0);
lcd.print(value,3); // print value after multiplying with calibration factor to LCD
lcd.setCursor(0,1 );
lcd.print(i); // Print raw ADC counts as recieved from transmitter
index = 0;
inData[index] = '\0';
started = false;
ended = false;
}
}
The receiving arduino do get the data through Xbee and it displays values perfectly on the LCD( Attached PIC). I also need to receive the data on a PC attached to the receiving arduino through its USB/Serial port.
When i try to use the serial monitor the display on LCD vanishes and the serial monitor displays garbled values. I thing the Serial.print(s) is sending back the data to the XBEE as both the DO and DI LED starts blinking on the XBEE SHIELD.