Go Down

Topic: Stripping identifier off serial string to get position number for servo (Read 132 times) previous topic - next topic

Slider69

Pretty much a newbie to programming and the Arduino. Putting together a project to control some automotive accessories. GUI is running on a Win10 tablet with bluetooth connection to a Mega 2560. There are several servos to control and a couple MOSFETs through the Processing app over bluetooth. I have had success with setting up the bluetooth connection and getting the button and toggle controls to work. The issue has become getting the servo's to work. I have processing sending a character string of d+(PWM request) for one of the controls and a character string of h+(position request) for a servo. The issue is stripping the identity character from the string and passing the numerical information to the MOSFET/servo. A stripped down version of the code in Processing:

import controlP5.*; //import control P5 library
import processing.serial.*; //import serial llibrary

Serial port; //Set serial as port
int DashDim;  //Set DashDim variable as int
int TempControl;  //set TempControl as in variable

ControlP5 cp5; //create control P5 library
PFont font1;
PFont font2;

void setup(){
  size (600, 760);  //window size
  cp5 = new ControlP5(this);  //set cp5 as ControlP5
  font1 = createFont("Bauhaus 93", 34);  //set font1
  font2 = createFont("Arial Rounded MT Bold", 28);  //set font2
 
  printArray(Serial.list());  //get serial llist to console
  port = new Serial(this,"COM21", 38400);  //set serial port for bluetooth
 
  cp5.addSlider("DashDim")  //add a slider to control dash light brightness
     .setLabel("DD")  //control voltage to LED guages with MOSFET
     .setPosition(200, 160)
     .setSize(340,50)
     .setRange(0,255)
     .setNumberOfTickMarks(5);
     
  cp5.addSlider("TempControl")  //add slider to control heat blend door
    .setPosition(200, 320)  //control heater blend door with RC servo
    .setSize(340,50)
    .setRange(0,180)
    .setNumberOfTickMarks(5);
}

void draw(){ //set window graphics
 
  background(220, 220, 220);
  fill(0, 0, 0);
  textFont(font2);
  text("Charger Control", 190, 50);
  text("Dash Dim", 20, 200);
  text("Heat Temp", 20, 360);
}

void DashDim(int TheBright){
  port.write('d'+int(TheBright));
  print("d"+int(TheBright));
  delay(145);
}

void TempControl(int TheHeat){
  port.write("h"+int(TheHeat));
  print("h"+int(TheHeat));
  delay(145);
}


:End Processing Sketch

I can see from the Processing console that it is sending d+"value" from the DashDim and h+"position from the TempControl slider.

Arduino code:

#include <Servo.h>

Servo DashDim; //Create servo object Dash Dimmer
  int posDashDim = 90;
Servo TempControl; //Servo object TempControl
  int posTempControl = 90;

void setup() {
  DashDim.attach(7);
  TempControl.attach(2);

  Serial.begin(9600); //Start serial communication USB
  Serial3.begin(38400); //Start serial communication Bluetooth
}

void loop() {

  if(Serial3.available()>0){ //if data is available to read
    delay(20);
    char val = Serial3.read(); //load variable
   
    if(val == 'd***'){
      //need code to strip the d and send the position number to the servo
        DashDim.write(v);
      }
    }


Thank you in advance for any help you can send this way!

ieee488

if(val == 'd***')  is never going to work

Serial.read reads a byte !

UKHeliBob

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

Robin2

You can completely avoid the need for identity characters if every message contains data for every output always in the same order - even if the values have not changed.

Something like <servoAval, servoBval, servoCval, mosfetVal>

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up