Upgrade Kids-Car
22 berichten
• Pagina 3 van 3 • 1, 2, 3
Advertisement
Re: Upgrade Kids-Car
Nou eigenlijk nog niet eens.
Overigens gebruik de pedaal voor het aansturen van een servo, welke op zijn beurt de handbediening van een oma scootmobiel aanstuurt (trekt of duwt). Met de schakelaar ingedrukt rijd ik met dezelfde gaspedaal achteruit.
Project is overigens een kermis botsauto met een scoor mobiele elektrische aandrijving
Kijk zo ziet mijn code eruit:
#include <Servo.h>
Servo myservo; // Create servo object to control a servo
int buttonPin = 3; // Switch from forward to backward
int buttonState = 0; // variable for reading the pushbutton status
int potpin = A0; // Analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
myservo.attach(9); // attaches the servo on pin 2 to the servo object
Serial.begin(9600); // Just for calibration and debug
pinMode(13, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
//Backward
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1000)
val = map(val, 0, 1023, 80, 0); // scale it to use it with the servo (value between 0 and 80)
myservo.write(val); // sets the servo position according to the scaled value
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // waits for the servo to get there
}
else
//Forward
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1000)
val = map(val, 0, 1023, 80, 160); // scale it to use it with the servo (value between 0 and 80)
myservo.write(val); // sets the servo position according to the scaled value
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(100); // waits for the servo to get there
}
//just for calibration and debug
int sensorValue = analogRead(potpin); // read the input on analog pin 0:
float voltage = sensorValue * (5.0 / 1023.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
Serial.print(sensorValue); // print out the ADC value you read:
Serial.print(" ");
Serial.print(voltage); // print out the voltage value you read:
Serial.print(" ");
Serial.println(val); // print out the servo value you read:
}
Overigens gebruik de pedaal voor het aansturen van een servo, welke op zijn beurt de handbediening van een oma scootmobiel aanstuurt (trekt of duwt). Met de schakelaar ingedrukt rijd ik met dezelfde gaspedaal achteruit.
Project is overigens een kermis botsauto met een scoor mobiele elektrische aandrijving
Kijk zo ziet mijn code eruit:
#include <Servo.h>
Servo myservo; // Create servo object to control a servo
int buttonPin = 3; // Switch from forward to backward
int buttonState = 0; // variable for reading the pushbutton status
int potpin = A0; // Analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
myservo.attach(9); // attaches the servo on pin 2 to the servo object
Serial.begin(9600); // Just for calibration and debug
pinMode(13, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
//Backward
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1000)
val = map(val, 0, 1023, 80, 0); // scale it to use it with the servo (value between 0 and 80)
myservo.write(val); // sets the servo position according to the scaled value
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // waits for the servo to get there
}
else
//Forward
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1000)
val = map(val, 0, 1023, 80, 160); // scale it to use it with the servo (value between 0 and 80)
myservo.write(val); // sets the servo position according to the scaled value
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(100); // waits for the servo to get there
}
//just for calibration and debug
int sensorValue = analogRead(potpin); // read the input on analog pin 0:
float voltage = sensorValue * (5.0 / 1023.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
Serial.print(sensorValue); // print out the ADC value you read:
Serial.print(" ");
Serial.print(voltage); // print out the voltage value you read:
Serial.print(" ");
Serial.println(val); // print out the servo value you read:
}
22 berichten
• Pagina 3 van 3 • 1, 2, 3
Wie is er online?
Gebruikers in dit forum: Geen geregistreerde gebruikers en 13 gasten