 Why is my sine wave being limited?

I’m using the Digispark board, which uses the Arduino IDE, and I’m pulling my hair (what’s left of it) out trying to figure out why my avgScaled variable is limiting the height of my sine wave.

At the “if” statement in the code (at the bottom of the post) which reads: if (avgScaled > 10) //set threshold to 10

no matter what value I use for the “greater than” cut off here, my sine wave peaks at that height and then starts back down.

Below is the output I get in my pseudo-serial window (the DigiKeyboard library produces serial window like results; the digispark doesn’t have the serial function built in):

---------------------------
SinVal=0.00
Output voltage to TA cell=50
Output voltage to EB cell=50
Input voltage=0
Resting State=0
Mean voltage=0
SinVal=0.02
Output voltage to TA cell=50
Output voltage to EB cell=50
Input voltage=0
Resting State=4
Mean voltage=2
SinVal=0.03
Output voltage to TA cell=50
Output voltage to EB cell=50
Input voltage=0
Resting State=8
Mean voltage=4
SinVal=0.05
Output voltage to TA cell=50
Output voltage to EB cell=50
Input voltage=0
Resting State=13
Mean voltage=6
SinVal=0.07
Output voltage to TA cell=50
Output voltage to EB cell=50
Input voltage=0
Resting State=17
Mean voltage=8
SinVal=0.07
Output voltage to TA cell=50
Output voltage to EB cell=50
Input voltage=0
Resting State=17
Mean voltage=8
SinVal=0.05
Output voltage to TA cell=50
Output voltage to EB cell=50
Input voltage=0
Resting State=13
Mean voltage=6
SinVal=0.03
Output voltage to TA cell=50
Output voltage to EB cell=50
Input voltage=0
Resting State=8
Mean voltage=4
SinVal=0.02
Output voltage to TA cell=50
Output voltage to EB cell=50
Input voltage=0
Resting State=4
Mean voltage=2
SinVal=0.00
Output voltage to TA cell=50
Output voltage to EB cell=50
Input voltage=0
Resting State=0
Mean voltage=0
SinVal=0.02
Output voltage to TA cell=50
Output voltage to EB cell=50
Input voltage=0
Resting State=4
Mean voltage=2
SinVal=0.03
Output voltage to TA cell=50
Output voltage to EB cell=50
Input voltage=0
Resting State=8
Mean voltage=4

My “Mean voltage” (the avgScaled integer) always approaches but never goes above the limit set in my if statement. So if I increase it to 100 in the if statement, the Mean voltage will near 100 before coming back down. The SinVal is what’s driving this; it never reaches 1 as it should. I’m a relative noob, and I feel like I’m missing something simple here but can’t figure out what it is.

Sketch:

//Inhibitory Endogenous Burster
//Cell A or B

#include "DigiKeyboard.h"

int cell1 = 1; //digital pin 2
int cell1Raw = 0;
int cell1Scaled = 0;
int avgScaled = 0;
int internalState; //value output to the PWM (analog out)
float sinVal;
const int taOut = 0;
const int ebOut = 1;
int outputLevel;
int IPSP = 0; //IPSP gives synaptic weight of 0
int noInput = 50;

void setup(){

//P0, P1, and P4 are capable of hardware PWM (analogWrite).
//pinMode(0, OUTPUT); //0 is P0, 1 is P1, 4 is P4 - unlike the analog inputs,
//for analog (PWM) outputs the pin number matches the port number.
pinMode(taOut,OUTPUT); //digital pin 0; to tonically active A cell
pinMode(ebOut, OUTPUT); //digital pin 1; to other endogenous burster cell
}

void loop(){

for (int x=0; x<180; x++) {
internalState = int(sinVal*255); //pwmVal is internal state of endogenously bursting cell

cell1Raw = analogRead(cell1);// input from SFA cell a or b
cell1Scaled = (cell1Raw / 4);

avgScaled = ((cell1Scaled + internalState) / 2); // calculate the average voltage of the 2 inputs and put it in avgScaled

if (avgScaled > 10) //set threshold to 10
{
outputLevel = IPSP; //inhibitory neuron activation leads to IPSP and synaptic weight of 0
}

else
{
outputLevel = noInput;
{

analogWrite(taOut, outputLevel);
analogWrite(ebOut, outputLevel);

DigiKeyboard.print("SinVal=");
DigiKeyboard.println(sinVal);
DigiKeyboard.print("Output voltage to TA cell=");
DigiKeyboard.println(outputLevel);
DigiKeyboard.print("Output voltage to EB cell=");
DigiKeyboard.println(outputLevel);
DigiKeyboard.print("Input voltage="); //from SFA cell a or b
//should map to realistic voltages
DigiKeyboard.println(cell1Scaled);
DigiKeyboard.print("Resting State="); //print initial state of the cell
DigiKeyboard.println(internalState);
DigiKeyboard.print("Mean voltage=");
DigiKeyboard.println(avgScaled);
DigiKeyboard.delay(10);
}

}
}}

TIA,

Joe

Try this

Grumpy_Mike: Try this

So I tried this and it didn't make any difference. I had previously tried the Spark Fun sine wave code found here https://www.sparkfun.com/tutorials/329 that also uses a floating point variable instead of an integer, and had the same limiting issues with that. Playing around with it tonight, I moved the calls to DigiKeyboard.print up higher in the sketch, to just after the for loop, and the problem went away. Damned if I know why though.

Joe

Well post the working code and we wil try and see.

Hi,

int avgScaled = 0;

If you are looking for average shouldn't it be float instead of int.

Tom.... :)

The new sketch. The only thing that has been changed is moving the DigiKeyboard calls to just after the for loop. Works the way I intended now.

//Inhibitory Endogenous Burster
//Cell A or B

#include “DigiKeyboard.h”

int cell1 = 1; //digital pin 2
int cell1Raw = 0;
int cell1Scaled = 0;
int avgScaled = 0;
int internalState; //value output to the PWM (analog out)
float sinVal;
const int taOut = 0;
const int ebOut = 1;
int outputLevel;
int IPSP = 0; //IPSP gives synaptic weight of 0
int noInput = 50;

void setup(){

//P0, P1, and P4 are capable of hardware PWM (analogWrite).
//pinMode(0, OUTPUT); //0 is P0, 1 is P1, 4 is P4 - unlike the analog inputs,
//for analog (PWM) outputs the pin number matches the port number.
pinMode(taOut,OUTPUT); //digital pin 0; to tonically active A cell
pinMode(ebOut, OUTPUT); //digital pin 1; to other endogenous burster cell
}

void loop(){

for (int x=0; x<180; x++) {
internalState = int(sinVal*255); //pwmVal is internal state of endogenously bursting cell

DigiKeyboard.print(“SinVal=”);
DigiKeyboard.println(sinVal);
DigiKeyboard.print(“Output voltage to TA cell=”);
DigiKeyboard.println(outputLevel);
DigiKeyboard.print(“Output voltage to EB cell=”);
DigiKeyboard.println(outputLevel);
DigiKeyboard.print(“Input voltage=”); //from SFA cell a or b
//should map to realistic voltages
DigiKeyboard.println(cell1Scaled);
DigiKeyboard.print(“Resting State=”); //print initial state of the cell
DigiKeyboard.println(internalState);
DigiKeyboard.print(“Mean voltage=”);
DigiKeyboard.println(avgScaled);

cell1Raw = analogRead(cell1);// input from SFA cell a or b
cell1Scaled = (cell1Raw / 4);

avgScaled = ((cell1Scaled + internalState) / 2); // calculate the average voltage of the 2 inputs and put it in avgScaled

if (avgScaled > 10) //set threshold to 10
{
outputLevel = IPSP; //inhibitory neuron activation leads to IPSP and synaptic weight of 0
}

else
{
outputLevel = noInput;
{

analogWrite(taOut, outputLevel);
analogWrite(ebOut, outputLevel);

}}}}

}}}}

Don't you just hate it when that happens?

Try using the auto format tool before posting.

The SinVal is what's driving this; it never reaches 1 as it should.

Why? What is the value of the angle? And most important, what is the value of the angle in radians? Can you print that too?