Pages: [1]   Go Down
Author Topic: need help with "when button is pressed, sketch is carried out"  (Read 588 times)
0 Members and 1 Guest are viewing this topic.
Malaysia
Offline Offline
Sr. Member
****
Karma: 3
Posts: 260
Stay calm and call batman!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good day,
I need help with an Arduino sketch. I got the sketch from a source but would like to add a button function to it. For example when button is pressed, then ( sketch is carried out). Been trying for days now but cannot seem to get it to work, my sketch without the button function is shown bellow






#include <SPI.h> // needed in Arduino 0019 or later
#include <Ethernet.h>
#include <Twitter.h>

// The inclusion of EthernetDNS is not needed in Arduino IDE 1.0 or later.
// Please uncomment below in Arduino IDE 0022 or earlier.
//#include <EthernetDNS.h>


// Ethernet Shield Settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// If you don't specify the IP address, DHCP is used(only in Arduino 1.0 or later).
byte ip[] = { 192, 168, 2, 250 };

// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("xxxxx");

// Message to post
char msg[140];
int delayS = 60;

void softReset()
{
  asm volatile ("jmp 0");
}

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

void loop()
{
  snprintf(msg, 140, "Hello World![%d]", analogRead(A0));

  Serial.println("Delaying ...");
  for(int i = 0; i < delayS; i++)
  {
    delay(1000);
    Serial.print(i);
    Serial.print(" ");
  }
  Serial.println();

  Serial.println("connecting ...");
  if (twitter.post(msg)) {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();
    int status = twitter.wait(&Serial);
    if (status == 200) {
      Serial.println("OK.");
    } else {
      Serial.print("failed : code ");
      Serial.println(status);
    }
  } else {
    Serial.println("connection failed.");
  }
  softReset();
}



can someone help with this? your help is much appreciated =)
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 19131
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How to use this forum


Code tags please.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks very muc

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

just to be sure is it a button on the keyboard you want to push to carry your sketch out?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4363
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Button example sketch in the IDE will show you how to read a button and act on it.  It turns an LED on and off but it could equally be made to run a function.

For your requirement the easiest thing to do would be to move everything in the loop() function of your current program to a new function, put the button reading code in the now empty loop() function and instead of turning a LED on/off when the button is pressed call the new function.

There are other things to consider, such as should the program continue to run after the button is released, or should it stop, is it OK for the same program to run twice (or more) or should it be prevented from running again but get the 'press button, run program' working first.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Malaysia
Offline Offline
Sr. Member
****
Karma: 3
Posts: 260
Stay calm and call batman!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone thanks for the replies,  Malibux, not a keyboard button, but a tactile switch.
i got a sketch to work, when i press button 2, it sends a sketch, however, i would like to add a second button to carry out a second function. I tried a sketch but only the first button works, the second however doesn't. can someone help me to proofread? the sketch is shown bellow




#include <SPI.h> // needed in Arduino 0019 or later
#include <Ethernet.h>
#include <Twitter.h>

// The inclusion of EthernetDNS is not needed in Arduino IDE 1.0 or later.
// Please uncomment below in Arduino IDE 0022 or earlier.
//#include <EthernetDNS.h>

boolean MsgSent = false;
const int Sensor = 2;
const int Sensor1 = 3;
// Ethernet Shield Settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// If you don't specify the IP address, DHCP is used(only in Arduino 1.0 or later).
byte ip[] = { 192, 168, 2, 250 };

// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("xxxxxxxx");

EthernetClient client;



// Message to post
char msg[140];
char msg1[141];
int delayS = 6;

void softReset()
{
  asm volatile ("jmp 0");
}

void setup()
{
  Serial.begin(9600);   
  if (Ethernet.begin(mac) == 0) { // start ethernet using mac & DHCP address
    Serial.println("Failed to configure Ethernet using DHCP"); 
    while(true)   // no point in carrying on, so stay in endless loop:
      ;
  }
  pinMode(Sensor, INPUT);
  digitalWrite(Sensor, HIGH);  //turn on  pull-up resistors
  delay(1000);
  Serial.println("Press Button 1 to Start!"); 
 
  pinMode(Sensor1, INPUT);
  digitalWrite(Sensor1, HIGH);  //turn on  pull-up resistors
  delay(1000);
  Serial.println("Press Button 2 to Start!");
}

void loop()

{
  if(digitalRead(Sensor) == LOW)
{
  snprintf(msg, 140, "Hello %d", analogRead(A0));

  Serial.println("Delaying ...");
  for(int i = 0; i < delayS; i++)
  {
    delay(1000);
    Serial.print(i);
    Serial.print(" ");
 
  }
  Serial.println();

  Serial.println("connecting ...");
  if (twitter.post(msg))
  {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();
    int status = twitter.wait(&Serial);
    if (status == 200) {
      Serial.println("OK.");
    } else {
      Serial.print("failed : code ");
      Serial.println(status);
    }
  softReset();
  {
  if(digitalRead(Sensor1) == LOW)
{
  snprintf(msg1, 141, "World %d", analogRead(A0));

  Serial.println("Delaying ...");
  for(int i = 0; i < delayS; i++)
  {
    delay(1000);
    Serial.print(i);
    Serial.print(" ");
 
  }
  Serial.println();

  Serial.println("connecting ...");
  if (twitter.post(msg))
  {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();
    int status = twitter.wait(&Serial);
    if (status == 200) {
      Serial.println("OK.");
    } else {
      Serial.print("failed : code ");
      Serial.println(status);
    }

  softReset();
}
}
}
}
}
}


Your help is much appreciated! =)
   
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 3
Posts: 260
Stay calm and call batman!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, let me add a code tag



Code:
#include <SPI.h> // needed in Arduino 0019 or later
#include <Ethernet.h>
#include <Twitter.h>

// The inclusion of EthernetDNS is not needed in Arduino IDE 1.0 or later.
// Please uncomment below in Arduino IDE 0022 or earlier.
//#include <EthernetDNS.h>

boolean MsgSent = false;
const int Sensor = 2;
const int Sensor1 = 3;
// Ethernet Shield Settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// If you don't specify the IP address, DHCP is used(only in Arduino 1.0 or later).
byte ip[] = { 192, 168, 2, 250 };

// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("xxxxxxxx");

EthernetClient client;



// Message to post
char msg[140];
char msg1[141];
int delayS = 6;

void softReset()
{
  asm volatile ("jmp 0");
}

void setup()
{
  Serial.begin(9600);   
  if (Ethernet.begin(mac) == 0) { // start ethernet using mac & DHCP address
    Serial.println("Failed to configure Ethernet using DHCP");
    while(true)   // no point in carrying on, so stay in endless loop:
      ;
  }
  pinMode(Sensor, INPUT);
  digitalWrite(Sensor, HIGH);  //turn on  pull-up resistors
  delay(1000);
  Serial.println("Press Button 1 to Start!");
 
  pinMode(Sensor1, INPUT);
  digitalWrite(Sensor1, HIGH);  //turn on  pull-up resistors
  delay(1000);
  Serial.println("Press Button 2 to Start!");
}

void loop()

{
  if(digitalRead(Sensor) == LOW)
{
  snprintf(msg, 140, "Hello %d", analogRead(A0));

  Serial.println("Delaying ...");
  for(int i = 0; i < delayS; i++)
  {
    delay(1000);
    Serial.print(i);
    Serial.print(" ");
 
  }
  Serial.println();

  Serial.println("connecting ...");
  if (twitter.post(msg))
  {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();
    int status = twitter.wait(&Serial);
    if (status == 200) {
      Serial.println("OK.");
    } else {
      Serial.print("failed : code ");
      Serial.println(status);
    }
  softReset();
  {
  if(digitalRead(Sensor1) == LOW)
{
  snprintf(msg1, 141, "World %d", analogRead(A0));

  Serial.println("Delaying ...");
  for(int i = 0; i < delayS; i++)
  {
    delay(1000);
    Serial.print(i);
    Serial.print(" ");
 
  }
  Serial.println();

  Serial.println("connecting ...");
  if (twitter.post(msg))
  {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();
    int status = twitter.wait(&Serial);
    if (status == 200) {
      Serial.println("OK.");
    } else {
      Serial.print("failed : code ");
      Serial.println(status);
    }

  softReset();
}
}
}
}
}
}
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 19131
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You threw a lot of braces at the end to get it to compile, didn't you?

Try using the auto-format tool. Then look at what is nested inside what.

Code:
void loop()

{
  if(digitalRead(Sensor) == LOW)
  {
    snprintf(msg, 140, "Hello %d", analogRead(A0));


You should look for a transition from HIGH to LOW, otherwise you are going to be sending that message an awful lot.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks very muc

Malaysia
Offline Offline
Sr. Member
****
Karma: 3
Posts: 260
Stay calm and call batman!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, i have done the auto format, but i still cant see what is nested. I'm new to programming . what do you mean by transition?
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 3
Posts: 260
Stay calm and call batman!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

and yes i did throw in braces to make it compile lol
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 19131
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, i have done the auto format, but i still cant see what is nested. I'm new to programming . what do you mean by transition?

It is high now, but last time through the loop it was low. You need to save the last state and compare them.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks very muc

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 19131
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, i have done the auto format, but i still cant see what is nested.

Code:
 if(digitalRead(Sensor) == LOW)
  {

  ...

        if(digitalRead(Sensor1) == LOW)
        {
        ...      
        }

  }

The test for Sensor1 is only done if the test for Sensor is true. One is "nested" inside the other.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks very muc

Malaysia
Offline Offline
Sr. Member
****
Karma: 3
Posts: 260
Stay calm and call batman!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi thanks for your help! finally got it to work!!
Logged

Pages: [1]   Go Up
Jump to: