Go Down

Topic: Function not working with existing Code (Read 510 times) previous topic - next topic

JQ21

Hi I created a function called VOICE(){}, and its suppose to replay a recorded message at a specific time with an RTC. The message will replay just fine, but when I include the VOICE() function with the code I created for my touch screen, the touch screen no longer responds when I press on it.

Code: [Select]
    if(x == 0 && now.hour () == 18) {
tft.drawRect(45, 0, 40, 40, WHITE);
// VOICE();
         if (p.y > 800  &&  p.x > 600 && p.z > MINPRESSURE && p.z < MAXPRESSURE){
             Serial.println ("I'M ON A");
             tft.drawRect(45, 0, 40, 40, BLACK);
             delay(100);
             SERVO_1();
             x=1;}
             
             if (now.hour() == 19){
               x = 0;
             }
                     

    }


Above is the code I wrote, but I commented out the VOICE() function to show that the touch screen works when VOICE() is commented out, but stops working when it is included with the code. Is there a way to make the VOICE() function work until the touchscreen is pressed?

-Jackson Q

PeterH

There isn't enough information there to tell. Please post the whole sketch so we don't have to guess what the rest of your code is doing.
I only provide help via the forum - please do not contact me for private consultancy.

michinyon

Write another sketch,  which just tests the functionality of the voice() function,   without the complications
of dealing with the touch screen.

Then,  when you have tested it thoroughly,   add it to the touch screen sketch.

JQ21

The main tab is as follows


Code: [Select]
#include <TouchScreen.h>
#include <Servo.h>
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

#define button  50


#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library

#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0

#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

#ifndef USE_ADAFRUIT_SHIELD_PINOUT
#error "This sketch is intended for use with the TFT LCD Shield. Make sure that USE_ADAFRUIT_SHIELD_PINOUT is #defined in the Adafruit_TFTLCD.h library file."
#endif

// These are the pins for the shield!
#define YP A1  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 7   // can be a digital pin
#define XP 6   // can be a digital pin

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define BLACK   0x0000
#define WHITE   0xFFFF

int oldbutton, currentbutton;

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;


void setup(void) {

   
Serial.begin(57600);

SERVO_SETUP();

  RTC_SETUP();

TFT_SETUP();

}

#define MINPRESSURE 5
#define MAXPRESSURE 1000

void loop(void) {
  DateTime now = RTC.now();
 
  RTC_RUN();

  Point p = ts.getPoint();
 

  // if sharing pins, you'll need to fix the directions of the touchscreen pins
  //pinMode(XP, OUTPUT);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  //pinMode(YM, OUTPUT);

  // we have some minimum pressure we consider 'valid'
  // pressure of 0 means no pressing!


 
  //  Serial.print("X = "); Serial.print(p.x);
//   Serial.print("\tY = "); Serial.print(p.y);
//   Serial.print("\tPressure = "); Serial.println(p.z);

    if(a == 0 && now.hour () == 18) {
tft.drawRect(45, 0, 40, 40, WHITE);
// VOICE();
         if (p.y > 800  &&  p.x > 600 && p.z > MINPRESSURE && p.z < MAXPRESSURE){
             Serial.println ("I'M ON A");
             tft.drawRect(45, 0, 40, 40, BLACK);
             delay(100);
             SERVO_1();
             a=1;}
             
             if (now.hour() == 19){
               a = 0;
             }
                     

    }
   
        if(b == 0 && now.hour () == 18) {
tft.drawRect(45, 70, 40, 40, WHITE);
// VOICE();
         if (p.y <= 800 && p.y >= 700  &&  p.x > 600 && p.z > MINPRESSURE && p.z < MAXPRESSURE){
             Serial.println ("I'M ON B");
             tft.drawRect(45, 70, 40, 40, BLACK);
             delay(100);
             SERVO_2();
             b=1;}
             
             if (now.hour() == 19){
               b = 0;
             }
                     

    }
   
     if(c == 0 && now.hour () == 18) {
tft.drawRect(45, 140, 40, 40, WHITE);
// VOICE();
         if (p.y <= 700 && p.y >= 500  &&  p.x > 600 && p.z > MINPRESSURE && p.z < MAXPRESSURE){
             Serial.println ("I'M ON C");
             tft.drawRect(45, 140, 40, 40, BLACK);
             delay(100);
             SERVO_3();
             c=1;}
             
             if (now.hour() == 19){
               c = 0;
             }
                     

    }                           
   
      if(d == 0 && now.hour () == 18) {
tft.drawRect(45, 210, 40, 40, WHITE);
// VOICE();
         if (p.y <= 500 && p.y >= 300  &&  p.x > 600 && p.z > MINPRESSURE && p.z < MAXPRESSURE){
             Serial.println ("I'M ON D");
             tft.drawRect(45, 210, 40, 40, BLACK);
             delay(100);
             SERVO_4();
             d=1;}
             
             if (now.hour() == 19){
               d = 0;
             }                   

    }
                                       
       if(e == 0 && now.hour () == 18) {
tft.drawRect(45, 280, 40, 40, WHITE);
// VOICE();
         if (p.y <= 300 && p.y >= 100  &&  p.x > 600 && p.z > MINPRESSURE && p.z < MAXPRESSURE){
             Serial.println ("I'M ON E");
             tft.drawRect(45, 280, 40, 40, BLACK);
             delay(100);
             SERVO_5();
             e=1;}
             
             if (now.hour() == 19){
               e = 0;
             }
                     

    }
 

  }     



unsigned long testText() {
  tft.fillScreen(BLACK);
  unsigned long start = micros();
 
A_White();
   
B_Yellow();
   
C_Red();
 
D_Green();
   
E_Magenta();

  return micros() - start;
}

// Copy string from flash to serial port
// Source string MUST be inside a PSTR() declaration!
void progmemPrint(const char *str) {
  char c;
  while(c = pgm_read_byte(str++)) Serial.print(c);
}

// Same as above, with trailing newline
void progmemPrintln(const char *str) {
  progmemPrint(str);
  Serial.println();
}


The code for the function voice() is

Code: [Select]
int PULSE = 48; // pulse for voice recorder


void VOICE() {
pinMode(48, OUTPUT);
   digitalWrite(PULSE,0);
   delay(5000);
   digitalWrite(PULSE,1);
   
}



F.Y.I, I'm using an Arduino MEGA 2560 and the main code was made by altering the sample tutorial from Adafruit for their TFT touch screen. If there are any unnecessary lines, please feel free to address them. Thanks  :)

-JQ

mistergreen

Code: [Select]
void VOICE() {
pinMode(48, OUTPUT);
  digitalWrite(PULSE,0);
  delay(5000);
  digitalWrite(PULSE,1);
 
}


probably the delay(5000). It'll make the touchscreen unresponsive. You'll need something like the blink without delay script.
http://arduino.cc/en/Tutorial/BlinkWithoutDelay

Go Up