-
-
-
@Gordon
Thanks for your response. I think there is no short circuit. There is only one button plugged on the board, no thing else.Debounce doesn't help me, I have tried various variables. from 1 to 300.
I'm trying more than one button, I think it is not a mechanical problem.
Power saving is not important for me at the moment, I'm pluging it to direct electric source.
-
I have coded a class for this problem. Works great and stable for me. I'm sharing it.
watchClass=function(pin,callback){ var lastTime=0; lastState=1; var interval=setInterval(function(){ currentState=digitalRead(pin); currentTime=getTime(); if (currentState!=lastState) { var e={ state:currentState, lastTime:lastTime, time:currentTime } lastTime=currentTime; lastState=currentState; callback(e); } },30); } pinMode(E2, "input_pulldown"); x=new watchClass(E2,function(e){ console.log(e); });
-
-
Hello,
I'm using setwatch to detetch keypress events. I wired up a pin as button. Voltage flows as "5V -> Button -> E2 Pin"
I'm using STMF32F4 Discovery.Here is my code :
var button1=E2; pinMode(button1, "input_pulldown"); setWatch(function(e) { console.log(e); }, button1, { repeat: true, edge: "both",debounce:10});
Some times it prints console while I'm not pressing button. Some times prints 2-3 times on a button press. Some times it doesn't print anything. So, I can't understand which is the real button press event;
Here is the output :
{ "state": true, "lastTime": undefined, "time": 2106.83884495238 } { "state": false, "lastTime": 2106.83884495238, "time": 2107.13843809523 } { "state": true, "lastTime": 2107.13843809523, "time": 2102.44481219047 } { "state": false, "lastTime": 2102.44481219047, "time": 2108.03721752380 } { "state": false, "lastTime": 2108.03721752380, "time": 2106.63911619047 } { "state": true, "lastTime": 2106.63911619047, "time": 2106.43938742857 } { "state": true, "lastTime": 2106.43938742857, "time": 2107.33816685714 } { "state": false, "lastTime": 2107.33816685714, "time": 2107.53789561904 } { "state": true, "lastTime": 2107.53789561904, "time": 2107.13843809523 } { "state": true, "lastTime": 2107.13843809523, "time": 2108.03721752380 } { "state": false, "lastTime": 2108.03721752380, "time": 2107.13843809523 } { "state": false, "lastTime": 2107.13843809523, "time": 2122.91701028571 } { "state": false, "lastTime": 2122.91701028571, "time": 2122.91701028571 } { "state": false, "lastTime": 2122.91701028571, "time": 2126.01280609523 } { "lastTime": 2126.01280609523, "time": 2126.81172114285, "pin": E2, "state": true } { "state": false, "lastTime": 2126.81172114285, "time": 2127.11131428571 } { "state": true, "lastTime":2127.11131428571, "time": 2127.11131428571 } { "state": true, "lastTime": 2127.11131428571, "time": 2125.81307733333 } { "state": false, "lastTime": 2125.81307733333, "time": 2132.90344838095 } { "state": true, "lastTime": 2132.90344838095, "time": 2125.91294171428 } { "state": true, "lastTime": 2125.91294171428, "time": 2133.30290590476 } { "state": false, "lastTime": 2133.30290590476, "time": 2133.50263466666 } { "state": true, "lastTime": 2133.50263466666, "time": 2132.40412647619 } { "state": true, "lastTime": 2132.40412647619, "time": 2131.90480457142 } { "state": false, "lastTime": 2131.90480457142, "time": 2133.60249904761 }
-
Yes, I'm going to use 7 steppers in my project. It is not only about steppers, OOP is easiest way to build application for me.
By taking advantage @Gordon's code I have build my OOP like this; The way below is easy to read and understand the code for me.
modules/stepperClass.js
exports.stepperClass = function (p1, p2, p3, p4) { var step = 0; var steps = [0b0001, 0b0011, 0b0010, 0b0110, 0b0100, 0b1100, 0b1000, 0b1001]; var stepperPins = [p4, p3, p2, p1]; this.writeStep = function () { step++; digitalWrite(stepperPins, steps[step % steps.length]); }; this.stop = function () { digitalWrite(stepperPins[0], LOW); digitalWrite(stepperPins[1], LOW); digitalWrite(stepperPins[2], LOW); digitalWrite(stepperPins[3], LOW); }; this.doStep = function (cnt) { for (i = 0; i < cnt; i++) { x = 0; while (x < 10) x++;//delay a little bit this.writeStep(); } this.stop(); };
projects/main.js
var stepperClass=require('stepperClass').stepperClass; stepper1=new stepperClass(A1,A2,A3,A4); stepper2=new stepperClass(B1,B2,B3,B4); stepper3=new stepperClass(C1,C2,C3,C4); stepper1.doStep(100); .... .... ....
-
Yes, I'm doing it with projects options in Web IDE.
How to import a class that gets parameters on construction like this :
modules/stepper.js
var stepperClass = function (p1, p2, p3, p4) { var step = 0; var steps = [0b0001, 0b0011, 0b0010, 0b0110, 0b0100, 0b1100, 0b1000, 0b1001]; var stepperPins = [p4, p3, p2, p1]; this.writeStep = function () { step++; digitalWrite(stepperPins, steps[step % steps.length]); }; this.stop = function () { digitalWrite(stepperPins[0], LOW); digitalWrite(stepperPins[1], LOW); digitalWrite(stepperPins[2], LOW); digitalWrite(stepperPins[3], LOW); }; this.doStep = function (cnt) { for (i = 0; i < cnt; i++) { x = 0; while (x < 10) x++; this.writeStep(); } this.stop(); }; };
-
Hello,
I'm trying to build my application as OOP, that's why I have parted all classes in different files. I have placed those files in modules directory.
Example ;
modules/connectionClass.js
var connectionClass=function(){ getVars=function(){ return {social_id:Math.floor((Math.random() * 4)),text:zeroFill(Math.floor((Math.random() * 10) + 1),6)} }; }
modules/helper.js
function zeroFill( number, width ) { width -= number.toString().length; if ( width > 0 ) { return new Array( width + (/\./.test( number ) ? 2 : 1) ).join( '0' ) + number; } return number + ""; // always return a string }
projects/main.js
require('connectionClass'); var connection=new connectionClass();
But at the line var connection=... I'm getting this error
Uncaught Error: Field or method does not already exist, and can't
create it on undefined at line x col yWhat I am doing wrong?
-
-
First thanks all, at the end I could drive it normally. Here is my code
var step = 0; var steps = [0b0001,0b0011,0b0010,0b0110,0b0100,0b1100,0b1000,0b1001]; var stepperPins = [E13,E11,E9,E7]; // Change these to pins for your motor driver function doStep() { step++; digitalWrite(stepperPins, steps[step % steps.length]); } function stop(){ digitalWrite(stepperPins[0],LOW); digitalWrite(stepperPins[1],LOW); digitalWrite(stepperPins[2],LOW); digitalWrite(stepperPins[3],LOW); } function go(f) { for (i=0;i<f;i++) { x=0; while (x<10) x++;//Delaying needed I don't know why. doStep(); } stop(); }
Can you explain me please how much degrees does the motor moves for each doStep() call?
I think it must move 5.625° for each doStep() but it is not, may be it is 32 time less than 5.625°.Also, I have tried both 5V and 9V , 9V has a great torque. 5V some times unable to start spinning, torque is not hight.
-
Hi @Gordon,
I don't know why, but when I remove the
while(i!=3)i++;
line stepper stops working. Change interval to 2-3-4 doesn't help. I have tried to change interval from 1 to 100 but no effect.Also change polarity didn't do any effect; Leds and motion is the same
digitalWrite(stepperPins[0],!(stepval&1)); digitalWrite(stepperPins[1],!(stepval&2)); digitalWrite(stepperPins[2],!(stepval&4)); digitalWrite(stepperPins[3],!(stepval&8));
-
Hello,
There is many questions, I'm sorry if I forgot to answer anyone.
At first, all 3 code work. The first code that write at the start of this thread works so slow.
The second one (http://forum.espruino.com/comments/12041929/) works good bot not fast as @DrAzzy's code (http://forum.espruino.com/comments/12042026/)
@DrAzzy code is the fastest (Apx. 13 RPM) When I use this code leds are not blinking in sequence any more. Leds allways on.I think that I'm wiring up correctly.
I'm using stm32F4DISCOVERY. I'm using my boards power supply(5+), today I'm going to try with +9 Volt external battery and write results here.
I have catched an interesting point.
@DrAzzy's code works good but only with console.log("doing step:"+step); line.
When I comment out this line it doesn't work :)Also, when I flash the code -with save()- in to the device and restart it @DrAzzy's code doesn't works. It's about console.log() command. I think console.log() command doesn't work if device is working stanalone.
So, I thought that it needs to wait a little bit before doing next step, here is my new code works good without comments and after save()
var step = 0; pinMode(D1,'output'); pinMode(D2,'output'); pinMode(D3,'output'); pinMode(D4,'output'); var steps = [0b0001,0b0011,0b0010,0b0110,0b0100,0b1100,0b1000,0b1001]; var stepperPins = [D4,D3,D2,D1]; // Change these to pins for your motor driver function doStep() { step++; i=0; while(i!=3)i++; //delay a little bit before doing steps. Better than console.log() var stepval=steps[step % steps.length]; digitalWrite(stepperPins[0],stepval&1); digitalWrite(stepperPins[1],stepval&2); digitalWrite(stepperPins[2],stepval&4); digitalWrite(stepperPins[3],stepval&8); } var stepInterval = setInterval(doStep, 1);
-
But I would want to understand why the code below doesn't work?It is expected to do same think but not.
var step = 0; var steps = [0b0001,0b0011,0b0010,0b0110,0b0100,0b1100,0b1000,0b1001]; var stepperPins = [D1,D2,D3,D4]; // Change these to pins for your motor driver function doStep() { step++; digitalWrite(stepperPins, steps[step % steps.length]); } var stepInterval = setInterval(doStep, 1);
-
Hi @russdx,
As you pointed; I have converted the code to javascript.at the end It worked great :)
Here is what I have donevar Pin0 = D1; var Pin1 = D2; var Pin2 = D3; var Pin3 = D4; var _step = 0; var boolean=true; var dir = true; function setup() { pinMode(Pin0, OUTPUT); pinMode(Pin1, OUTPUT); pinMode(Pin2, OUTPUT); pinMode(Pin3, OUTPUT); } function loop() { switch(_step){ case 0: digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, HIGH); break; case 1: digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, HIGH); break; case 2: digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, LOW); break; case 3: digitalWrite(Pin0, LOW); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, LOW); break; case 4: digitalWrite(Pin0, LOW); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); break; case 5: digitalWrite(Pin0, HIGH); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); break; case 6: digitalWrite(Pin0, HIGH); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); break; case 7: digitalWrite(Pin0, HIGH); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, HIGH); break; default: digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); break; } if(dir){ _step++; }else{ _step--; } if(_step>7){ _step=0; } if(_step<0){ _step=7; } } setup(); var stepInterdal = setInterval(loop, 1);
-
Thanks for your answer @Gordon
I have wired it up like thisStepper Pin 1-D1
Stepper Pin 2-D2
Stepper Pin 3-D3
Stepper Pin 4-D4My new code is below, I'm changing the sequence by code
var step = 0; var targetStep = 0; var steps = [0b0001,0b0010,0b0100,0b1000]; var stepperPins = [D1,D3,D2,D4]; function doStep() { step++; digitalWrite(stepperPins, steps[step % steps.length]); } var stepInterval = setInterval(doStep, 100);
Now leds blinking in 1-3-2-4 sequence, but now it is juddering back-forward with very very small and quick steps. It is not forwarding any more.
Also, I have forgot to tell; I'm using ULN2003 driver that shown here : http://www.instructables.com/id/BYJ48-Stepper-Motor/
Addition :
This step motor works really fast, please check this video. Stepper in the video works 20 times faster than mine. The same stepper and driver I have.
http://www.youtube.com/watch?v=KPR1Zt37GPE
-
-
Hello,
I'm using this code to run my step motor :
var step = 0; var steps = [0b0001,0b0010,0b0100,0b1000]; var stepperPins = [A3,A2,B10,B11]; // Change these to pins for your motor driver function doStep() { step++; digitalWrite(stepperPins, steps[step % steps.length]); } var stepInterval = setInterval(doStep, 200);
I'm using 28BYJ-48 – 5V stepper motor and controller : http://robocraft.ru/files/datasheet/28BYJ-48.pdf
But it works so slow, approximately 0.5RPM. When I set interval to 30ms it is a little bit faster, but approximately 1RPM. I have tried with 5V and 9V input.
What I am doing wrong?
-
-
-
I'm trying @JumJum 's code init function allways return false. I'm a newbie, I don't know if my device works and wired properly.
I have tracked the code, I see that code enter in to sendCommandWaitFor() function but it never enters inside sp.on('data',function(d){}); function.
I think it means that I can send command but it doesn't return any data. So, my device doesn't wired properly or not working.Is my logic right?
-
Thanks for your response, but no way, it didn't worked again. I'm giving up for now.
Here is the error message :Uncaught Error: No Acknowledgement at line 1 col 46 var wifi = require("ESP8266").connect(Serial4); ^ Uncaught Error: Field or method does not already exist, and can't create it on undefined at line 2 col 5 wifi.connect("WiFi_Name","WPA2_Key", function() { ^ in function called from system
-
@Gordon thanks for your response.
I have tried only this code, but result is the same:
Serial4.setup(115200, { rx: C11, tx : C10 }); var wifi = require("ESP8266").connect(Serial4, function() { wifi.connect("WiFi_Name","WPA2_Key", function() { }); });
I think you are pointing this thread on the forum : http://forum.espruino.com/conversations/253943/?offset=25
I'm going to check it.Thanks.
Hello,
I need to set some variables. Those variables are positions of motors that I use in my project. I want to set those variables because I want to continue work if device restarts somehow.
I'm setting the devices position after each step. When stepping job done, I use save() function to save current position.
But this way causes some conflicts, I think it doesn't only change value and save it. It appends some code after my pure code, then saves it.
Are there a better way to set variables without using an external storage device?