needto toggle a led witha specific range of analog input ( same as button state

iam finding trouble in programming my arduino

i have a touch screen connected to my analog inputs on my arduino uno , when i touch at a specified area (within a range of coordinates), i need to turn the led " ON" which is connected to digital pin 13 , when i again touch it it should switch off the led ,,,,

as it can be done with button state with digital inputs

Start with the touchscreen first and dump the value to your terminal serial.printf() , just to get control of your touchscreen, these things are not that easy.

Where is your code? The type of digital event (this area of this screen was touched vs. this button was pressed) matters not at all in determining how to react to the digital event.

this was my program

int y1 = A0;
int x2 = A1;
int y2 = A2;
int x1 = A3;

void setup() { 
  Serial.begin(9600);
} 

int readX(){
  pinMode(y1, INPUT);
  pinMode(x2, OUTPUT);
  pinMode(y2, INPUT);
  pinMode(x1, OUTPUT);

  digitalWrite(x2, LOW);
  digitalWrite(x1, HIGH);

  delay(5); //pause to allow lines to power up

  return analogRead(y1);
}

int readY(){

  pinMode(y1, OUTPUT);
  pinMode(x2, INPUT);
  pinMode(y2, OUTPUT);
  pinMode(x1, INPUT);

  digitalWrite(y1, LOW);
  digitalWrite(y2, HIGH);

  delay(5); //pause to allow lines to power up

  return analogRead(x2);
}

void loop()
{
  int x = readX();
  int y = readY();


  if(x < 1023 & y < 1023){
    
    
    if (y>0 && x>0 && x<329 && y<413)
  {
   Serial.println("1 "); \\ *** here i want to toggle the led ON  and OFF 
    
  } else 
  if (y<413 && x<568 && x>329)
  {
    Serial.println("2 "); \\ *** here i want to toggle the led ON and OFF 
  } 
    

  delay(100); //just to slow this down so it is earier to read in the terminal - Remove if wanted
  }
}
  if(x < 1023 & y < 1023){

Since they can never be greater than 1023, this test seems pointless. If you are going to test that they are not higher than the highest possible value, why not test that they are indeed not less than the lowest possible value?

   Serial.println("1 "); \\ *** here i want to toggle the led ON  and OFF

So do that! It's done EXACTLY the same way that it is done when a switch is pressed.

    Serial.println("2 "); \\ *** here i want to toggle the led ON and OFF

The same pin? Or, do you mean that one region turns the pin on and the other region turns the pin off?

Clear requirements are needed BEFORE you begin coding.

int y1 = A0;
int x2 = A1;
int y2 = A2;
int x1 = A3;

void setup() { 
  Serial.begin(9600);
} 

int readX(){
  pinMode(y1, INPUT);
  pinMode(x2, OUTPUT);
  pinMode(y2, INPUT);
  pinMode(x1, OUTPUT);

  digitalWrite(x2, LOW);
  digitalWrite(x1, HIGH);

  delay(5); //pause to allow lines to power up

  return analogRead(y1);
}

int readY(){

  pinMode(y1, OUTPUT);
  pinMode(x2, INPUT);
  pinMode(y2, OUTPUT);
  pinMode(x1, INPUT);

  digitalWrite(y1, LOW);
  digitalWrite(y2, HIGH);

  delay(5); //pause to allow lines to power up

  return analogRead(x2);
}

void loop()
{
  int x = readX();
  int y = readY();


  if(x < 1000 & y < 1000){    \\** its just used to ignore the coordinates  when the touch screen is not touched (usually if its not touched the value returned is 1023 ,1023 )
    
    
    if (y>0 && x>0 && x<329 && y<413)
  {
   Serial.println("1 "); \\ *** here i want to toggle the led 1 ON  and OFF 
    
  } else 
  if (y<413 && x<568 && x>329)
  {
    Serial.println("2 "); \\ *** here i want to toggle the led 2 ON and OFF 
  } 
    

  delay(100); 
  }
}

i had a series of LEDs which should be switched by pressing the specific areas in the touchscreen ( 10 diff leds using 10 diff positions on touchscreen )

like touching the 1st Quadrant on the touch screen should turn led 1 ON and in the same way touching it to turn the led OFF

similarly i wanna toggle 4 LEDs with 4 Quadrant's on the touch screen

simple toggle test code.

//zoomkat LED button toggle test 11-08-2012

int button = 5; //button pin, connect to ground as button
int press = 0;
boolean toggle = true;

void setup()
{
  pinMode(13, OUTPUT); //LED on pin 13
  pinMode(button, INPUT); //arduino monitor pin state
  digitalWrite(5, HIGH); //enable pullups to make pin 5 high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      digitalWrite(13, HIGH);   // set the LED on
      toggle = !toggle;
    }
    else
    {
      digitalWrite(13, LOW);    // set the LED off
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

:slight_smile: :slight_smile: :slight_smile: :slight_smile: thanks for your immediate reply . now its solved .........but there is a Little denouncing if use the 100ms delay