I should have thought of creating a smaller test program myself. I was sure someone would say that it was totally obvious that I had a resistor wrong or a wire in the wrong spot or my tounge stuck out incorrectly, couldn't possibly be my code.
So anyway created a test program, but kept the TFT on my first downsize of code as I wanted to see whether all the components worked together with minimal code..
So
The switches (and PCF8574) work perfectly with the test program and the TFT works the RTC is read correctly, so it seems that its probably my software. Also works perfectly with my crappily soldered 5 pin keyboard.
Loaded up my large program and that needs the resistor to work correctly. grrrr
So I guess I am 99.5% convinced its not hardware, and I will now add my complex code in bit by bit till I break it again.
Here's the test code for your interest
I will let you know how I get on
thanks
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <UTFTGLUE.h>
#include <TimedAction.h>
#include <TouchScreen.h>
#include <PCF8574.h>
#include <Wire.h>
#include "RTClib.h"
int count = 0;
int count2 = 0;
int statePin = LOW;
int piezoPin = 10;
/* *********************************** tft display ******************************************************* */
MCUFRIEND_kbv tft;
UTFTGLUE myGLCD(0x9488, A2, A1, A3, A4, A0);
#if defined(__SAM3X8E__)
#und ef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
// most mcufriend shields use these pins and Portrait mode:
uint8_t YP = A1; // must be an analog pin, use "An" notation!
uint8_t XM = A2; // must be an analog pin, use "An" notation!
uint8_t YM = 7; // can be a digital pin
uint8_t XP = 6; // can be a digital pin
uint8_t SwapXY = 1; // this was orignally 0 but drawing was inversed
// screen 1 uses full screen touch is scewed on theat screen
//uint16_t TS_LEFT = 920;
//uint16_t TS_RT = 150;
//uint16_t TS_TOP = 940;
//uint16_t TS_BOT = 120;
// screen 2 made no difference whatsoever
uint16_t TS_LEFT = 946;
uint16_t TS_RT = 211;
uint16_t TS_TOP = 287;
uint16_t TS_BOT = 750;
char *name = "Unknown controller";
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
TSPoint tp;
//#define MINPRESSURE 20
//#define MAXPRESSURE 1000
#define SWAP(a, b) {uint16_t tmp = a; a = b; b = tmp;}
int16_t BOXSIZE;
int16_t PENRADIUS = 3;
uint16_t identifier, oldcolor, currentcolor;
uint8_t Orientation = 1; //0 is PORTRAIT
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// Declare which fonts we will be using
#if !defined(SmallFont)
extern uint8_t SmallFont[]; //.kbv GLUE defines as GFXFont ref
#endif
#if !defined(LargeFont)
extern uint8_t LargeFont[]; //.kbv GLUE defines as GFXFont ref
#endif
/* *********************************** RTC ******************************************************* */
RTC_DS1307 RTC;
/* ********************************* pcf 8574 ****************************************************************** */
#define LED_ON 0
#define LED_OFF 1
PCF8574 PCF_38(0x38); // add switches to lines (used as input)
PCF8574 PCF_39(0x39); // add leds to lines (used as output)
uint8_t expander = B00100000;
TimedAction timer = TimedAction(100, control_function);
/* *************************************************** */
void setup()
{
Serial.begin(9600);
PCF8574::PCF8574(expander);
get_rtc_time();
uint16_t tmp;
tft.begin(9600);
tft.reset();
identifier = tft.readID();
randomSeed(analogRead(5)); //.kbv Due does not like A0
pinMode(A0, OUTPUT); //.kbv mcufriend have RD on A0
digitalWrite(A0, HIGH);
// Setup the LCD
name = "ILI9488";
switch (Orientation) { // adjust for different aspects
case 0: break; //no change, calibrated for PORTRAIT
case 1: tmp = TS_LEFT, TS_LEFT = TS_BOT, TS_BOT = TS_RT, TS_RT = TS_TOP, TS_TOP = tmp; break;
case 2: SWAP(TS_LEFT, TS_RT); SWAP(TS_TOP, TS_BOT); break;
case 3: tmp = TS_LEFT, TS_LEFT = TS_TOP, TS_TOP = TS_RT, TS_RT = TS_BOT, TS_BOT = tmp; break;
}
// ts = TouchScreen(XP, YP, XM, YM, 300); //call the constructor AGAIN with new values.
tft.begin(identifier);
tft.setRotation(Orientation);
myGLCD.InitLCD();
myGLCD.setFont(LargeFont);
myGLCD.clrScr();
refresh_tft_display();
}
/**************************************/
void loop() {
timer.check();
}
/**************************************/
void control_function()
{
int val = 0;
for (int sw = 0; sw < 5; sw++)
{
val = PCF_38.read(sw); // get the value for pin i
// Serial.print(x);
// Serial.print("val");
// Serial.print(val);
// Serial.print(" ");
if (val == 0) // key is pressed
{
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(0, 0, 480, 120);
tft.setTextColor(GREEN);
tft.setTextSize(3);
tft.setCursor(100, 25);
tft.print("key ");
tft.print(String(sw));
tft.print(" was pressed");
soundAlarm();
}
}
refresh_tft_display();
}
/* *************************************************** */
void refresh_tft_display()
{
count++;
count2++;
if (count == 10)
{
count = 0;
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(0, 105, 480, 320);
tft.setTextSize(9);
tft.setTextColor(WHITE);
tft.setCursor(200, 125);
tft.print(String(count2/10));
}
}
/* *************************************************** */
void get_rtc_time()
{
Wire.begin();
RTC.begin();
if (!RTC.isrunning()) {
Serial.println("RTC is NOT running!");
}
else
{
Serial.println("clock is running");
DateTime now = RTC.now();
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
delay(1000);
Wire.end();
}
}
void soundAlarm() {
for (int j = 0; j < 10; j++)
{
float alarmFrequency = 100; // The value for the alarm tone in Hz
float period = (1.0 / alarmFrequency) * 10000000;
long beepDuration = 5000; // the time in microseconds (0.25 seconds)
long elapsedTime = 0;
while (elapsedTime < beepDuration) {
digitalWrite(piezoPin, HIGH);
delayMicroseconds(period / 4);
digitalWrite(piezoPin, LOW);
delayMicroseconds(period / 4);
elapsedTime += (period);
}
digitalWrite(piezoPin, LOW);
delayMicroseconds(beepDuration * 4);
}
}
AND attached is a pic of a new breadboard that's not so much spaghetti