chipKIT® Development Platform

Inspired by Arduino™

[Solved] Problem with routine execution understanding

Created Thu, 11 Jul 2013 02:02:59 +0000 by AhMedina


AhMedina

Thu, 11 Jul 2013 02:02:59 +0000

Hi every one, I really didn't know what subject line to put, i hope that is ok. My problem is: I'm working on my thesis using chipkit uno32 and a xbee protoshield, connected to 5 push buttons, 1 Hall Sensor (DN6851), 1 Humidity and Temperature Sensor (SHT7x) and 7 LEDs.   All hardware and connections are OK. The problem is the execution logics, i really don't understand how to join all the parts of my code properly, I have 3 codes: the 'main code' that has the a task that sends the values of humidity and temperature of the SHT7x every 10s, the Xbee configuration with a switch case for all the payload codes that i need to turn ON and OFF the LEDs; another, 'the rain gauge code' of  the hall sensor, that counts using the function called pulseIn that i used to count the HIGH of the Hall Sensor and resets after 10s of inactivity; and the last one is the 'buttons with debounce code' that toggles the state of the LEDs. I've tried to put the 'buttons w/debounce code' in the  'main code' but the buttons doen'st work!  :smiley-red:

#include "System_Defs.h"
#include <XBee.h>
#include <Sensirion.h>
#include <Bounce.h>
#define BUTTON0 A0
#define bombLed 4
#define BUTTON1 A1
#define blowLed 5
#define BUTTON2 A2
#define S0 6
#define BUTTON3 A3
#define S1 7
#define BUTTON12 12
#define S2 8
int salValue0 = LOW;
int salValue1 = LOW;
int salValue2 = LOW;
int salValue3 = LOW;
int salValue4 = LOW;

//TASKs
int medicion_id;
unsigned long medicion_var;
// allocate two bytes for to hold a 10-bit analog reading
uint8_t payload[] = {   0, 0, 0, 0};
// with Series 1 you can use either 16-bit or 64-bit addressing
// 16-bit addressing: Enter address of remote XBee, typically the coordinator
Tx16Request tx = Tx16Request(0x0001, payload, sizeof(payload));
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle 
Rx16Response rx16 = Rx16Response();
Rx64Response rx64 = Rx64Response();
//variables

boolean Manual=true;

//Salidas
int16_t statusLed = 9;
int16_t errorLed = 10;
/*int16_t bombLed = 4;
int16_t blowLed = 5;
int16_t S0 = 6;
int16_t S1 = 7;
int16_t S2 = 8;
*/

//SHT
const int16_t dataPin  =  2;
const int16_t clockPin =  3;
int16_t option = 0;
int16_t data = 0;
float temperature;
float humidity;
float dewpoint;

Sensirion tempSensor = Sensirion(dataPin, clockPin);

void flashLed(int16_t pin, int16_t times, int16_t wait) {

  for (int16_t i = 0; i < times; i++) {
    digitalWrite(pin, HIGH);
    delay(wait);
    digitalWrite(pin, LOW);

    if (i + 1 < times) {
      delay(wait);
    }
  }
}

//Task
void envio_medicion(int id, void * tptr) {
  //a partir de aquí es para la lectura del sensor y la impresión     
        tempSensor.measure(&temperature, &humidity, &dewpoint);
        delay (10);
        //envio de datos por el Xbee
        payload[0] = (int16_t) temperature >> 8 & 0xff; //cast a temperature para
        payload[1] = (int16_t) temperature & 0xff; //que pueda ser manipulada
        payload[2] = (int16_t) humidity >> 8 & 0xff; //cast a humedad para
        payload[3] = (int16_t) humidity & 0xff; //que pueda ser manipulada
        delay (20);
        xbee.send(tx);
        // flash TX indicator
        flashLed(statusLed, 1, 100); 
    }
    
Bounce bouncer0 = Bounce( BUTTON0, 50 ); 
Bounce bouncer1 = Bounce( BUTTON1, 50 );
Bounce bouncer2 = Bounce( BUTTON2, 50 );
Bounce bouncer3 = Bounce( BUTTON3, 50 );
Bounce bouncer12 = Bounce( BUTTON12, 50 );   

void setup() {

  pinMode(statusLed, OUTPUT);
  pinMode(errorLed, OUTPUT);
  pinMode(blowLed,  OUTPUT);
  pinMode(bombLed,  OUTPUT);
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2,  OUTPUT);
  
  pinMode(BUTTON0,INPUT);
  pinMode(BUTTON1,INPUT);
  pinMode(BUTTON2,INPUT);
  pinMode(BUTTON3,INPUT);
  pinMode(BUTTON12,INPUT);


  // start serial
  xbee.begin(9600);
  flashLed(statusLed, 3, 50);

  digitalWrite(blowLed, LOW);
  digitalWrite(bombLed, LOW);
  digitalWrite(S0, LOW);
  digitalWrite(S1, LOW);
  digitalWrite(S2, LOW);
  
  //Task
  medicion_id = createTask(envio_medicion, 5000, TASK_ENABLE, &medicion_var); 

}

// continuously reads packets, looking for RX16 or RX64
void loop() {
      
  xbee.readPacket();

  if (xbee.getResponse().isAvailable()) {
    // got something

    if (xbee.getResponse().getApiId() == RX_16_RESPONSE || xbee.getResponse().getApiId() == RX_64_RESPONSE) {
      // got a rx packet

      if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
        xbee.getResponse().getRx16Response(rx16);
        option = rx16.getOption();
        data = rx16.getData(0);
      } 
      else {
        xbee.getResponse().getRx64Response(rx64);
        option = rx64.getOption();
        data = rx64.getData(0);
      }

      // TODO check option, rssi bytes    
      flashLed(statusLed, 1, 10);
      
       
      //MODOS DE OPERACIÓN
      switch (data){
        
      case 0x05: //MODO EMERGENCIA
        digitalWrite(blowLed, LOW);                        
        digitalWrite(bombLed, LOW);
        digitalWrite(S0, LOW);                             
        digitalWrite(S1, LOW);
        digitalWrite(S2, LOW);
        break;

      case 0x02: //Modo Manual
        Manual=true;
        digitalWrite(blowLed, LOW);                        
        digitalWrite(bombLed, LOW);
        digitalWrite(S0, LOW);                             
        digitalWrite(S1, LOW);
       digitalWrite(S2, LOW);
        break;

        //MODO "AUTOMATICO" *SOLO PARA PRUEBAS EN LV *
      case 0x03:
        Manual=false;
        digitalWrite(blowLed, HIGH);                        
        digitalWrite(bombLed, HIGH);
        digitalWrite(S0, HIGH);                             
        digitalWrite(S1, HIGH);
        digitalWrite(S2, HIGH);
        break;

        //Establece Salidas
      case 0x04:
        Manual=true;
        digitalWrite(blowLed, LOW);                        
        digitalWrite(bombLed, HIGH);
        digitalWrite(S0, HIGH);                             
        digitalWrite(S1, HIGH);
        digitalWrite(S2, LOW);
        break;

        //Caso 1: Soplador y Bomba Apagados
      case 0xAA:
        Manual=true;
        digitalWrite(blowLed, LOW);
        digitalWrite(bombLed, LOW);
        break;

        //Caso 2: Soplador ON & Bomba OFF
      case 0xFF:
        Manual=true;
        digitalWrite(blowLed, HIGH);
        digitalWrite(bombLed, LOW);
        digitalWrite(S0, LOW);                             
        digitalWrite(S1, LOW);
        digitalWrite(S2, LOW);
        break;

        //Caso 3: Soplador OFF & Bomba ON
      case 0xBB:
        Manual=true;
        digitalWrite(blowLed, LOW);
        digitalWrite(bombLed, HIGH);
        digitalWrite(S0, LOW);                             
        digitalWrite(S1, LOW);
        digitalWrite(S2, LOW);
        break;


        //Caso 4: Soplador ON & Bomba ON
      case 0xEE:
        Manual=true;
        digitalWrite(blowLed, HIGH);
        digitalWrite(bombLed, HIGH);
        digitalWrite(S0, LOW);                             
        digitalWrite(S1, LOW);
        digitalWrite(S2, LOW);
        break;

      }
  
          ///////////////Buttons w/debounce (doesn't work)
      if ( bouncer0.update() ) {
     if ( bouncer0.read() == HIGH) {
       if ( salValue0 == LOW ) {
         salValue0 = HIGH;
       } else {
         salValue0 = LOW;
       }
       digitalWrite(bombLed,salValue0);
     }
   }
   
   if ( bouncer1.update() ) {
     if ( bouncer1.read() == HIGH) {
       if ( salValue1 == LOW ) {
         salValue1 = HIGH;
       } else {
         salValue1 = LOW;
       }
       digitalWrite(blowLed,salValue1);
     }
   }
   
   if ( bouncer2.update() ) {
     if ( bouncer2.read() == HIGH) {
       if ( salValue2 == LOW ) {
         salValue2 = HIGH;
       } else {
         salValue2 = LOW;
       }
       digitalWrite(S0,salValue2);
     }
   }
   
      if ( bouncer3.update() ) {
     if ( bouncer3.read() == HIGH) {
       if ( salValue3 == LOW ) {
         salValue3 = HIGH;
       } else {
         salValue3 = LOW;
       }
       digitalWrite(S1,salValue3);
     }
   }
   
      if ( bouncer12.update() ) {
     if ( bouncer12.read() == HIGH) {
       if ( salValue4 == LOW ) {
         salValue4 = HIGH;
       } else {
         salValue4 = LOW;
       }
       digitalWrite(S2,salValue4);
     }
   }
////////////////////////////////     
      

      Serial.print(data);
    } 
    else {
      // not something we were expecting
      flashLed(errorLed, 1, 25);    
    }
  } 
  else if (xbee.getResponse().isError()) {
    //nss.print("Error reading packet.  Error code: ");  
    //nss.println(xbee.getResponse().getErrorCode());
    // or flash error led
  } 
}

and the same thing with the 'rain gauge code':

/*
Cuenta los pulsos altos, despues de 10s se resetea la cuenta C:

Arduino has a convenient function called pulseIn 
that you can use to count the HIGH or the LOW of a pulse. 
The difference between the LOW and HIGH 
must be at least greater than 3 volts.
http://note19.com/2008/12/28/circuit-gear-arduino-and-counting-pulses/
http://arduino.cc/en/Reference/PulseIn
*/

int pulsePin = 11;
unsigned long counter = 0;
unsigned long duration = 0;
unsigned long timeout = 10000000; // in microseconds
 
void setup() {
  pinMode(pulsePin, INPUT);
  // enable the 20K pull-up resistor to steer
  // the input pin to a HIGH reading.
  digitalWrite(pulsePin, HIGH);
  Serial.begin(9600);
  Serial.println("Here we go again");
}
 
void loop() {
  duration = pulseIn(pulsePin, HIGH, timeout);
  if (duration == 0) {
    Serial.print("Pulse started before the timeout.");
    Serial.println("");
    counter=0;
    
  } else {
    counter++;
    Serial.print(counter);
    Serial.print(", ");
    Serial.print(duration);
    Serial.println("");
  }
}

in addition I've tested the three codes separately and all works fine, the problem is when I try to join the codes together  :~

this is the 'buttons w/debbounce code':

#include <Bounce.h>
#define BUTTON0 A0
#define bombLed 4
#define BUTTON1 A1
#define blowLed 5
#define BUTTON2 A2
#define S0 6
#define BUTTON3 A3
#define S1 7
#define BUTTON12 12
#define S2 8
int salValue0 = LOW;
int salValue1 = LOW;
int salValue2 = LOW;
int salValue3 = LOW;
int salValue4 = LOW;

// This example changes the state of the LED everytime the button is pushed
// Build the circuit indicated here: http://arduino.cc/en/Tutorial/Button


Bounce bouncer0 = Bounce( BUTTON0, 50 ); 
Bounce bouncer1 = Bounce( BUTTON1, 50 );
Bounce bouncer2 = Bounce( BUTTON2, 50 );
Bounce bouncer3 = Bounce( BUTTON3, 50 );
Bounce bouncer12 = Bounce( BUTTON12, 50 );

void setup() {
  
  
  pinMode(bombLed,  OUTPUT);
  pinMode(blowLed,  OUTPUT);
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2,  OUTPUT);
    
  pinMode(BUTTON0,INPUT);
  pinMode(BUTTON1,INPUT);
  pinMode(BUTTON2,INPUT);
  pinMode(BUTTON3,INPUT);
  pinMode(BUTTON12,INPUT);
  
  digitalWrite(blowLed, LOW);
  digitalWrite(bombLed, LOW);
  digitalWrite(S0, LOW);
  digitalWrite(S1, LOW);
  digitalWrite(S2, LOW);
  

}

void loop() {

   if ( bouncer0.update() ) {
     if ( bouncer0.read() == HIGH) {
       if ( salValue0 == LOW ) {
         salValue0 = HIGH;
       } else {
         salValue0 = LOW;
       }
       digitalWrite(bombLed,salValue0);
     }
   }
   
   if ( bouncer1.update() ) {
     if ( bouncer1.read() == HIGH) {
       if ( salValue1 == LOW ) {
         salValue1 = HIGH;
       } else {
         salValue1 = LOW;
       }
       digitalWrite(blowLed,salValue1);
     }
   }
   
   if ( bouncer2.update() ) {
     if ( bouncer2.read() == HIGH) {
       if ( salValue2 == LOW ) {
         salValue2 = HIGH;
       } else {
         salValue2 = LOW;
       }
       digitalWrite(S0,salValue2);
     }
   }
   
      if ( bouncer3.update() ) {
     if ( bouncer3.read() == HIGH) {
       if ( salValue3 == LOW ) {
         salValue3 = HIGH;
       } else {
         salValue3 = LOW;
       }
       digitalWrite(S1,salValue3);
     }
   }
   
      if ( bouncer12.update() ) {
     if ( bouncer12.read() == HIGH) {
       if ( salValue4 == LOW ) {
         salValue4 = HIGH;
       } else {
         salValue4 = LOW;
       }
       digitalWrite(S2,salValue4);
     }
   }
}

Please please! I don't really want to do my work, i just want to understand what I'm doing wrong, where should I put the other codes in the main code to work? what I'm missing? How the routine works? Is the Task function screwing all? all comments and suggestions are helpful!!! Thanks in advance.


jmlynesjr

Fri, 12 Jul 2013 00:14:22 +0000

Take a look at using Pin Change Interrupts for your switches. All else will probably work ok in a main loop calling subroutines. I have a post on the forum using this technique with a Sparkfun rotary encoder. The library is courtesy of Majenko.

James


AhMedina

Fri, 12 Jul 2013 02:17:55 +0000

Take a look at using Pin Change Interrupts for your switches...

Hi James, thanks for your reply! :D
where can I read about it? :oops:

...All else will probably work ok in a main loop calling subroutines. I have a post on the forum using this technique with a Sparkfun rotary encoder. The library is courtesy of Majenko.

I'll search for that topic, I'm new in all this stuff so my knowledge is limited but I really want to expand it! Thanks again!


jmlynesjr

Fri, 12 Jul 2013 18:31:52 +0000

Sorry:

Look at http://github.com/jmlynesjr/chipKIT-Arduino-Examples for several examples. RotaryEncoderISR4.pde - Sparkfun Rotary Encoder Driver - Interrupt Version

James


AhMedina

Wed, 30 Oct 2013 17:42:27 +0000

Sorry: Look at http://github.com/jmlynesjr/chipKIT-Arduino-Examples for several examples. RotaryEncoderISR4.pde - Sparkfun Rotary Encoder Driver - Interrupt Version James

Thanks a lot, it was useful for me! :D


jmlynesjr

Wed, 30 Oct 2013 19:54:39 +0000

Excellent. Glad the example was of use to you. Good luck with your thesis!

James