Go Down

Topic: Need help getting Modbus input to pwm output. (Read 376 times) previous topic - next topic

nikk4s

I am sending modbus word arrays to an arduino mega2560.  I have been able to get the analog read part working but cannot seem to get the analog write to pwm pins working.

Code: [Select]
#include <Mudbus.h>

#include <SPI.h>
#include <Ethernet.h>

//#include "Mudbus.h"

Mudbus Mb;
//Function codes 1(read coils), 3(read registers), 5(write coil), 6(write register)
//signed int Mb.R[0 to 125] and bool Mb.C[0 to 128] MB_N_R MB_N_C
//Port 502 (defined in Mudbus.h) MB_PORT


void setup()
{
  uint8_t mac[]     = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x06 };
  uint8_t ip[]      = { 192, 168, 0, 59 };
  uint8_t gateway[] = { 192, 168, 0, 1 };
  uint8_t subnet[]  = { 255, 255, 255, 0 };
  Ethernet.begin(mac, ip, gateway, subnet);
  //Avoid pins 4,10,11,12,13 when using ethernet shield

  delay(5000);
  Serial.begin(9600);

 

  //   INPUTS
 
  //  OUTPUTS
 
}

  int five = 0;
  int six = 0;
  int seven = 0;
  int eight = 0;
  int nine = 0;
  int twelve = 0;
  int thirteen = 0;
 
void loop()
{
  Mb.Run();
 
  //Analog inputs 0-1023
  Mb.R[0] = analogRead(A0); //pin A0 to Mb.R[0]
  Mb.R[1] = analogRead(A1);
  Mb.R[2] = analogRead(A2);
  Mb.R[3] = analogRead(A3);
  Mb.R[4] = analogRead(A4);
  Mb.R[5] = analogRead(A5);
  Mb.R[6] = analogRead(A6);
  Mb.R[7] = analogRead(A7);
  Mb.R[8] = analogRead(A8);
  Mb.R[9] = analogRead(A9);
//  Mb.R[10] = analogRead(A10); //work on removing
//  Mb.R[11] = analogRead(A11); //work on removing
//  Mb.R[12] = analogRead(A12); //work on removing
//  Mb.R[13] = analogRead(A13); //work on removing
  Mb.R[10] = analogRead(A14);
  Mb.R[11] = analogRead(A15);

   //Digital inputs
   
 
 

  //Analog outputs 0-255
//
//  analogWrite(4, Mb.R[12]); Leave this one out

//  analogWrite(5, Mb.R[12]);
//  analogWrite(6, Mb.R[13]);
//  analogWrite(7, Mb.R[14]);
//  analogWrite(8, Mb.R[15]);
//  analogWrite(9, Mb.R[16]);
//  analogWrite(12, Mb.R[17]);
//  analogWrite(13, Mb.R[18]);

  five = Mb.R[12];
  six = Mb.R[13];
  seven = Mb.R[14];
  eight = Mb.R[15];
  nine = Mb.R[16];
  twelve = Mb.R[17];
  thirteen = Mb.R[18];
 
  analogWrite(5, five);
  analogWrite(6, six);
  analogWrite(7, seven);
  analogWrite(8, eight);
  analogWrite(9, nine);
  analogWrite(12, twelve);
  analogWrite(13, thirteen); 

  //Digital outputs
 
 
}


UKHeliBob

Quote
cannot seem to get the analog write to pwm pins working.
None of your pins appear to be set as outputs.  Would that help, do you think ?

What do you see if you print the values that you are trying to output ?

Code: [Select]
  analogWrite(5, five);
Are you trying to write an int (the five variable) to a PWM pin that takes a maximum value of 255 ?


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

Juraj

None of your pins appear to be set as outputs.  Would that help, do you think ?
Quote
You do not need to call pinMode() to set the pin as an output before calling analogWrite().
https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/

one2many

the pwm setup is missing.. set the dividers, period, etc

UKHeliBob

the pwm setup is missing.. set the dividers, period, etc
Not needed if you use analogWrite() and want the standard output.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up