Go Down

Topic: need help with "when button is pressed, sketch is carried out" (Read 604 times) previous topic - next topic

DMond

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 =)

Nick Gammon


Malibux

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

UKHeliBob

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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

DMond

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! =)
 

DMond

Sorry, let me add a code tag



Code: [Select]
#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();
}
}
}
}
}
}

Nick Gammon

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: [Select]

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.

DMond

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?

DMond

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

Nick Gammon


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.

Nick Gammon


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


Code: [Select]

 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.

DMond


Go Up