[Solved] Read analogWrite value from Tinker

Hi Folks,

I saw the post “Read value from Tinker analogWrite pins”, and I am looking for what is probably a simpler version of that discussion.

I simply want to read [0-255] from Tinker and latch onto that value in my Core firmware. I can see that the value is passed once when I update Tinker, but it is momentary, and I am not skilled enough to latch onto that value.

I am using A5, and I simply want to control pwm on A5…but from Tinker. I want the Tinker value to persist until I update the value on Tinker.

Please help this hardware guy!

My code:

int led = D0; 
int led2 = D7; 
int xPin = A0;
int yPin = A1;
int zPin = A2;
int tPin = A3;
int tVal = 0;
int xVal = 0;
int yVal = 0;
int zVal = 0;
int iPWM = 0;

int tinkerDigitalRead(String pin);
int tinkerDigitalWrite(String command);
int tinkerAnalogRead(String pin);
int tinkerAnalogWrite(String command);

//PUT YOUR VARIABLES HERE

void setup()
{
    Spark.function("digitalread", tinkerDigitalRead);
    Spark.function("digitalwrite", tinkerDigitalWrite);
    Spark.function("analogread", tinkerAnalogRead);
    Spark.function("analogwrite", tinkerAnalogWrite);

    //PUT YOUR SETUP CODE HERE
    pinMode(led, OUTPUT);
    pinMode(led2, OUTPUT);
    pinMode(D6,INPUT);
    
    Serial.begin(9600);

}

void loop()
{
    tVal = analogRead(tPin);
    xVal = analogRead(xPin);
    yVal = analogRead(yPin);
    zVal = analogRead(zPin);
    Serial.print(tVal);
    Serial.print(" ");
    Serial.print(xVal);
    Serial.print(" ");
    Serial.print(yVal);
    Serial.print(" ");
    Serial.println(iPWM);
    delay(50);   
    
    tinkerDigitalWrite("D7" "HIGH");
    tinkerDigitalRead("D6Val" "D6");
    tinkerAnalogRead("xVal" "A0");
    tinkerAnalogWrite("A5" "iPWM");


}

int tinkerDigitalRead(String pin) {
    int pinNumber = pin.charAt(1) - '0';
    if (pinNumber< 0 || pinNumber >7) return -1;
    if(pin.startsWith("D")) {
        pinMode(pinNumber, INPUT_PULLDOWN);
        return digitalRead(pinNumber);}
    else if (pin.startsWith("A")){
        pinMode(pinNumber+10, INPUT_PULLDOWN);
        return digitalRead(pinNumber+10);}
    return -2;}

int tinkerDigitalWrite(String command){
    bool value = 0;
    int pinNumber = command.charAt(1) - '0';
    if (pinNumber< 0 || pinNumber >7) return -1;
    if(command.substring(3,7) == "HIGH") value = 1;
    else if(command.substring(3,6) == "LOW") value = 0;
    else return -2;
    if(command.startsWith("D")){
        pinMode(pinNumber, OUTPUT);
        digitalWrite(pinNumber, value);
        return 1;}
    else if(command.startsWith("A")){
        pinMode(pinNumber+10, OUTPUT);
        digitalWrite(pinNumber+10, value);
        return 1;}
    else return -3;}

int tinkerAnalogRead(String pin){
    int pinNumber = pin.charAt(1) - '0';
    if (pinNumber< 0 || pinNumber >7) return -1;
    if(pin.startsWith("D")){
        pinMode(pinNumber, INPUT);
        return analogRead(pinNumber);}
    else if (pin.startsWith("A")){
        pinMode(pinNumber+10, INPUT);
        return analogRead(pinNumber+10);}
    return -2;}

int tinkerAnalogWrite(String command){
    int pinNumber = command.charAt(1) - '0';
    if (pinNumber< 0 || pinNumber >7) return -1;
    String value = command.substring(3);
    if(command.startsWith("D")){
        pinMode(pinNumber, OUTPUT);
        analogWrite(pinNumber, value.toInt());
        return 1;}
    else if(command.startsWith("A")){
        pinMode(pinNumber+10, OUTPUT);
        analogWrite(pinNumber+10, value.toInt());
        return 1;}
    else return -2;}

I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy

@IoT_me, what do you mean by this?
If you just slightly change the tinkerAnalogWrite() function to store the “momentary” value into a global variable you can reuse it wherever you want.

like this:

int lastAnalogVal;
...

int tinkerAnalogWrite(String command)
{
  int pinNumber = command.charAt(1) - '0';
  if (pinNumber< 0 || pinNumber >7) return -1;
  String value = command.substring(3);
  if(command.startsWith("D")) {
    pinMode(pinNumber, OUTPUT);
    analogWrite(pinNumber, (lastAnalogVal = value.toInt()));
    return 1;
  } else if(command.startsWith("A")) {
    pinMode(pinNumber+10, OUTPUT);
    analogWrite(pinNumber+10, (lastAnalogVal = value.toInt()));
    return 1;
  } else return -2;
}

Although there’d be more potential to streamline this fn, this way you’d at least get the value you are looking for for further use.

I also wouldn’t do this in loop()

void loop()
{
  tVal = analogRead(tPin);
  xVal = analogRead(xPin);
  yVal = analogRead(yPin);
  zVal = analogRead(zPin);
  ...
  tinkerDigitalWrite("D7" "HIGH"); // this should rather be 
                                   // 'tinkerDigitalWrite("D7,HIGH");'
  tinkerDigitalRead("D6Val" "D6"); // this would be 'D6Val = tinkerDigitalRead("D6");'
  tinkerAnalogRead("xVal" "A0");   // this would be 'xVal = tinkerAnalogRead("A0");' 
                                   // or use lastAnalogVal
  tinkerAnalogWrite("A5" "iPWM");  // this is a bit more tricky, since you have 
                                   // to insert the value of iPWM into the string
                                   // there are several ways to do that
}

since the Tinker App might alter your pinMode settings and the parameters you pass in there don’t fit the function signature.

Can you show me how to do that? I’m a dumb hardware guy…

Sure, look previous post, again.
I just started and posted it quickly to signal others that you’re already receiving attention :wink:
Then I started to edit and extend the answer.

1 Like

That worked the first time! And, as you suggested, I deleted the function call from the loop. It may be no big deal for you…but it’s a miracle for me. I tried all sorts of things yesterday.

thanks very much!!

If I can help you on electrical or mechanical hardware questions, let me know!

3 Likes

Thanks for the offer :smile:

If you need anything else or want to know why and how all this works - feel free to ask. There are loads of people eager to share their knowledge.

3 Likes