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