Convert an arduino library for touchscreen to a general c library - arduino

I have been using the adafruit 3.5'' tft touchscreen break out board with arduino. It has arduino library available for this purpose. I have attached the library below. I am trying to make this touchscreen work without the arduino board with atmega16 . I have changed the library into a c file accordingly. I have written a program to just change the screen color, but that also in not happening. I do not understand where is the problem. can some one help me out. pls !!!
This the code after editing in c file .
#define SS PINB4
#define MOSI PINB5
#define MISO PINB6
#define SCK PINB7
#define CS PIND0
#define DC PIND1
#define RST PIND6
void delay(uint8_t a)
{
unsigned int i,j;
for(i=0;i<a;i++)
{
for(j=0;j<1024;j++);
}
}
void Adafruit_HX8357() {
_cs = CS;
_dc = DC;
_rst = RST;
_mosi = MOSI;
_sck = SCK;
}
void Adafruit_GFX(int16_t w, int16_t h)
{
_width = w;
_height = h;
rotation = 0;
cursor_y = cursor_x = 0;
textsize = 1;
textcolor = textbgcolor = 0x001F;
}
void setRotation(uint8_t m) {
writecommand(HX8357_MADCTL);
rotation = m % 4; // can't be higher than 3
switch (rotation) {
case 0:
writedata(MADCTL_MX | MADCTL_MY | MADCTL_RGB);
_width = HX8357_TFTWIDTH;
_height = HX8357_TFTHEIGHT;
break;
case 1:
writedata(MADCTL_MV | MADCTL_MY | MADCTL_RGB);
_width = HX8357_TFTHEIGHT;
_height = HX8357_TFTWIDTH;
break;
case 2:
writedata( MADCTL_RGB);
_width = HX8357_TFTWIDTH;
_height = HX8357_TFTHEIGHT;
break;
case 3:
writedata(MADCTL_MX | MADCTL_MV | MADCTL_RGB);
_width = HX8357_TFTHEIGHT;
_height = HX8357_TFTWIDTH;
break;
}
}
void writecommand(uint8_t c) {
PORTD &= ~(1<<PIND1);//
//dcport &= ~dcpinmask;
//digitalWrite(_dc, LOW);
PORTB &= ~(1<<PINB7);//clkport &= ~clkpinmask;
//digitalWrite(_sclk, LOW);
PORTD &= ~(1<<PIND0);//*csport &= ~cspinmask;
//digitalWrite(_cs, LOW);
spiwrite(c);
//Serial.print("Command 0x"); Serial.println(c, HEX);
PORTD |= (1<<PIND0);//*csport |= cspinmask;
//digitalWrite(_cs, HIGH);
}
void writedata(uint8_t c) {
PORTD |= (1<<PIND1);//
//dcport |= dcpinmask;
//digitalWrite(_dc, HIGH);
PORTB &= ~(1<<PINB7);//clkport &= ~clkpinmask;
//digitalWrite(_sclk, LOW);
PORTD &= ~(1<<PIND0);// *csport &= ~cspinmask;
//digitalWrite(_cs, LOW);
spiwrite(c);
//Serial.print("Data 0x"); Serial.println(c, HEX);
//digitalWrite(_cs, HIGH);
PORTD |= (1<<PIND0);//*csport |= cspinmask;
}
void spiwrite(uint8_t c) {
SPDR = c;
while(!(SPSR & (1<<SPIF) ));
}
void init()
{
DDRB = (1<<PINB5)|(1<<PINB7);
SPCR = 0x50;
SPSR = 0x00;
}
void begins(uint8_t type) {
if (_rst > 0) {
DDRD |= (1<<PIND6);//pinMode(_rst, OUTPUT);
_rst = 0;// reset LOW
}
DDRD |= (1<< PIND1);//pinMode(_dc, OUTPUT);
DDRD |= (1<< PIND0);//pinMode(_cs, OUTPUT);
// toggle RST low to reset
if (_rst > 0) {
_rst = 1;//digitalWrite(_rst, HIGH);
delay(100);
_rst = 0;//digitalWrite(_rst, LOW);
delay(100);
_rst = 1;//digitalWrite(_rst, HIGH);
delay(150);
}
writecommand(HX8357_SWRESET);
// setextc
writecommand(HX8357D_SETC);
writedata(0xFF);
writedata(0x83);
writedata(0x57);
delay(300);
// setRGB which also enables SDO
writecommand(HX8357_SETRGB);
writedata(0x80); //enable SDO pin!
// writedata(0x00); //disable SDO pin!
writedata(0x0);
writedata(0x06);
writedata(0x06);
writecommand(HX8357D_SETCOM);
writedata(0x25); // -1.52V
writecommand(HX8357_SETOSC);
writedata(0x68); // Normal mode 70Hz, Idle mode 55 Hz
writecommand(HX8357_SETPANEL); //Set Panel
writedata(0x05); // BGR, Gate direction swapped
writecommand(HX8357_SETPWR1);
writedata(0x00); // Not deep standby
writedata(0x15); //BT
writedata(0x1C); //VSPR
writedata(0x1C); //VSNR
writedata(0x83); //AP
writedata(0xAA); //FS
writecommand(HX8357D_SETSTBA);
writedata(0x50); //OPON normal
writedata(0x50); //OPON idle
writedata(0x01); //STBA
writedata(0x3C); //STBA
writedata(0x1E); //STBA
writedata(0x08); //GEN
writecommand(HX8357D_SETCYC);
writedata(0x02); //NW 0x02
writedata(0x40); //RTN
writedata(0x00); //DIV
writedata(0x2A); //DUM
writedata(0x2A); //DUM
writedata(0x0D); //GDON
writedata(0x78); //GDOFF
writecommand(HX8357D_SETGAMMA);
writedata(0x02);
writedata(0x0A);
writedata(0x11);
writedata(0x1d);
writedata(0x23);
writedata(0x35);
writedata(0x41);
writedata(0x4b);
writedata(0x4b);
writedata(0x42);
writedata(0x3A);
writedata(0x27);
writedata(0x1B);
writedata(0x08);
writedata(0x09);
writedata(0x03);
writedata(0x02);
writedata(0x0A);
writedata(0x11);
writedata(0x1d);
writedata(0x23);
writedata(0x35);
writedata(0x41);
writedata(0x4b);
writedata(0x4b);
writedata(0x42);
writedata(0x3A);
writedata(0x27);
writedata(0x1B);
writedata(0x08);
writedata(0x09);
writedata(0x03);
writedata(0x00);
writedata(0x01);
writecommand(HX8357_COLMOD);
writedata(0x55); // 16 bit
writecommand(HX8357_MADCTL);
writedata(0xC0);
writecommand(HX8357_TEON); // TE off
writedata(0x00);
writecommand(HX8357_TEARLINE); // tear line
writedata(0x00);
writedata(0x02);
writecommand(HX8357_SLPOUT); //Exit Sleep
delay(150);
writecommand(HX8357_DISPON); // display on
delay(50);
}
void drawPixel(int16_t x, int16_t y, uint16_t color) {
if((x < 0) ||(x >= _width) || (y < 0) || (y >= _height)) return;
setAddrWindow(x,y,x+1,y+1);
//digitalWrite(_dc, HIGH);
PORTD |= (1<<PIND1);// dcport |= dcpinmask;
//digitalWrite(_cs, LOW);
PORTD &= ~(1<<PIND0);//csport &= ~cspinmask;
spiwrite(color >> 8);
spiwrite(color);
PORTD |= (1<<PIND0);//*csport |= cspinmask;
//digitalWrite(_cs, HIGH);
}
void drawFastVLine(int16_t x, int16_t y, int16_t h,uint16_t color) {
// Rudimentary clipping
if((x >= _width) || (y >= _height)) return;
if((y+h-1) >= _height)
h = _height-y;
setAddrWindow(x, y, x, y+h-1);
uint8_t hi = color >> 8, lo = color;
PORTD |= (1<<PIND1);//*dcport |= dcpinmask;
//digitalWrite(_dc, HIGH);
PORTD &= ~(1<<PIND0);//*csport &= ~cspinmask;
//digitalWrite(_cs, LOW);
while (h--) {
spiwrite(hi);
spiwrite(lo);
}
PORTD |= (1<<PIND0);//*csport |= cspinmask;
//digitalWrite(_cs, HIGH);
}
void drawRect(int16_t x, int16_t y, int16_t w, int16_t h,uint16_t color){
drawFastHLine(x, y, w, color);
drawFastHLine(x, y+h-1, w, color);
drawFastVLine(x, y, h, color);
drawFastVLine(x+w-1, y, h, color);
}
void drawFastHLine(int16_t x, int16_t y, int16_t w,uint16_t color) {
// Rudimentary clipping
if((x >= _width) || (y >= _height)) return;
if((x+w-1) >= _width) w = _width-x;
setAddrWindow(x, y, x+w-1, y);
uint8_t hi = color >> 8, lo = color;
PORTD |= (1<<PIND1);//*dcport |= dcpinmask;
PORTD &= ~(1<<PIND0);//*csport &= ~cspinmask;
//digitalWrite(_dc, HIGH);
//digitalWrite(_cs, LOW);
while (w--) {
spiwrite(hi);
spiwrite(lo);
}
PORTD |= (1<<PIND0);//*csport |= cspinmask;
//digitalWrite(_cs, HIGH);
}
void fillScreen(uint16_t color) {
drawRect(0, 0, _width, _height, color);
}
void setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1,uint16_t y1)
{
writecommand(HX8357_CASET); // Column addr set
writedata(x0 >> 8);
writedata(x0 & 0xFF); // XSTART
writedata(x1 >> 8);
writedata(x1 & 0xFF); // XEND
writecommand(HX8357_PASET); // Row addr set
writedata(y0>>8);
writedata(y0); // YSTART
writedata(y1>>8);
writedata(y1); // YEND
writecommand(HX8357_RAMWR); // write to RAM
}
void setCursor(int16_t x, int16_t y) {
cursor_x = x;
cursor_y = y;
}
void setTextSize(uint8_t s) {
textsize = (s > 0) ? s : 1;
}
void setTextColor(uint16_t c) {
textcolor = textbgcolor = c;
}
int main(void)
{
init();
Adafruit_HX8357();
begins(HX8357D);
Adafruit_GFX(HX8357_TFTWIDTH, HX8357_TFTHEIGHT);
setRotation(3);
fillScreen(HX8357_RED);
return 0;
}
The link for original library is
https://github.com/adafruit/Adafruit_HX8357_Library
https://github.com/adafruit/Adafruit-GFX-Library

Related

Reading temperature values from TMP117 sensor using MSP430FR5969 MCU

I'm trying to read temperature values from the TMP117 sensor [1] connected to an MSP430FR5969 MCU [2] through the I2C protocol. The data in the result register of the sensor is in two's complement format, has a data width of 16 bits, and a resolution of 0.0078125 °C. I used Code Composer Studio to program the MCU and I've attached the code below:
#include <msp430.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#define SLAVE_ADDR 0x48
#define CONVERSION_READY 0x10
#define TMP117_TEMP_REG 0x00
#define TMP117_CONFIG_REG 0x01
#define TMP117_RESOLUTION 0.0078125f
#define CMD_TYPE_0_SLAVE 0
#define CMD_TYPE_1_SLAVE 1
#define CMD_TYPE_2_SLAVE 2
#define CMD_TYPE_0_MASTER 3
#define CMD_TYPE_1_MASTER 4
#define CMD_TYPE_2_MASTER 5
#define TYPE_0_LENGTH 1
#define TYPE_1_LENGTH 2
#define TYPE_2_LENGTH 6
#define MAX_BUFFER_SIZE 20
char temperature[] = "Temperature is: ";
char dot[] = ".";
char celcuis[] = " degree Celcius\r\n";
int i;
char text[] = " I am an MSP430FR5969\r\n";
char rx_char[5];
volatile int rx_val = 0;
void clockSetup();
void gpioSetup();
void uartSetup();
void i2cSetup();
void ser_output(char *str);
uint8_t MasterType1 [TYPE_1_LENGTH] = {0x02, 0x20};
uint8_t SlaveType1 [TYPE_1_LENGTH] = {0};
typedef enum I2C_ModeEnum{
IDLE_MODE,
NACK_MODE,
TX_REG_ADDRESS_MODE,
RX_REG_ADDRESS_MODE,
TX_DATA_MODE,
RX_DATA_MODE,
SWITCH_TO_RX_MODE,
SWITHC_TO_TX_MODE,
TIMEOUT_MODE
} I2C_Mode;
I2C_Mode MasterMode = IDLE_MODE;
/* The Register Address/Command to use*/
uint8_t TransmitRegAddr = 0;
uint8_t ReceiveBuffer[MAX_BUFFER_SIZE] = {0};
uint8_t RXByteCtr = 0;
uint8_t ReceiveIndex = 0;
uint8_t TransmitBuffer[MAX_BUFFER_SIZE] = {0};
uint8_t TXByteCtr = 0;
uint8_t TransmitIndex = 0;
I2C_Mode I2C_Master_WriteReg(uint8_t dev_addr, uint8_t reg_addr, uint8_t *reg_data, uint8_t count);
I2C_Mode I2C_Master_ReadReg(uint8_t dev_addr, uint8_t reg_addr, uint8_t count);
void CopyArray(uint8_t *source, uint8_t *dest, uint8_t count);
I2C_Mode I2C_Master_ReadReg(uint8_t dev_addr, uint8_t reg_addr, uint8_t count)
{
MasterMode = TX_REG_ADDRESS_MODE;
TransmitRegAddr = reg_addr;
RXByteCtr = count;
TXByteCtr = 0;
ReceiveIndex = 0;
TransmitIndex = 0;
UCB0I2CSA = dev_addr;
UCB0IFG &= ~(UCTXIFG + UCRXIFG);
UCB0IE &= ~UCRXIE;
UCB0IE |= UCTXIE;
UCB0CTLW0 |= UCTR + UCTXSTT;
__bis_SR_register(LPM0_bits + GIE);
return MasterMode;
}
I2C_Mode I2C_Master_WriteReg(uint8_t dev_addr, uint8_t reg_addr, uint8_t *reg_data, uint8_t count)
{
MasterMode = TX_REG_ADDRESS_MODE;
TransmitRegAddr = reg_addr;
CopyArray(reg_data, TransmitBuffer, count);
TXByteCtr = count;
RXByteCtr = 0;
ReceiveIndex = 0;
TransmitIndex = 0;
UCB0I2CSA = dev_addr;
UCB0IFG &= ~(UCTXIFG + UCRXIFG);
UCB0IE &= ~UCRXIE;
UCB0IE |= UCTXIE;
UCB0CTLW0 |= UCTR + UCTXSTT;
__bis_SR_register(LPM0_bits + GIE);
return MasterMode;
}
void CopyArray(uint8_t *source, uint8_t *dest, uint8_t count)
{
uint8_t copyIndex = 0;
for (copyIndex = 0; copyIndex < count; copyIndex++)
{
dest[copyIndex] = source[copyIndex];
}
}
void main(void)
{
WDTCTL = WDTPW | WDTHOLD;
clockSetup();
gpioSetup();
uartSetup();
i2cSetup();
__bis_SR_register(GIE);
while (1)
{
_delay_cycles(500000);
I2C_Master_ReadReg(SLAVE_ADDR, 0x00, TYPE_1_LENGTH);
CopyArray(ReceiveBuffer, SlaveType1, TYPE_1_LENGTH);
if(ReceiveBuffer[1] & CONVERSION_READY)
{
I2C_Master_ReadReg(SLAVE_ADDR, 0x00, TYPE_1_LENGTH);
CopyArray(ReceiveBuffer, SlaveType1, TYPE_1_LENGTH);
_no_operation();
}
ltoa(rx_val, rx_char, 10);
ser_output(temperature);
ser_output(rx_char);
ser_output(dot);
ser_output(celcuis);
}
}
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector = USCI_B0_VECTOR
__interrupt void USCI_B0_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(USCI_B0_VECTOR))) USCI_B0_ISR (void)
#else
#error Compiler not supported!
#endif
{
//Must read from UCB0RXBUF
//// uint8_t rx_val = 0;
switch(__even_in_range(UCB0IV, USCI_I2C_UCBIT9IFG))
{
case USCI_NONE: break; // Vector 0: No interrupts
case USCI_I2C_UCALIFG: break; // Vector 2: ALIFG
case USCI_I2C_UCNACKIFG: // Vector 4: NACKIFG
break;
case USCI_I2C_UCSTTIFG: break; // Vector 6: STTIFG
case USCI_I2C_UCSTPIFG: break; // Vector 8: STPIFG
case USCI_I2C_UCRXIFG3: break; // Vector 10: RXIFG3
case USCI_I2C_UCTXIFG3: break; // Vector 12: TXIFG3
case USCI_I2C_UCRXIFG2: break; // Vector 14: RXIFG2
case USCI_I2C_UCTXIFG2: break; // Vector 16: TXIFG2
case USCI_I2C_UCRXIFG1: break; // Vector 18: RXIFG1
case USCI_I2C_UCTXIFG1: break; // Vector 20: TXIFG1
case USCI_I2C_UCRXIFG0: // Vector 22: RXIFG0
rx_val = UCB0RXBUF;
if (RXByteCtr)
{
ReceiveBuffer[ReceiveIndex++] = rx_val;
RXByteCtr--;
}
if (RXByteCtr == 1)
{
UCB0CTLW0 |= UCTXSTP;
}
else if (RXByteCtr == 0)
{
UCB0IE &= ~UCRXIE;
MasterMode = IDLE_MODE;
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
break;
case USCI_I2C_UCTXIFG0: // Vector 24: TXIFG0
switch (MasterMode)
{
case TX_REG_ADDRESS_MODE:
UCB0TXBUF = TransmitRegAddr;
if (RXByteCtr)
MasterMode = SWITCH_TO_RX_MODE; // Need to start receiving now
else
MasterMode = TX_DATA_MODE; // Continue to transmision with the data in Transmit Buffer
break;
case SWITCH_TO_RX_MODE:
UCB0IE |= UCRXIE; // Enable RX interrupt
UCB0IE &= ~UCTXIE; // Disable TX interrupt
UCB0CTLW0 &= ~UCTR; // Switch to receiver
MasterMode = RX_DATA_MODE; // State state is to receive data
UCB0CTLW0 |= UCTXSTT; // Send repeated start
if (RXByteCtr == 1)
{
//Must send stop since this is the N-1 byte
while((UCB0CTLW0 & UCTXSTT));
UCB0CTLW0 |= UCTXSTP; // Send stop condition
}
break;
case TX_DATA_MODE:
if (TXByteCtr)
{
UCB0TXBUF = TransmitBuffer[TransmitIndex++];
TXByteCtr--;
}
else
{
//Done with transmission
UCB0CTLW0 |= UCTXSTP; // Send stop condition
MasterMode = IDLE_MODE;
UCB0IE &= ~UCTXIE; // disable TX interrupt
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
break;
default:
__no_operation();
break;
}
break;
default: break;
}
}
void ser_output(char *str)
{
while(*str !=0)
{
while(!(UCA0IFG&UCTXIFG));
UCA0TXBUF = *str++;
}
}
void gpioSetup()
{
P1OUT &= ~BIT0; // Clear P1.0 output latch
P1DIR |= BIT0; // For LED
P1SEL1 |= BIT6 | BIT7; // I2C pins
P2SEL1 |= BIT0 | BIT1; // USCI_A0 UART operation
P2SEL0 &= ~(BIT0 | BIT1);
PM5CTL0 &= ~LOCKLPM5;
}
void clockSetup()
{
FRCTL0 = FRCTLPW | NWAITS_1;
CSCTL0_H = CSKEY >> 8;
CSCTL1 = DCORSEL | DCOFSEL_0;
CSCTL2 = SELA__VLOCLK | SELS__DCOCLK | SELM__DCOCLK;
CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1;
CSCTL0_H = 0;
}
void uartSetup()
{
UCA0CTL1 |= UCSWRST;
UCA0CTL1 |= UCSSEL_2;
UCA0BR0 = 6;
UCA0BR1 = 0;
UCA0MCTLW = 0xAA;
UCA0MCTLW |= UCOS16 | UCBRF_1;
UCA0CTL1 &= ~UCSWRST;
UCA0IE |= UCRXIE;
}
void i2cSetup()
{
UCB0CTLW0 |= UCSWRST;
UCB0CTLW0 |= UCMODE_3 | UCMST | UCSSEL__SMCLK | UCSYNC;
UCB0CTLW1 |= UCSSEL_2;
UCB0BRW = 10;
UCB0I2CSA = 0x0048;
UCB0CTLW0 &= ~UCSWRST;
UCB0IE |= UCRXIE | UCNACKIE | UCBCNTIE;
}
I get the following data:
Temperature values in ReceviedBuffer, and transmitted value to PuTTY terminal
(i) My first question is that why I'm getting the RecevieBuffer1 values on the terminal and the missing value of the RecevieBuffer[0].
(ii) My second question is that how would I convert the 16-bit raw data to value in Celsius.
Many thanks
To get a 16-bit value from your buffer of bytes you need to cast each byte up to 16-bit values, shift the second byte by 8-bits, and OR the two together. In one line it would look like this:
int16_t value = int16_t(buffer[0]) | int16_t(buffer[1]) << 8;
Based on the value you are reading of 0x0aaf, I would guess the temperature sensor you are using probably outputs the value in in celsius*100 to keep it an integer and still have 100th of degree precision. So to get the value in celsius you'd have to cast to a float or double and divide by 100.0. So 0x0aaf is 2735, meaning 27.35 degrees C.

Arduino lcd screen showing broken characters at random times

So I've been working on an arduino project for an escape room puzzle. It's essentially a bomb with a timer on an lcd screen. After all of the elements of the bomb are solved the timer stops and the lcd screen displays that the bomb has been defused and gives the group a clue to the next puzzle. 9 times out of 10 it works perfectly. But every once in a while when it is supposed to display that the bomb is defused the lcd screen just shows random broken characters. I haven't had any luck diagnosing the problem. Hoping somebody here might have an idea.
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Tone.h>
#define pound 14
Tone tone1;
int Scount = 0;
int Mcount = 0;
int Hcount = 1;
int DefuseTimer = 0;
long secMillis = 0;
long interval = 1000;
char password[6] = "594432";
int currentLength = 0;
int i = 0;
char entered[6];
int ledPin = 23;
int ledPin2 = 25;
int ledPin3 = 27;
int ledPin4 = 29;
int ledPin5 = 31;
int ledPin6 = 34;
const int plugin1 = 44;
const int plugin2 = 46;
const int plugin3 = 48;
const int plugin4 = 50;
const int plugin5 = 52;
int plugin1State = 0;
int plugin2State = 0;
int plugin3State = 0;
int plugin4State = 0;
int plugin5State = 0;
const int switch1 = 37;
int switch1State = 0;
const int key1 = 40;
int key1State = 0;
int puzzle1 = 0;
int puzzle2 = 0;
int puzzle3 = 0;
int solved = 0;
LiquidCrystal lcd(7, 8, 10, 11, 12, 13);
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3};
byte colPins[COLS] = {A4, A5, A6};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(plugin1, INPUT);
pinMode(plugin2, INPUT);
pinMode(plugin3, INPUT);
pinMode(plugin4, INPUT);
pinMode(plugin5, INPUT);
digitalWrite(plugin1, HIGH);
digitalWrite(plugin2, HIGH);
digitalWrite(plugin3, HIGH);
digitalWrite(plugin4, HIGH);
digitalWrite(plugin5, HIGH);
pinMode(switch1, INPUT);
digitalWrite(switch1, HIGH);
pinMode(key1, INPUT_PULLUP);
digitalWrite(key1, HIGH);
tone1.begin(9);
lcd.begin(16, 2);
Serial.begin(9600);
lcd.clear();
lcd.setCursor(0, 0);
tone1.play(NOTE_E6, 200);
delay(3000);
lcd.clear();
currentLength = 0;
}
void loop()
{
timer();
plugin1State = digitalRead(plugin1);
plugin2State = digitalRead(plugin2);
plugin3State = digitalRead(plugin3);
plugin4State = digitalRead(plugin4);
plugin5State = digitalRead(plugin5);
if (plugin1State == LOW && plugin2State == LOW && plugin3State == LOW && plugin4State == LOW && plugin5State == LOW)
{
puzzle1 = 1;
}
if (puzzle1 == 1)
{
digitalWrite(ledPin4, HIGH);
switch1State = digitalRead(switch1);
if (switch1State == LOW)
{
puzzle2 = 1;
}
}
if (puzzle2 == 1)
{
digitalWrite(ledPin5, HIGH);
key1State = digitalRead(key1);
if (key1State == LOW)
{
puzzle3 = 1;
}
if (key1State == HIGH)
{
digitalWrite(ledPin6, LOW);
}
}
if (puzzle3 == 1)
{
digitalWrite(ledPin6, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Code: ");
while (currentLength < 6)
{
timer();
char key2 = keypad.getKey();
if (key2 == "#")
{
currentLength = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Code: ");
}
else if (key2 != NO_KEY)
{
lcd.setCursor(currentLength + 7, 0);
lcd.cursor();
lcd.print(key2);
entered[currentLength] = key2;
currentLength++;
tone1.play(NOTE_C6, 200);
delay(100);
lcd.noCursor();
lcd.setCursor(currentLength + 6, 0);
lcd.print("*");
lcd.setCursor(currentLength + 7, 0);
lcd.cursor();
}
}
if (currentLength == 6)
{
if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3] && entered[4] == password[4] && entered[5] == password[5])
{
solved = 1;
while (solved == 1)
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("BOMB 1 DEFUSED");
currentLength = 0;
digitalWrite(ledPin3, HIGH);
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("RELEASE");
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("TOXIC GAS");
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("CLUE: %&#$#");
delay(6000);
}
}
else
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("Wrong Password!");
delay(1500);
currentLength = 0;
}
}
}
}
void timer()
{
if (Hcount <= 0)
{
if ( Mcount < 0 )
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("The Bomb Has ");
lcd.setCursor (0, 1);
lcd.print("Exploded!");
while (Mcount < 0)
{
digitalWrite(ledPin, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin2, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin2, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin3, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin3, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
}
}
}
lcd.setCursor (0, 1); // sets cursor to 2nd line
lcd.print ("Timer:");
if (Hcount >= 10)
{
lcd.setCursor (7, 1);
lcd.print (Hcount);
}
if (Hcount < 10)
{
lcd.setCursor (7, 1);
lcd.write ("0");
lcd.setCursor (8, 1);
lcd.print (Hcount);
}
lcd.print (":");
if (Mcount >= 10)
{
lcd.setCursor (10, 1);
lcd.print (Mcount);
}
if (Mcount < 10)
{
lcd.setCursor (10, 1);
lcd.write ("0");
lcd.setCursor (11, 1);
lcd.print (Mcount);
}
lcd.print (":");
if (Scount >= 10)
{
lcd.setCursor (13, 1);
lcd.print (Scount);
}
if (Scount < 10)
{
lcd.setCursor (13, 1);
lcd.write ("0");
lcd.setCursor (14, 1);
lcd.print (Scount);
}
if (Hcount < 0)
{
Hcount = 0;
}
if (Mcount < 0)
{
Hcount --;
Mcount = 59;
}
if (Scount < 1) // if 60 do this operation
{
Mcount --; // add 1 to Mcount
Scount = 59; // reset Scount
}
if (Scount > 0) // do this oper. 59 times
{
unsigned long currentMillis = millis();
if (currentMillis - secMillis > interval)
{
tone1.play(NOTE_G5, 200);
secMillis = currentMillis;
Scount --; // add 1 to Scount
digitalWrite(ledPin2, HIGH); // sets the LED on
delay(10); // waits for a second
digitalWrite(ledPin2, LOW); // sets the LED off
delay(10); // waits for a second
//lcd.clear();
}
}
}
That would most likely be a an electrical issues either with your circuit of your LCD. The fact that a simple code like yours (not trying to insult you in any way) works 9/10 of times means that there is probably nothing wrong with the code (I've seen none).
For quick check, try reinstalling the IDE. That might update the Arduino libraries that get downloaded with the IDE. I doubt that it would solve the issue but that is an quick free easy way to try
I would personnaly suggest to disconnect everything, and reconnecting them. And if it doesn't work, then return that LCD and get yourself a new one.
I had the same issue when I used a non shielded 1m ribbon cable for the 16x2 LCD. The main board with the MCU had to be placed further away from the display. It showed random character for any electromagnetic interference. For example, turning on a fluorescent tube, starting an electric screwdriver, relay switches etc. Shortening the ribon cable solved the problem.
Anyway, the LCD is sensitive to electromagnetic interference. Keep it away from these sources.

Arduino-4X4 matrix keypad with I/O shift register

I need help. I have done some research and my little understanding of keypad scanning is that the ShiftIn value of Input Column should return zero (0) when a keypad button is pressed. Mine is only returning 255 (or 11111111) in BIN. All I need is to track the zero value when a key is pressed and then scan the keys matrix to display the pressed key. I will appreciate any help. I have added my code and schematic.
]1
const int kbdRows = 4;
const int kbdCols = 4;
int LatchIn = 2; //165 pin1
int ClockPin = 3; // 595 pin11 & 165 pin2
int DataIn = 4; //165 pin9
int LatchOut = 5; // 595 pin12
int DataOut = 6; //595 pin14
int led = 7;
int PinState = 0;
char keys[kbdRows][kbdCols] = {
{ '1','2','3','4' },
{ '5','6','7','8' },
{ '9','0','A','B' },
{ 'C','D','E','F' }
};
byte KeyIsDown() {
int row;
int col;
int rowBits;
int colBits;
rowBits = 0X10;
for (row = 0; row < kbdRows; row++) {
digitalWrite(ClockPin, LOW);
digitalWrite(LatchOut, LOW);
shiftOut(DataOut, ClockPin, LSBFIRST, rowBits);
digitalWrite(LatchOut, HIGH);
delay(5);
digitalWrite(ClockPin, HIGH);
digitalWrite(LatchIn, LOW);
delay(5);
digitalWrite(LatchIn, HIGH);
colBits = shiftIn(DataIn, ClockPin, LSBFIRST);
for (col = 0; col < kbdCols; col++) {
if (colBits==0) {
// not sure what condition to put here
byte keypressed = keys[kbdRows][kbdCols]; here
// I know this is the right stuff to return here
}
return colBits;
colBits = colBits >> 1;
}
rowBits = rowBits << 1;
}
}
void setup() {
pinMode(ClockPin, OUTPUT);
pinMode(DataOut, OUTPUT);
pinMode(DataIn, INPUT_PULLUP);
pinMode(LatchOut, OUTPUT);
pinMode(LatchIn, OUTPUT);
digitalWrite(LatchOut, HIGH);
digitalWrite(LatchIn, HIGH);
Serial.begin(9600);
digitalWrite(led, HIGH);
}
void loop() {
byte retColBit = KeyIsDown();
Serial.print("ColBit: ");
Serial.println(retColBit,BIN);
delay(500);
PinState = digitalRead(DataOut);
Serial.print("DataOut: ");
Serial.println(PinState,BIN);
delay(500);
}

LED matrix (HUB-75 type)

I'm trying to make the 64x64 LED matrix work without libraries. Started driving it with STM32F103C8T6 chip figured out the problem and tried ATmega328 instead, but nothing changed.
The problem is when I'm trying to lit a LED on specified address it actually lights up with almost 0 brightness, but the LED directly under it (on the next address) lights up with full brightness.
I cut the code to a minimum.
#define CLK 0
#define LAT 1
#define R1 2
uint16_t data [64][4];
void shiftOutLed (void);
int main () {
data [0][0] = 0x0001;
data [0][2] = 0x1800;
DDRC |= 0x0F; //pin 0 - A, pin 1 - B, pin 2 - C, pin3 - D
DDRB |= 0x03; // pin 0 - CLK, pin 1 - LAT
DDRD |= 0x04; // pin 2 - R1
while (1){
shiftOutLed ();
}
}
void shiftOutLed (void){
for (uint8_t _addr = 0; _addr < 16; _addr++){
PORTC &= 0xF0;
PORTC |= _addr;
for (uint8_t _byte = 0; _byte < 4; _byte++){
for (uint8_t _bit = 0; _bit < 16; _bit++){
if (data[_addr][_byte] & (1 << _bit)) PORTD |= (1 << R1);
else PORTD &= ~(1 << R1);
PORTB |= (1 << CLK);
PORTB &= ~(1 << CLK);
}
}
PORTB |= (1 << LAT);
PORTB &= ~(1 << LAT);
PORTD &= ~(1 << R1);
}
}
When I modify the code introducing delays while clocking and latching every bit LEDs light up in right row.
Seems like the matrix needs the address set after clocking the entire row
#define CLK 0
#define LAT 1
#define R1 2
uint16_t data [64][4];
void shiftOutLed (void);
int main () {
data [0][0] = 0x0001;
data [0][2] = 0x1800;
DDRC |= 0x0F; //pin 0 - A, pin 1 - B, pin 2 - C, pin3 - D
DDRB |= 0x03; // pin 0 - CLK, pin 1 - LAT
DDRD |= 0x04; // pin 2 - R1
while (1){
shiftOutLed ();
}
}
void shiftOutLed (void){
for (uint8_t _addr = 0; _addr < 16; _addr++){
PORTC &= 0xF0;
for (uint8_t _byte = 0; _byte < 4; _byte++){
for (uint8_t _bit = 0; _bit < 16; _bit++){
if (data[_addr][_byte] & (1 << _bit)) PORTD |= (1 << R1);
else PORTD &= ~(1 << R1);
PORTB |= (1 << CLK);
PORTB &= ~(1 << CLK);
}
}
PORTC |= _addr;
PORTB |= (1 << LAT);
PORTB &= ~(1 << LAT);
PORTD &= ~(1 << R1);
}
}
Now it works

I m transmitting flex sensor values through serial port of arduino using Serial.print() function but I'm not able to read it at the receiving end

Actually in my code I'm transmitting accelerometer values as well as 4 flex sensor values. I have converted accelerometer value to 4 state as F(forward), B(backward),R(right),L(left). I am able to receive this 4 states and also I'm able to write code for these states. With these states I'm also sending flex sensor values to control servo motor remotely. But I'm not able to read those flex values as it is a varying integer. Please help me to read the flex values. I have tried Serial.read(), Serial.parseInt().
My transmitter code is:
int xpin = A0;
int x;
int ypin = A1;
int y;
int vcc=13;
const int flexpin1 = 2;
const int flexpin2 = 3;
const int flexpin3 = 4;
const int flexpin4 = 5;
int flex1[20];
int flex2[20];
int flex3[20];
int flex4[20];
int flexsum1=0;
int flexsum2=0;
int flexsum3=0;
int flexsum4=0;
char state1 = 'S';
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(flexpin1, INPUT);
pinMode(flexpin2, INPUT);
pinMode(flexpin3, INPUT);
pinMode(flexpin4, INPUT);
pinMode(xpin, INPUT);
pinMode(ypin, INPUT);
pinMode(vcc, OUTPUT);
digitalWrite(vcc, HIGH);
}
void loop() {
x = analogRead(xpin);
Serial.print("xpin=");
Serial.print(x);
y = analogRead(ypin);
Serial.print("\t ypin=");
Serial.print(y);
if ( y > 415)
{
state1 = 'F';
}
else if (y < 315)
{
state1 = 'B';
}
else if (x > 410)
{
state1 = 'R';
}
else if (x < 310)
{
state1 = 'L';
}
else {
state1 = 'S';
}
// flex readings stabelized//
for(int x=0; x<25; x++)
{
flex1[x]=analogRead(flexpin1);
flex2[x]=analogRead(flexpin2);
flex3[x]=analogRead(flexpin3);
flex4[x]=analogRead(flexpin4);
flexsum1=flexsum1+flex1[x];
flexsum2=flexsum2+flex2[x];
flexsum3=flexsum3+flex3[x];
flexsum4=flexsum4+flex4[x];
delayMicroseconds(20);
}
flexsum1=flexsum1/25;
flexsum2=flexsum2/25;
flexsum3=flexsum3/25;
flexsum4=flexsum4/25;
Serial.print("\t\t\tflexsum1= ");
Serial.print(flexsum1);
Serial.print("\tflexsum2= ");
Serial.print(flexsum2);
Serial.print("\tflexsum3= ");
Serial.print(flexsum3);
Serial.print("\tflexsum4= ");
Serial.print(flexsum4);
Serial.print("\t state1=");
Serial.println(state1);
delay(200);
}
My receiving code:
#include<Servo.h>
int IN1 = 13;
int IN2 = 12;
int IN3 = 7;
int IN4 = 6;
char state1 = 'S';
Servo servo1, servo2, servo3, servo4;
int flex1, flex2, flex3, flex4;
int angle1, angle2, angle3, angle4;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
servo1.attach(9);
servo2.attach(8);
servo3.attach(10);
servo4.attach(11);
}
void loop() {
// put your main code here, to run repeatedly:
while (Serial.available()) {
delay(10);
state1 = Serial.read();
Serial.print(state1);
switch (state1)
{
case 'F' : digitalWrite(IN1 , HIGH);
digitalWrite(IN2 , LOW);
digitalWrite(IN3 , HIGH);
digitalWrite(IN4 , LOW);
Serial.print("\tcar is moving forward ");
delay(100);
break;
case 'B' : digitalWrite(IN1 , LOW);
digitalWrite(IN2 , HIGH);
digitalWrite(IN3 , LOW);
digitalWrite(IN4 , HIGH);
Serial.print("\tcar is moving backward ");
delay(100);
break;
case 'L' : digitalWrite(IN1 , HIGH);
digitalWrite(IN2 , LOW);
digitalWrite(IN3 , LOW);
digitalWrite(IN4 , HIGH);
Serial.print("\tcar is turning left ");
delay(100);
break;
case 'R' : digitalWrite(IN1 , LOW);
digitalWrite(IN2 , HIGH);
digitalWrite(IN3 , HIGH);
digitalWrite(IN4 , LOW);
Serial.print("\tcar is turning right");
delay(100);
break;
case 'S' : digitalWrite(IN1 , LOW);
digitalWrite(IN2 , LOW);
digitalWrite(IN3 , LOW);
digitalWrite(IN4 , LOW);
delay(100);
break;
}
if (flex1 >= 845)
{
flex1 = Serial.read();
angle1 = map(flex1, 848, 1000, 0, 180);
angle1 = constrain(angle1, 0, 90);
servo1.write(angle1);
Serial.print("\tangle1=");
Serial.print(angle1);
delay(200);
}
if (flex2 >= 820)
{
flex2 = Serial.read();
angle2 = map(flex2, 825, 1000, 0, 180);
angle2 = constrain(angle2, 0, 90);
servo2.write(angle2);
Serial.print("\tangle2=");
Serial.print(angle2);
delay(200);
}
if (flex3 >= 770)
{
flex3 = Serial.read();
angle3 = map(flex3, 770, 930, 0, 180);
angle3 = constrain(angle3, 90, 180);
servo3.write(angle3);
Serial.print("\tangle3=");
Serial.print(angle3);
delay(200);
}
if (flex4 >= 870)
{
flex4 = Serial.read();
angle4 = map(flex4, 875, 1020, 0, 180);
angle4 = constrain(angle4, 90, 180);
servo4.write(angle4);
Serial.print("\tangle4=");
Serial.print(angle4);
delay(200);
}
}
}

Resources