Start the loop After pressing Button

Hello All,

firstly i need to explain my I2C setup:

Master(Arduino UNO) ->Slave (Arduino Mega 2560)+3.2" TFT

Master sends the X,Y,Z Values to the slave arduino using Nick Gammon’s I2C_Anything Library.

Master Program:
<#include <Wire.h>
#include <I2C_Anything.h>

const byte SLAVE_ADDRESS = 42;
float X=34.78984;
float Y-=-2.876;
float z=743.98;

void setup()
{
Wire.begin ();
} // end of setup

void loop()
{

Wire.beginTransmission (SLAVE_ADDRESS);
I2C_writeAnything (X);
I2C_writeAnything (Y);
I2C_writeAnything (Z);
Wire.endTransmission ();

delay (200);

} >

Slave program+3.2"TFT program(UTFT &URtouch):

<
#include <Wire.h>
#include <I2C_Anything.h>
const byte MY_ADDRESS = 42;
volatile boolean haveData = false;
volatile float X;
volatile float Y;
volatile float Z;
volatile char ch;

#include <UTFT.h>
#include <URTouch.h>

extern uint8_t BigFont;
extern uint8_t SmallFont;
int x,y;
#define BLACK 0x0000
#define RED 0xF800
#define GREEN 0x07E0
#define BLUE 0x001F
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define ORANGE 0xFD20
#define GREENa 0xAFE5
#define DARKGREEN 0x03E0
#define WHITE 0xFFFF
// Specific hardware
UTFT myGLCD(ILI9341_16,38,39,40,41);
URTouch myTouch(6,5,4,3,2);
int x21=0; int y21=75; int x22=93; int y22=94;
void setup() {
myGLCD.InitLCD(PORTRAIT);
myGLCD.clrScr();
myTouch.InitTouch(PORTRAIT);
myTouch.setPrecision(PREC_MEDIUM);
Wire.begin (MY_ADDRESS);
Serial.begin (115200);
Wire.onReceive (receiveEvent);
}

void loop() {
while (haveData)
{
Serial.print (ch);
Serial.print ("\t");
Serial.print (X,4);
Serial.print ("\t");
Serial.print (Y,4);
Serial.print ("\t");
Serial.print (Z,4);
Serial.println ("\t");
haveData = true;
}

readdata();
}

void receiveEvent (int howMany)
{
if (howMany >= (sizeof ch)+(sizeof X) + (sizeof Y)+(sizeof Z))
{
I2C_readAnything (ch);
I2C_readAnything (X);
I2C_readAnything (Y);
I2C_readAnything (Z);
haveData = true;
}

}

void readdata()
{ myGLCD.clrScr();
myGLCD.setFont(SmallFont);
myGLCD.setColor(BLUE); button(x21,y21,x22,y22);
myGLCD.setBackColor(BLUE); myGLCD.setColor(WHITE); myGLCD.print(“ReadData”,x21+24,y21+4);

while(true)
{

if (myTouch.dataAvailable())
{
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();

if ((y>=y21) && (y<=y22))
{
if ((x>=x21) && (x<=x22))
{

Redbox(x21,y21,x22,y22);
myGLCD.clrScr();
myGLCD.setBackColor(RED); myGLCD.setColor(WHITE); myGLCD.print(“ok”,x21+24,y21+4);
myGLCD.setBackColor(BLACK);
myGLCD.setFont(BigFont);
myGLCD.setColor(YELLOW); myGLCD.printNumF(X,4,CENTER,60);
myGLCD.setColor(YELLOW); myGLCD.printNumF(Y,4,CENTER,80);
myGLCD.setColor(YELLOW); myGLCD.printNumF(Z,4,CENTER,100);

}
}
}
}
}
void button(int x1, int y1, int x2, int y2)
{
myGLCD.fillRoundRect (x1,y1,x2,y2);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (x1,y1,x2,y2);
}

void Redbox(int x1, int y1, int x2, int y2)
{
myGLCD.setColor(255, 0, 0);
myGLCD.drawRoundRect (x1, y1, x2, y2);
while (myTouch.dataAvailable())
myTouch.read();
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect(x1, y1, x2, y2);
}

My Objective is that while pressing "ReadData "Button Then it should print X,Y,Z infinetly…Now its printing X,Y,Z values on TFT once onlyBut Not Continuously…may need to add some true false conditions but no idea how to do ?

Need urgent Help
Thanks in advance.
i am newbie so its getting hard

Master Program:

#include <Wire.h>
#include <I2C_Anything.h>

const byte SLAVE_ADDRESS = 42;
float X=34.78984;
float Y-=-2.876;
float z=743.98;

void setup() 
{
  Wire.begin ();
}  // end of setup

void loop() 
{


    Wire.beginTransmission (SLAVE_ADDRESS);
    I2C_writeAnything (X);
    I2C_writeAnything (Y);
    I2C_writeAnything (Z);
    Wire.endTransmission ();
      
    delay (200);

Slave program+3.2"TFT program(UTFT &URtouch):

#include <Wire.h>
#include <I2C_Anything.h>
const byte MY_ADDRESS = 42;
volatile boolean haveData = false;
volatile float X;
volatile float Y;
volatile float Z;
volatile char ch;

#include <UTFT.h>
#include <URTouch.h>

extern uint8_t BigFont[];
extern uint8_t SmallFont[];
int x,y;
#define  BLACK           0x0000
#define RED             0xF800
#define GREEN           0x07E0
#define BLUE            0x001F
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0 
#define ORANGE          0xFD20
#define GREENa          0xAFE5 
#define DARKGREEN       0x03E0
#define WHITE           0xFFFF
// Specific hardware
UTFT myGLCD(ILI9341_16,38,39,40,41);
URTouch myTouch(6,5,4,3,2);
int x21=0; int y21=75; int x22=93; int y22=94; 
void setup() {
    myGLCD.InitLCD(PORTRAIT);
    myGLCD.clrScr();
    myTouch.InitTouch(PORTRAIT);
    myTouch.setPrecision(PREC_MEDIUM);
    Wire.begin (MY_ADDRESS);
    Serial.begin (115200);
    Wire.onReceive (receiveEvent);
}

void loop() {
    while (haveData)
    {
    Serial.print (ch); 
    Serial.print ("\t");
    Serial.print (X,4); 
    Serial.print ("\t");   
    Serial.print (Y,4);
    Serial.print ("\t");  
    Serial.print (Z,4);
    Serial.println ("\t");     
    haveData = true; 
    } 
         

    readdata();
    }

    void receiveEvent (int howMany)
 {
 if (howMany >= (sizeof ch)+(sizeof X) + (sizeof Y)+(sizeof Z))
   {
   I2C_readAnything (ch);
   I2C_readAnything (X);   
   I2C_readAnything (Y);   
   I2C_readAnything (Z);   
   haveData = true;     
   } 

 }

 void readdata()
 { myGLCD.clrScr();
    myGLCD.setFont(SmallFont);  
    myGLCD.setColor(BLUE); button(x21,y21,x22,y22); 
    myGLCD.setBackColor(BLUE);   myGLCD.setColor(WHITE);     myGLCD.print("ReadData",x21+24,y21+4); 

    

   while(true)
  {
       
        if (myTouch.dataAvailable())
           {
              myTouch.read();
              x=myTouch.getX();
              y=myTouch.getY();
              
            
        if ((y>=y21) && (y<=y22)) 
           {      
          if ((x>=x21) && (x<=x22)) 
             {  
                    
                Redbox(x21,y21,x22,y22);
                 myGLCD.clrScr();
                myGLCD.setBackColor(RED);   myGLCD.setColor(WHITE);                 myGLCD.print("ok",x21+24,y21+4); 
                myGLCD.setBackColor(BLACK);
                myGLCD.setFont(BigFont);
                myGLCD.setColor(YELLOW);  myGLCD.printNumF(X,4,CENTER,60);  
                myGLCD.setColor(YELLOW);  myGLCD.printNumF(Y,4,CENTER,80); 
                myGLCD.setColor(YELLOW);  myGLCD.printNumF(Z,4,CENTER,100);  
                
     
             }
           }  
 }
 }
 }
void button(int x1, int y1, int x2, int y2)
{
  myGLCD.fillRoundRect (x1,y1,x2,y2);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (x1,y1,x2,y2);
}


void Redbox(int x1, int y1, int x2, int y2)
{
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
  while (myTouch.dataAvailable())
  myTouch.read();
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(x1, y1, x2, y2);
}

You have a few WHILE loops.

  • Your while(true) will never terminate.
  • Your while(havedata) will never terminate once it starts.

Try replacing the WHILEs with IF

Allow the loop() function to look after the repetition.

...R

I am really confused..what to do?I tried some true false conditions but not getting what i want..
it should start the loop fuction as i pressed the "Readdata' button but it only shows once.. :frowning:

You are not the only one who is confused. Your description of what you want to happen is not at all clear. And you have not commented at all on my suggestions in Reply #3

If you want a program to wait in setup() until you press a button you can do that with code like this, as the last thing in setup()

while (digitalRead(buttonPin) == HIGH) { // assumes LOW when pressed

}

...R

My only goal is that while i press the button,it should start displaying X,Y,Z continuously…sir i have followed your instructions… but No luck…This is the code i have tried…

#include <Wire.h>
#include <I2C_Anything.h>
#include <UTFT.h>
#include <URTouch.h>
const byte MY_ADDRESS = 42;
volatile boolean haveData = false;
volatile boolean stoploop=false;
volatile boolean stoploop1=false;
volatile boolean stoploop2=false;
volatile float X;
volatile float Y;
volatile float Z;
volatile char ch;
int button1=4;

extern uint8_t BigFont[];
extern uint8_t SmallFont[];
int x,y;
#define  BLACK          0x0000
#define RED             0xF800
#define GREEN           0x07E0
#define BLUE            0x001F
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0 
#define ORANGE          0xFD20
#define GREENa          0xAFE5 
#define DARKGREEN       0x03E0
#define WHITE           0xFFFF

UTFT myGLCD(ILI9341_16,38,39,40,41);
URTouch myTouch(6,5,4,3,2);
int x21=0; int y21=75; int x22=93; int y22=94; 
void setup() {
    myGLCD.InitLCD(PORTRAIT);
    myGLCD.clrScr();
    myTouch.InitTouch(PORTRAIT);
    myTouch.setPrecision(PREC_MEDIUM);
    Wire.begin (MY_ADDRESS);
    Serial.begin (115200);
    Wire.onReceive (receiveEvent);
    pinMode(button1,OUTPUT);
    digitalWrite(button1,LOW);
     myGLCD.setFont(SmallFont);  
     myGLCD.setColor(BLUE); button(x21,y21,x22,y22); 
     myGLCD.setBackColor(BLUE);   myGLCD.setColor(WHITE);  myGLCD.print("ReadData",x21+24,y21+4); 
      
}

void loop()
{
  while(true)
  {
   if (myTouch.dataAvailable())
           {
           
              myTouch.read();
              x=myTouch.getX();
              y=myTouch.getY();
            
        if ((y>=y21) && (y<=y22)) 
           {      
          if ((x>=x21) && (x<=x22)) 
             {                        
                Redbox(x21,y21,x22,y22);
                digitalWrite(button1,HIGH);
                start_loop();
               
             }}}}
               
}
void XYZ_LOOP()
  {
  while(!haveData)
    {
     delay(500);
     Serial.print (ch); 
     Serial.print ("\t");
     Serial.print (X,4); 
     Serial.print ("\t");   
     Serial.print (Y,4);
     Serial.print ("\t");  
     Serial.print (Z,4);
     Serial.println ("\t");    
   
     myGLCD.setBackColor(BLACK);
     myGLCD.setFont(BigFont);
     myGLCD.setColor(YELLOW);  myGLCD.printNumF(X,4,CENTER,60);  
     myGLCD.setColor(YELLOW);  myGLCD.printNumF(Y,4,CENTER,80); 
     myGLCD.setColor(YELLOW);  myGLCD.printNumF(Z,4,CENTER,100);    
     haveData=false;   
     
  }  

    
  }

void start_loop()
{
  if(digitalRead(button1)==HIGH)
  { XYZ_LOOP();
  }}
  


void button(int x1, int y1, int x2, int y2)
{
 
  myGLCD.fillRoundRect (x1,y1,x2,y2);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (x1,y1,x2,y2);
}


void Redbox(int x1, int y1, int x2, int y2)
{
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
  while (myTouch.dataAvailable())
  myTouch.read();
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(x1, y1, x2, y2);
}
  void receiveEvent (int howMany)
 {
 if (howMany >= (sizeof ch)+(sizeof X) + (sizeof Y)+(sizeof Z))
   {
   I2C_readAnything (ch);
   I2C_readAnything (X);   
   I2C_readAnything (Y);   
   I2C_readAnything (Z);   
   haveData = true;     
   } 

 }

akshay123:
My only goal is that while i press the button,it should start displaying X,Y,Z continuously..sir i have followed your instructions..

I can see no evidence in the code in Reply #6 that you took any notice of my Replies #3 or #5

I also suspect that part of the problem is that you have not described your requirement to yourself in sufficient detail. The piece I have quoted here is nowhere near sufficient to form the basis for a program design. Keep in mind that a computer is very stupid so you have to do all the thinking.

...R