Trouble with analogWrite causing red flash when calling from a function like tinker

So, tinker analog write seems to work famously.
When I try to call analogWrite from any function I create, or from a function that is attached to a subscribe, I get a red flash.
The same type of operation in tinker works fine. If I add my function to tinker in addition to tinker it fails there too.
Why does analogwrite work in tinker but not in my functions. what is going on? I had a program working with a photon for years but this just wont run on my argons.

code snippet:

int setdac(String command) {
        pinMode(D2, OUTPUT);
        analogWrite(D2, command.toInt()); //output PWM
}

here is the code from tinker with the analog write:

int tinkerAnalogWrite(String command)
{
	//convert ascii to integer
	int pinNumber = command.charAt(1) - '0';
	//Sanity check to see if the pin numbers are within limits
	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’m stumped as to why this runs no crash but my simpler function crashes.
I’m sending a numeric value of 50 fwiw.

If you look closely your attempt is missing one crucial statement that is present in the Tinker function and it hasn’t got anything to do with analogWrite().

You are failing to return an int while the function signature requires you to do so.


BTW, I also reformatted your code snippets to display as code blocks.
That’s done by means of the image “Preformatted Text” feature.

1 Like

WOW I have fought this for a while. Thanks for the help!
I added a return and now all is happy!

1 Like

I’m trying to use a timer to fire a publish event to trigger a webhook. When the timer fires I’m getting a red flash again.

If I change the code and never start the timer, I can trigger it from the console and it works fine. The argon will receive the data I am expecting.

Something seems amiss when trying to use the timer.
Any thoughts?

Timer cointimer(15000, cointimerfunction);

void setup() {
    Serial.begin(230400);
    //pinMode(DAC1, OUTPUT);
    pinMode(pwmpin, OUTPUT);
    pinMode(ledpin, OUTPUT);
    Particle.function("setgauge",setdac);
    Particle.subscribe("hook-response/bchpls", bitcoinHandler);  // Subscribe to the webhook response
    cointimer.start();
   cointimerfunction();  // Trigger initial bitcoin webhook
   digitalWrite(ledpin,LOW);
}

void bitcoinHandler(const char *event, const char *data) {  // This is called when the Photon recives the webhook data.
    String latest = String(data);                  // Get the latest price of Bitcoin
    Serial.print("BCH Price: ");
    Serial.println(latest);                                     // Print it to serial
    coindac(latest);
    digitalWrite(ledpin, HIGH);
    
}

void cointimerfunction(){
     Particle.publish("bchpls");   
}

We’d also need to see the timer callback function.
Also, when you experience a red SOS panic flash it would be important to state how many slow flashes you see after the initial SOS (… — …) code.

A potential problem could be the limited stack of the timer thread. If this is the case you’d see an SOS+13 panic crash.

ok I updated the post above with the other code.
It seems to be flashing about 10 times maybe 9… but they do not flash evenly. It is hard to tell.

ok its the SOS and a hard fault single blink after that.
https://docs.particle.io/tutorials/device-os/led/photon/

This will explain the crash
https://docs.particle.io/reference/device-os/firmware/#software-timers

To do timed publishes you can use the Software Timer to set a flag and then act accordingly in loop().
Or you create a dedicated publishing thread that also does the timing or uses a sync object which you control from the Software Timer.

Are you really need a Timer for that ? or thread ?
what about something like this:

unsigned long interval = 0;
....
rest of your code...
....
void loop() {
...... some other stuff....
 if (millis() - interval > 15000) {
  interval = millis(); 
  Particle.publish("bchpls"); 
  }  
 ...... some other stuff_2....
}

or if you really want Timer:

bool some_flag = false;
Timer cointimer(15000, cointimerfunction);
....
rest of your code...
....
void loop() {
...... some other stuff....
 if(some_flag){
   Particle.publish("bchpls");
   some_flag = false;
  }  
 ...... some other stuff_2....
}

void cointimerfunction(){
     some_flag = true;
        
}

Awesome, thank you. Working now with no timer. Bummer. Timers used to be so useful on the old photon.

// Mac McAlpine UNCC MEGR3171 Indroduction to Instrumentation
//pwm with 3K in series with the 1ma FS meter seems to be an accurate match.

int ledpin = D7;
int pwmpin = D2;
int dacset = 0;
int interval = 10000;
int lastinterval = 0;

//Timer cointimer(15000, cointimerfunction);

void setup() {
    Serial.begin(230400);
    //pinMode(DAC1, OUTPUT);
    pinMode(pwmpin, OUTPUT);
    pinMode(ledpin, OUTPUT);
    Particle.function("setgauge",setdac);
    //Particle.subscribe("macsboost/setdac", setdacsub, MY_DEVICES);  // Subscribe to the webhook response
    //Particle.subscribe("hook-response/bitcoin2", bitcoinHandler, MY_DEVICES);  // Subscribe to the webhook response
    Particle.subscribe("hook-response/bchpls", bitcoinHandler);  // Subscribe to the webhook response
   // cointimer.start();
   cointimerfunction();  // Trigger initial bitcoin webhook
   digitalWrite(ledpin,LOW);
    lastinterval = millis();
}


void cointimerfunction(){
     //Particle.publish("bchpls");
     //Particle.publish("bitcoin2");  // Trigger bitcoin webhook
     bool success = Particle.publish("bchpls");  // Trigger bitcoin webhook
     if (success) 
     {
         digitalWrite(ledpin,HIGH);
         delay(100);
     }
     
}


void loop() {
    int x=0;
    if (millis()-lastinterval>interval){ 
        lastinterval = millis();
        cointimerfunction();  // Trigger initial bitcoin webhook
    }  
    /*    for (x=0; x<256; x++) {  //photon DAC current limit approx 10mA 
        analogWrite(DAC1, x);  //max is 4095
        analogWrite(pwmpin, x, 500); //last value is frequency in hz, 1-65535  max is 255
        Serial.println(x); //write is for byte value
        delay(10);
        
        // sets DAC pin to an output voltage of 1024/4095 * 3.3V = 0.825V.
        }
        delay(1000);
     */   
     //analogWrite(pwmpin,random(235),500); //generates randoms up to 235 using 256
     //delay(250);
     //analogWrite(pwmpin, dacset); //output PWM
     
        
    //digitalWrite(ledpin,!digitalRead(ledpin));
    //delay(100);
    digitalWrite(ledpin,LOW);
}

int setdac(String command) {
    int commandint = command.toInt();
    Serial.println(command);
    if ((commandint >=0) && (commandint <=100)){
        commandint = map(commandint, 0, 100, 0, 255); //in low, high to out low, high
        analogWrite(pwmpin, commandint, 500); //output PWM

    }
    return 1;//required
}



void bitcoinHandler(const char *event, const char *data) {  // This is called when the Photon recives the webhook data.
    String latest = String(data);                  // Get the latest price of Bitcoin
    Serial.print("BCH Price: ");
    Serial.println(latest);                                     // Print it to serial
    coindac(latest);
    digitalWrite(ledpin, HIGH);
    
}

int coindac(String command) {
    int commandint = command.toInt();
     if ((commandint >=1) && (commandint <=10000)){
        commandint = map(commandint, 0, 10000, 0, 255); //in low, high to out low, high
        analogWrite(pwmpin, commandint, 500); 
    }
    return 1;
}
    

The webhook uses the following url below for bch price
The response template is : {{USD}}

https://min-api.cryptocompare.com/data/price?fsym=BCH&tsyms=USD

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.