//Using a rotation matrix to generate sin and cos values
//output them on Pin A4 and input them on Pin A1 as analog values.
//Pin A4 is wired to Pin A1 for this test
A1.mode('analog');
A4.mode('analog');
//console.log(A1.getMode());
//console.log(A4.getMode());
//for angles <5 degrees sin(a)=a, where a is in radians
var s=6.28/360;
//cos^2 = 1-sin^2
var b=1.0-s*s;
// the cos
var c=Math.sqrt(b);
// a unit vector point along positive x axis
var ss=0.0;
var cc=1.0;
var sss=ss;
var ccc=cc;
for(i=0;i<361;i+=1){
//sin values
analogWrite(A4,0.5+ss/2.0);
console.log(i,' ',0.5+ss/2.0,' ',analogRead(A1));
//cos values
// analogWrite(A4,0.5+cc/2.0);
// console.log(i,' ',0.5+cc/2.0,' ',analogRead(A1));
//Matrix multiply the unit vector times the rotation matrix
// [c,-s]
// [ccc,sss] = [cc,ss] * [s, c]
//
ccc=c*cc-s*ss;
sss=s*cc+c*ss;
ss=sss;
cc=ccc;
}//next i
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Pins used are for an Espruino board.
And some of the output: