After so many requests of my code of my Gyrocar Toy on my YouTube channel. I decided to post my code here. This code is very primitive as I want to keep everything as simple as possible. Here is the complete code:
#include <Servo.h> Servo myservo; int potPin = 2; // select the input pin for the potentiometer int val = 86; // variable to store the value coming from the sensor int outval = 86; boolean pushedfront = false; boolean pushedrear = false; int lastval = 86; void setup() { myservo.attach(9); } void loop() { val = analogRead(potPin); // read the value from the sensor val = map(val, 0, 1023, -80, 279); outval = val; if(val<70&&val>35) outval-=4; else if(val>120) outval+=3; if(val>136){ outval=136; } else if(val<26){ outval=26; } myservo.write(outval); if(outval>110 && pushedfront==false){ myservo.write(140); delay(200); pushedfront=true; } else if(outval<70 && pushedrear==false){ myservo.write(35); delay(200); pushedrear=true; } if(outval+lastval>=220){ pushedrear=false; } else if(outval==50){ pushedfront=false; } lastval=outval; }
Please note that this toy is not yet finished. So maybe I have to make change of this code some time in the future.