Newbie Advice please

Thanks Peter

although you may have just spoken ancient greek to me, think im going to have to master the basic programming before i head into shift registers and servo drivers,

1 Like

PCB Manufacturer, No idea depends on your location, im making my own from a cnc machine (for the drilling) i have here and a developing kit i bought online but my board is only basic

im currently using the developer breadboards until i have my final circuits

i’d suggest google is your friend on that situation, depends on the complexity of the board and your location

You are likely going to need to use some of the arduino tutorials but luckily there are lots of them aimed at beginners. In this case pwm.setPWM is a function call to a library that drives the pwm board. the first argument is the number of the servo you mean (probably from 0 to 15) I don’t know what the 0 is without reading the library documentation and the pulselen argument is a number between 0 and 4095 that will set the interval of the pwm pulse (which in turn sets the position of the servo). So your application that outputs the servo channel needs to set that in to servonum by an assignment like:

servonum = channel_number_from_your_source;
pulselen = servo_position_from_your_source;
pwm.setPWM(servonum, 0, pulselen);

these three lines set on servo position. Now you need to figure out how you get the values from your source in to the variables and put the whole thing in a loop that cycles around forever (like your current program) reading values from your source and writing them to the variables above and calling the servo library. After that needs to be similar code that drives your shift registers. Copying other folks existing code that does mostly what you want is a good starting point, the problem is finding code that does mostly what you want out of the sea of code out there, but google is your friend.

Peter

fantastic thanks Peter
have gone back to basic coding at present see if i can get the logistics right, so have taken the servo driver out for now and am running off pin 2 for testing in the software at the moment

Hi Peter

ive had some success going back to basics and using the pwm ports of the uno and a basic code, which i’ll post at the bottom of here, could i be a real pain and ask if you can give me some advice on how to modify the code to use with the servo board, the code attached is for the first 3 items and i have 8 servo channels in total, hence the need for the pca board

#include <Servo.h>
Servo Pumpkin1;
Servo Pumpkin2;
Servo Pumpkin3;
int pos = 0;

int Chan1 = 3;     //PWM-Servo 1
int Chan2 = 5;     //PWM-Servo 2
int Chan3 = 6;     //PWM-Servo 3

int i = 0;     // Loop counter
int incomingByte[8];   
int val1;
int val2;
int val3;

//setup the pins/ inputs & outputs
void setup()
{
  Serial.begin(57600);   

  pinMode(Chan1, OUTPUT);
  Pumpkin1.attach(3);
  pinMode(Chan2, OUTPUT);
  Pumpkin2.attach(5);
  pinMode(Chan3, OUTPUT);
  Pumpkin3.attach(6);
}
void loop(){
  if (Serial.available() >= 8) {
}
    for (int i=0; i<9; i++) {
      // read each byte
      incomingByte[i] = Serial.read();
    }
    //Pumpkin1
    val1 = incomingByte[0];                  
    val1 = map(val1, 0, 255, 0, 45);    
    Pumpkin1.write(val1);                   
    delay(.1);                            
    //Pumpkin2
    val2 = incomingByte[1];
    val2 = map(val2, 0, 255, 0, 45);    
    delay(.1);
    //Pumpkin3
    val3 = incomingByte[2];
    val3 = map(val3, 0, 255, 0, 45);     
    Pumpkin3.write(val3);
    delay(.1);
    }

There are a number of ways of doing this so here are some options

  1. It appears that you are reading 8 bytes from the serial port to give you the position values of the 8 servos (with only three in use at the moment). So you could read only two bytes from the serial port (channel_number, position_value) and output that to the servo board. That is the minimal code on the arduino but pushes determining the channel number to the source of the input data. This code should do that (noting that I haven’t tested this):
void loop(){
 if (Serial.available() <= 2) {
   chan = Serial.read();
   pos   = Serial.read();
   pwm.setPWM(chan, 0, pos);
 }

}

You need to add the definitions of the variables and run the initialization code for the pwm board in your setup function. This code should loop around doing nothing until it sees two bytes from the serial port. Once it has two bytes on the serial port, it sets the channel number as the first one and the position as the second one (you can either control the range of the position value at the source or use the map function as you last example does to control the range of the position byte.

  1. You can replace the

Pumpkin1.write(val1);

in the code above with

pwm.setPWM(0, 0, incomingByte[0]);

pwm.setPWM(1, 0, incomingByte[1]);

up to 7 which will give you the first 8 servos on the servo board with minimal changes to the above code (again you may need to add map calls to control the range if you need to). I’d probably use option 1 and would probably add a third byte so you have

Servo, channel, positioon as the three bytes for a servo command and

SR, data_byte, data_byte

for the shift register commands. An if statement like this selects between the two commands:

#define SERVO 0
#define SR 1

void loop(){

 if (Serial.available() <= 3) {
    cmd = Serial.read();
    if (cmd == SERVO) {
         ... servo code above
    }
    if (cmd == SR) {
         .. shift register code
    }

}

the source of the data sets the first byte to 0 to select the servo and 1 to select the shift register. At present the two commands are the same length, making them different lengths would be a fair bit more complex (easier to leave them the same length I expect).

Peter

if this works where’s the buy you a beer button, been racking my brain on this all day

It just takes practice, I have the advantage of been doing this for 30 or 40 years so its easy for me. It wasn’t when I started.

Peter

was thinking of outsourcing this to my old man he has more experience on coding than me i just design and build usually, but thought im never going to learn if i dont try, bloody hell ive gone grey on top trying this small project lol

i did find and rearrange a code for my shift registers, im still waiting on them arriving hopefully it’ll be sometime this week

//code to use one or multiple 595 shift registers, generic serial plugin
//and VixenLights

#include <Shifter.h>

#define SER_Pin 8 //SER_IN
#define RCLK_Pin 9 //L_CLOCK
#define SRCLK_Pin 10 //CLOCK

#define NUM_REGISTERS 2 //how many registers are in the chain


//initaize shifter using the Shifter library
Shifter shifter(SER_Pin, RCLK_Pin, SRCLK_Pin, NUM_REGISTERS);
int incomingByte[8]; // array to store the 8 values from the serial port

void setup(){
Serial.begin(57600); 
}

void loop(){

if (Serial.available() >= 8) {
// read the oldest byte in the serial buffer:
for (int i=0; i<8; i++) {
// read each byte
incomingByte[i] = Serial.read();
}

shifter.clear(); //set all pins on the shift register chain to LOW
shifter.write(); //send changes to the chain and display them

//delay(1000);

//0-7 for 8 shift register outputs, continue with 8-15 for second 595 etc. for multiple 595's
shifter.setPin(0, incomingByte[0]); 
shifter.setPin(1, incomingByte[1]);
shifter.setPin(2, incomingByte[2]);
shifter.setPin(3, incomingByte[3]);
shifter.setPin(4, incomingByte[4]);
shifter.setPin(5, incomingByte[5]);
shifter.setPin(6, incomingByte[6]);
shifter.setPin(7, incomingByte[7]);
shifter.setPin(8, incomingByte[8]); 
shifter.setPin(9, incomingByte[9]);
shifter.setPin(10, incomingByte[10]);

shifter.write(); //send changes to the chain and display them

//delay(1000);

would this interfere with the ic2 programming of the servo board?

Shouldn’t, but you will have to figure out how to share the serial data between the two functions. Probably by switching on the value of the first byte transmitted to select the servos or the shift register and keep state because of the different data lengths. There is also an error in the above code,

int incomingByte[8]

needs to be

int incomingByte[11]

to have enough room for 11 values. Currently the last three values 8,9 and 10 will get random values from whatever is in the memory beyond the end of the array, they won’t be set from the serial input.

if (Serial.available() >= 8) {

needs to be

if (Serial.available() >= 11) {

and

for (int i=0; i<8; i++) {

needs to be

for (int i=0; i<12; i++) {

to get all 11 values from the serial line. This is also inefficient in that it is using one byte for each bit when you could be only sending two bytes for the needed 11 bits although your library call would need to change to some other function that will serialize bytes (I assume there is one in that library but haven’t looked).

Peter

thanks for the error corrections peter

have now amended and set the code in prep for arrival of parts

still banging my head with this servo board, ive been told now to use headers for identifers etc and im just getting swapped, plus i feel as though im taking the mick asking you to debug my issues and write my code

Honestly your choice of hardware is your main issue. If you had used an ESP8266 based board and individually addressable LEDs like i mentioned before your code would be only a few lines.

Like for the LEDs you include the fastled library, inintialize the LED array replacing NUM_LEDS with the number of LEDs you have connected.

#include <FastLED.h>
FASTLED_USING_NAMESPACE
CRGB leds[NUM_LEDS];

Then you setup it up with a single line of code. Replacing LED_TYPE with your LED model number, DATA_PIN with the pin you are using for the LEDs, COLOR_ORDER is replaced with GRB or RGB etc. The last thing is NUM_LEDS is replaced with the total number of LEDs being used.

  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);

Then to write to an LED you simply type:

leds[LED_NUMBER] = HUE;
fastled.show;

You replace LED_NUMBER with the led you want to change the color of and replace the HUE with the hue you want from 0-255. You can also address the colors as named colors like blue, red, aqua with a slight change. There are lots of examples on how to use the library. You can set all the LED colors before calling fastled.show and it writes out all the LED data in a single go. There are also functions to set all the LEDs the same color in a single go or to fill them with a rainbow of colors etc.

Then for the servos because the ESP boards can use almost all the pins a PWM pins you can just simply write a PWM value to them like you are doing with your first 8 servos on the Arduino.

If you insist on using your current hard to use hardware may I suggest you at least look for libraries to make coding easier instead of trying to write it all yourself. Like for the I2C stuff you can use the wire library from Arduino or many other serial libraries available online. I am sure you will also find libraries for your shift registers and servo board etc.

EDIT: I also have boxes of hundreds of discrete LEDs and would never consider using them for anything other than indicators due to the time it would take to connect and code them vs the time it takes to connect 3 wires and write simple code for individually addressable LEDs. I would say this is even more true for someone new coding and/or hardware

i understand my choice of hardware may not have been ideal, but with this being my first attempt at anything like this and not wanting to have to spend out on new led strings or addressable leds i saw these 2 devices and at a total cost of £6.58 for the uno pwm board and parts to build my ssr borad, its not as if i have spent a fortune if i give up or it gets away with me.
still doesnt stop me from feeling like a pest, but then again everyone has to start somewhere,

That is exactly correct, we all were new to this at some point and asked people for help. I’m just paying the help I got forward. I’m not actually writing your code for you just providing suggestions on ways to do it which often are not obvious.

Peter

I wasn’t trying to say you are doing anything wrong I just hate to see people suffer and having to write to shift registers etc is torture to me. Either way it is good to learn how to use these devices but I would highly recommend looking for libraries to make it easier. Most of these things have been done before and a lot of those people wrote libraries to make their life easier and shared them to make everyone’s life easier.

thanks sub, do like the challenge, its keeping my mind distracted at present, just wish i could understand it a little better than what i do, guess i will in time

Seems ok, give them a try. Seem reasonably priced

Hi Peter

My parts finally arrived today I have the prototype board built with the shift registers programmed it with the code you helped me with and when I ran vixen it worked, all 11 channels am made up… but I have one issue fading. I’ve been doing some reading this evening, can I use this to achieve what I’m after?

Vixen sends the brightness out so my thinking is I’d only need to add the enabled line to the bringing wouldn’t I ?