Help getting the RF M4 Receiver to work (Code Issue)

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
joshuakane
 
Posts: 282
Joined: Sat Apr 13, 2013 4:40 pm

Help getting the RF M4 Receiver to work (Code Issue)

Post by joshuakane »

Hello:

I am looking for some coding assistance.

I have a FLORA, and I have written the program below. The idea is that 20 leds light up green, and when button "A" on the Remote is pushed the LED's wipe and only 10 light up Yellow. If you press button "B" the LED's wipe and 5 Light up Red.

The connections are set so that

cPin = 12; (connected from the Flora D12 to the RF D0)
dPin = 6; (Connected from the Flora D6 to the RF D1)


Code: Select all

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_WS2801.h>


//
//debnouncer
//
#define DEBOUNCE 10  // button debouncer, how many ms to debounce, 5+ ms is usually plenty

//
//LEDs
//
int stripDataPin  = 10;    // Yellow wire on Adafruit Pixels
int stripClockPin = 9;    // Green wire on Adafruit Pixels
const int cPin = 12;
const int dPin = 6;

// Set the first variable to the NUMBER of pixels. 32 = 32 pixels in a row
// The LED's in this strip will be 20 per the specifications of the costume
Adafruit_WS2801 strip = Adafruit_WS2801(20,stripDataPin,stripClockPin);

//
//Remote
//
// here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc
#define C_PINONREMOTE 1  // was 6
#define D_PINONREMOTE 2 //was 11
byte buttons[] = {C_PINONREMOTE, D_PINONREMOTE}; // the analog 0-5 pins are also known as 14-19
// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
// we will track if a button is just pressed, just released, or 'currently pressed' 
byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];


int start = 0;
int prevX = 0;
int currentX = 0;

int cState = 0;
int dState = 0;

void setup() 
{
  Serial.begin(9600);
  
  // Start up the LED strip
  strip.begin();
  // Update the strip, to start they are all 'off'
  strip.show();
  
  //pinMode(cPinOnRemote, INPUT); 
  //pinMode(dPinOnRemote, INPUT);
  
  //What I was looking for was something that would initilize green, powering up one by one. 
  colorWipe(Color(0, 255, 0), 75);  // green fill
  
}

void check_switches()
{
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  static long lasttime;
  byte index;

  if (millis() < lasttime) {
     // we wrapped around, lets just try again
     lasttime = millis();
  }
  
  if ((lasttime + DEBOUNCE) > millis()) {
    // not enough time has passed to debounce
    return; 
  }
  // ok we have waited DEBOUNCE milliseconds, lets reset the timer
  lasttime = millis();
  
  for (index = 0; index < NUMBUTTONS; index++) {
    justpressed[index] = 0;       // when we start, we clear out the "just" indicators
    justreleased[index] = 0;
     
    currentstate[index] = digitalRead(buttons[index]);   // read the button
    
    /*     
    Serial.print(index, DEC);
    Serial.print(": cstate=");
    Serial.print(currentstate[index], DEC);
    Serial.print(", pstate=");
    Serial.print(previousstate[index], DEC);
    Serial.print(", press=");
    */
    
    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
          // just pressed
          justpressed[index] = 1;
      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
          // just released
          justreleased[index] = 1;
      }
      pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed
    }
    //Serial.println(pressed[index], DEC);
    previousstate[index] = currentstate[index];   // keep a running tally of the buttons
  }
}

void loop() 
{
  check_switches();      // when we check the switches we'll get the current state
  
  for (byte i = 0; i < NUMBUTTONS; i++) {
    if (justpressed[i]) {
      Serial.print(i, DEC);
      Serial.println(" Just pressed"); 
      // remember, check_switches() will CLEAR the 'just pressed' flag
      switch (i) 
      {
        case C_PINONREMOTE:
          //do something when var equals 1
          // half Power
          Serial.println("Half Power");
          hideAll();
          Half_Power(Color(255,255,0));
          break;
          
        case D_PINONREMOTE:
          //do something when var equals 2
          // Critical_Power
          Serial.println("Critical Power"); 
          hideAll();
          Critical_Power(Color(255,0,0));
          break;
          
         default:
         break;
          // if nothing else matches, do the default
          // default is optional
          // 
      }
    }
    if (justreleased[i]) {
      Serial.print(i, DEC);
      Serial.println(" Just released");
      // remember, check_switches() will CLEAR the 'just pressed' flag
    }
    if (pressed[i]){
      Serial.print(i, DEC);
      Serial.println(" pressed");
      // is the button pressed down at this moment
    }
  }  
}

//This most likely does not ever run
//  int i = currentX - prevX;


void colorWipe(uint32_t c, uint8_t wait) {
  int i;

  for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

/*void check_switches()
{
cPinOnRemoteState = digitalRead(cPinOnRemote);
dPinOnRemoteState = digitalRead(dPinOnRemote);
//}
// 
*/
void Half_Power(uint32_t c){
HalfLight(c);
}

void Critical_Power(uint32_t c){
QuarterLight(c);
}

/* Helper functions */ 

//Input a value 0 to 384 to get a color value.
//The colors are a transition r - g - b - back to r

void HalfLight(uint32_t c){
  for (int i=0; i < 10; i++) {
    strip.setPixelColor(i,Color(255, 255, 0));
  }
}

void QuarterLight(uint32_t c){
  for (int i=0; i < 5; i++) {
    strip.setPixelColor(i,Color(255, 0, 0));
  }
}

void hideAll(){
  for(int i = 0; i > strip.numPixels();i++){
   strip.setPixelColor(i,Color(0, 0, 0));
  }
  strip.show();
}

void displayAll(){
  for(int i = 0; i > strip.numPixels();i++){
   strip.setPixelColor(i,Color(0, 255, 0));
  }
  strip.show();
}
  
// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
  uint32_t c;
  c = 0;
  c <<= 8;
  c |= 255;
  c <<= 8;
  c |= 0;
  return c;
}

/* colorWipe(Color(255, 0, 0));    // red fill
   colorWipe(Color(255, 255, 0));  // yellow fill
   colorWipe(Color(0, 255, 0));    // green fill */


User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Help getting the RF M4 Receiver to work (Code Issue)

Post by adafruit_support_bill »

And what exactly is the problem? What does your code do (or not do)?

User avatar
joshuakane
 
Posts: 282
Joined: Sat Apr 13, 2013 4:40 pm

Re: Help getting the RF M4 Receiver to work (Code Issue)

Post by joshuakane »

Hey Bill,

Thanks for responding!

Right now the code will initialize the 20 LED's in the chain. The light up one at a time as they are supposed to.

Pressing button "A" or button "B" does not result in anything happening.

The hope was that pressing button "A" would set the strand of LED's to Yellow, and have 10 of them lit. Pressing "B" would set them to red and have 5 of them lit.

Ultimately the goal would be to have all 20 LED's light up, and have pressing "A" decrement the LED's one and if they reach 10 LED's they change color to yellow, if they reach 5 LED's they turn Red. PRessing "B" would increment them back up so once there were 6 it would turn yellow again, and once there was 11 it would turn green again. I am sure this could be done with a loop and a counter, I am just trying to learn how to do express it in the program

Any help you can offer would be greatly assisted.

Also I managed to figure out that the FLORA changed slightly from the Backpack tutorial, as the "D" numbers changed for both the connections for the RF module, as well as for the LED Strand.

Thanks,

Joshua

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Help getting the RF M4 Receiver to work (Code Issue)

Post by adafruit_support_bill »

You need to define your buttons the same as the pins you are connecting the RF module to:

Code: Select all

// here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc
#define C_PINONREMOTE 1  // was 6
#define D_PINONREMOTE 2 //was 11
byte buttons[] = {C_PINONREMOTE, D_PINONREMOTE}; // the analog 0-5 pins are also known as 14-19

User avatar
joshuakane
 
Posts: 282
Joined: Sat Apr 13, 2013 4:40 pm

Re: Help getting the RF M4 Receiver to work (Code Issue)

Post by joshuakane »

Thanks Bill, so if I understand you correctly then I should be able to do something like this.

Code: Select all

// here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc
#define C_PINONREMOTE 12  // This is the wire connected to the Flora at D12 and foing to RF D0
#define D_PINONREMOTE 6   // This is the wire connected to the Flora at D6 and foing to RF D1
byte buttons[] = {C_PINONREMOTE, D_PINONREMOTE}; // the analog 0-5 pins are also known as 14-19

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Help getting the RF M4 Receiver to work (Code Issue)

Post by adafruit_support_bill »

Yes. If you open the Serial Monitor, you can verify if those signals are being detected by the checkSwitches() function.

User avatar
joshuakane
 
Posts: 282
Joined: Sat Apr 13, 2013 4:40 pm

Re: Help getting the RF M4 Receiver to work (Code Issue)

Post by joshuakane »

I am guessing that I have something askew on the Flora.

When I run the serial monitor I constantly see a message like the following
1 pressed
0 pressed
1 pressed
0 pressed
1 pressed
0 pressed
1 pressed

Right now on the Flora D6 and D12 have (2) 1/2Watt 4.7 k ohm resistors. One on each have a resistor running to the Ground, the others are running to button 0 and 1 on the RF Transmitter. If I disconnect the transmitter it also thinks the buttons are being pressed constantly.

Unfortunately I don't have a breadboard handy, and I was just going by what was present in the "Flora Backpack" tutorial.

Any thoughts?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Help getting the RF M4 Receiver to work (Code Issue)

Post by adafruit_support_bill »

Post a photo or diagram of your connections to the Flora.

User avatar
joshuakane
 
Posts: 282
Joined: Sat Apr 13, 2013 4:40 pm

Re: Help getting the RF M4 Receiver to work (Code Issue)

Post by joshuakane »

Hey Bill,

I took some time and did some troubleshooting via sample code, and the serial monitor like you suggested, I now know that the RF module is sending and receiving fine. Thanks for the assistance on that.

I also went and refactored the code I wrote earlier but I still only get the strip of pixels to initialize green. No matter what I set this startup value to, it will light all 20 of them as green. Even of I set the color to red or blue it alway initializes green.

I also cannot get any of the Pixels to turn off, or change color at all.

Thanks,

Joshua


Code: Select all

// Set the first variable to the NUMBER of pixels. 20 = 20 pixels in a row
// The LED's in this strip will be 20 per the specifications of the costume
Adafruit_WS2801 strip = Adafruit_WS2801(20,stripDataPin,stripClockPin);

Code: Select all

void setup()
{
  pinMode(inPinC, INPUT);
  pinMode(inPinD, INPUT);
//  digitalWrite(outPin, current_state); // setup the Output LED for initial state

// Startup the LED Strip
  strip.begin();
// Update the strip, to start they are all 'off'
  strip.show();
 
//What I was looking for was something that would initilize green, powering up one by one. 
  colorWipe(Color(0, 255, 0), 75);  // green fill
}
Even when I set the inital line to

colorWipe(Color(255, 255, 0), 75); // yellow fill

I still get an inital startup of Green

Code: Select all

#include <Adafruit_WS2801.h>
#include <Wire.h>
#include <SPI.h>


//
// LEDS
//
int health = 20;
int stripDataPin  = 10;    // Yellow wire on Adafruit Pixels
int stripClockPin = 9;    // Green wire on Adafruit Pixels

// Set the first variable to the NUMBER of pixels. 20 = 20 pixels in a row
// The LED's in this strip will be 20 per the specifications of the costume
Adafruit_WS2801 strip = Adafruit_WS2801(20,stripDataPin,stripClockPin);

//
// RF Pins
//

int inPinC = 6;         // the number of the input pin
int inPinD = 12;       // the number of the input pin


int counterC = 0;       // how many times we have seen new value    
int counterD = 0;       // how many times we have seen new value
int readingC;           // the current value read from the input pin C
int readingD;           // the current value read from the input pin D
int current_stateC = LOW;    // the debounced input value
int current_stateD = LOW;    // the debounced input value

// the following variable is a long because the time, measured in milliseconds,
// will quickly become a bigger number than can be stored in an int.
long timeC = 0;         // the last time the output pin was sampled
long timeD = 0;         // the last time the output pin was sampled

int debounce_count = 2; // number of millis/samples to consider before declaring a debounced input



void setup()
{
  pinMode(inPinC, INPUT);
  pinMode(inPinD, INPUT);
//  digitalWrite(outPin, current_state); // setup the Output LED for initial state

// Startup the LED Strip
  strip.begin();
// Update the strip, to start they are all 'off'
  strip.show();
 
//What I was looking for was something that would initilize green, powering up one by one. 
  colorWipe(Color(0, 255, 0), 75);  // green fill
}


void loop()
{
  // If we have gone on to the next millisecond
  if(millis() != timeC)
  {
    readingC = digitalRead(inPinC);

    if(readingC == current_stateC && counterC > 0)
    {
      counterC--;
    }
    if(readingC != current_stateC)
    {
       counterC++; 
    }
    // If the Input has shown the same value for long enough let's switch it
    if(counterC >= debounce_count)
    {
      counterC = 0;
      current_stateC = readingC;
      Serial.println("MedPack");
      MedPack();

    }
   timeC = millis();
  }




  // If we have gone on to the next millisecond
  if(millis() != timeD)
  {
    readingD = digitalRead(inPinD);

    if(readingD == current_stateD && counterD > 0)
    {
      counterD--;
    }
    if(readingD != current_stateD)
    {
       counterD++; 
    }
    // If the Input has shown the same value for long enough let's switch it
    if(counterD >= debounce_count)
    {
      counterD = 0;
      current_stateD = readingD;
      Serial.println("TakeDamage");
      TakeDamage();
    }
    timeD = millis();
  }
}

// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
  uint32_t c;
  c = 0;
  c <<= 8;
  c |= 255;
  c <<= 8;
  c |= 0;
  return c;
}

void colorWipe(uint32_t c, uint8_t wait) {
  int i;

  for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}


void TakeDamage(){
int i;
  //G
  if(health <= 20 && health > 10){
      for (i=0; i < health; i++) {
        strip.setPixelColor(i,Color(0, 255, 0));
        strip.show();
        delay(50);
      }
      
      for (i=health; i < 20; i++) {
        strip.setPixelColor(i,Color(0, 0, 0));
        strip.show();
        delay(50);
        
      }
    }
    
  //Y
  if(health <= 10 && health > 5){
      for (i=0; i < health; i++) {
        strip.setPixelColor(i,Color(255, 255, 0));
        strip.show();
        delay(50);
      }
      
      for (i=health; i < 20; i++) {
        strip.setPixelColor(i,Color(0, 0, 0));
        strip.show();
        delay(50);
      }
  }
  
  //R
  if(health <= 5){
      for (i=0; i < health; i++) {
        strip.setPixelColor(i,Color(0, 0, 255));
        strip.show();
        delay(50);
      }
      
      for (i=health; i < 20; i++) {
        strip.setPixelColor(i,Color(0, 0, 0));
        strip.show();
        delay(50);
      }
  }
  
  health--;
     Serial.println("Damage Taken");
     Serial.println(health);
}
    
void MedPack(){
int i;
  //G
  if(health >= 20 && health < 10){
      for (i=0; i < health; i++) {
        strip.setPixelColor(i,Color(0, 255, 0));
        strip.show();
        delay(50);
      }
      
      for (i=health; i > 20; i++) {
        strip.setPixelColor(i,Color(0, 0, 0));
        strip.show();
        delay(50);
      }
    }
    
  //Y
  if(health >= 10 && health < 5){
      for (i=0; i < health; i++) {
        strip.setPixelColor(i,Color(255, 255, 0));
        strip.show();
        delay(50);
      }
      
      for (i=health; i > 20; i++) {
        strip.setPixelColor(i,Color(0, 0, 0));
        strip.show();
        delay(50);
      }
  }
  
  //R
  if(health >= 5){
      for (i=0; i > health; i++) {
        strip.setPixelColor(i,Color(0, 0, 255));
        strip.show();
        delay(50);
      }
      
      for (i=health; i > 20; i++) {
        strip.setPixelColor(i,Color(0, 0, 0));
        strip.show();
        delay(50);
      }
  }
  health++; 
    Serial.println("Healed");
    Serial.println(health);
}

User avatar
joshuakane
 
Posts: 282
Joined: Sat Apr 13, 2013 4:40 pm

Re: Help getting the RF M4 Receiver to work (Code Issue)

Post by joshuakane »

Figured out my problem, it turns out I am a bonehead :-)

While Debugging I changed some values and never changed them back

Code: Select all

{
  uint32_t c;
  c = 0;
  c <<= 8;
  c |= 255;
  c <<= 8;
  c |= 0;
  return c;
}
When it should have been

Code: Select all

{
  uint32_t c;
  c = r;
  c <<= 8;
  c |= g;
  c <<= 8;
  c |= b;
  return c;
}

User avatar
joshuakane
 
Posts: 282
Joined: Sat Apr 13, 2013 4:40 pm

Re: Help getting the RF M4 Receiver to work (Code Issue)

Post by joshuakane »

Thanks for all your help today Bill!

I really appreciate the time and help!! :D :D :D :D

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Help getting the RF M4 Receiver to work (Code Issue)

Post by adafruit_support_bill »

Glad you got it working! :)

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”