Pages: [1]   Go Down
Author Topic: needto toggle a led witha specific range of analog input ( same as button state  (Read 510 times)
0 Members and 1 Guest are viewing this topic.
hyderabad
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Belgium
Offline Offline
Full Member
***
Karma: 6
Posts: 135
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Gear: Arduino- Uno,Due,Ethernet,  OLS, Buspirate, J-Link, TDS1002, Rigol DG1022

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50166
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

hyderabad
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this was my program

Code:
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
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50166
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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?

Code:
   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.

Code:
    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.
Logged

hyderabad
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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 )
Logged

hyderabad
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9675
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

simple toggle test code.

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
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

hyderabad
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



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

Pages: [1]   Go Up
Jump to: