Bluetooth Serial Char array overwrites itself on new loop() iteration


hello

i trying make system can control led strip lights , bluetooth terminal command can select pre-written patterns inside arduino. using hc06 module bluetooth, using fastled library striplights , softwareserial control bluetooth.

i using robin2's "serial input basics" forum post serial capture of char array. char[] variable "command" should hold command, 3 exist, cylon, strobe , off.

cylon works correctly. once selected continue display cylon pattern until told otherwise or switched off. strobe ever strobe once , striplights go off. upon examining debug prints, shows variable "command" being cleared when new loop reached.

the serial.println() commands used debug ctrl shift m console, , myserial() commands bluetooth receiver.

the output of ctrl shift m console when entering "cylon" , pressing send on phone, few seconds later entering "strobe" , pressing send looks follows:

code: [select]

command is:
command is:
command is:
command is:
new command!
command is:cylon
command is:cylon
command is:cylon
command is:cylon
command is:cylon
command is:cylon
new command!
command is:strobe
command is:
command is:


as can see there no "new command!" indicator after strobe, yet still erases somehow.

this might easy fix , maybe requires fresh set of eyes, appreciated

regards





code: [select]
#include "fastled.h"
#include <eeprom.h>
#include <softwareserial.h>
softwareserial myserial(10, 11); // rx, tx

#define num_leds 60
#define data_pin 7


// define array of leds
crgb leds[num_leds];
string color1;
string color2;
string color3;
int delayflag = 0;
uint8_t ghue = 0; // rotating "base color" used many of patterns

const byte numchars = 64;
char receivedchars[numchars];   // array store received data
char command[numchars];
boolean newdata = false;


void setup() {
  serial.begin(9600);
  myserial.begin(9600);
  myserial.println("resetting");
  leds.addleds<ws2812, data_pin, rgb>(leds, num_leds);
  leds.setbrightness(84);

}

void loop() {
  memset(receivedchars, 0, sizeof(receivedchars));
  recvwithendmarker();
  shownewdata();

    serial.print("command is:");
    serial.println(command);
  //  serial.println(strcmp("strobe", command));

  //"cylon" pattern code
  if ( strcmp("cylon", command) == 0) {
    static uint8_t hue = 0;
    // first slide led in 1 direction
    (int = 0; < num_leds; i++) {
      // set i'th led red
      leds[i] = chsv(hue++, 255, 255);
      // show leds
      fastled.show();
      // we've shown leds, reset i'th led black
      // leds[i] = crgb::black;
      fadeall();
      delaymicroseconds(5000);
      delaymicroseconds(5000);
    }
  }


  //strobe pattern
  else if ( strcmp("strobe", command) == 0) {

    (int = 0 ; < 61 ; i++) {
      leds[i] = crgb::white;
    }

    fastled.show();
    delay(50);

    (int = 0 ; < 61 ; i++) {
      leds[i] = crgb::black;
    }

    fastled.show();
    delay(50);
  }

  //off pattern
  else if ( strcmp("off", command) == 0) {

    (int = 0 ; < 61 ; i++) {
      leds[i] = crgb::black;
    }

    fastled.show();
  }


  else {}

}


void recvwithendmarker() {
  static byte ndx = 0;
  char endmarker = '\n';
  char rc;
  newdata = false;
  while (myserial.available() > 0 && newdata == false) {
    rc = myserial.read();

    if (rc != endmarker) {
      receivedchars[ndx] = rc;
      ndx++;
      if (ndx >= numchars) {
        ndx = numchars - 1;
      }
    }
    else {
      receivedchars[ndx] = '\0'; // terminate string
      ndx = 0;
      newdata = true;
    }
  }
}

void shownewdata() {
  if (newdata == true) {
    myserial.println(receivedchars);
    serial.println("new command!");
    strcpy(command, receivedchars);
    memset(receivedchars, 0, sizeof(receivedchars));
    newdata = false;
  }
}

void fadeall() {
  (int = 0; < num_leds; i++) {
    leds[i].nscale8(250);
  }
}


your problem first 3 lines of loop. clearing out received characters s when call shownewdata copies command clearing out. 


Arduino Forum > Using Arduino > Programming Questions > Bluetooth Serial Char array overwrites itself on new loop() iteration


arduino

Comments

Popular posts from this blog

Help with codes to add 1 more shift register and add more to matrix LED

tcp client

Can't read sms using Thinker A7 communicate with Arduino