HELP!! I need to build an electric vehicle for my Science Olympiad club. I attached the rules below. In a nutshell the vehicle needs to be travel the first 850 cm as quickly as possible, and then travel to a distance (between 900 and 1200 cm(50 to 350 cm if you subtract the first 850)) and stop as accurately as possible.
I have just joined the Arduino community and don't have enough time to learn it and balance my other responsibilities. The proportion system must be powered by 9v or less.
My idea:
Set the distance (after the first 850 cm) using a potentiometer and displaying it in binary on 9 LEDs
Then I would click a Button to signify that the distance shown is the one I want
I would want the circuit to wait for another button input before writing 255 pwm to the transistor that controls the the 4 dc 9 volt motors YES I NEED THE POWER
Then using a wheel marked black every other cm use a color sensor to count the number of centimeters traveled
When the Vehicle has traveled the first 850 cm it will slow down to a minimum speed and stop after traveling the distance specified from the potentiometer
The microcontroller will then trigger a servo to jam the gears(after turning off the motor)
All along the way Using color sensors and a servo to make sure the vehicle stays on the track(there will be a centerline)
These our ideas feel free to give suggestions and PLEASE HELP by maybe coding a section or the code and showing me the circuit diagram
THANK YOU SO MUTCH!!!!
Well no
I Have in the past four days have been learning as much of c for Arduino as i can.
I have compiled a code several times on 123d circuits.
How do you Insert Code on a form
These are my codes but I am not finished
#include <Servo.h>
Servo servo;
int motorPin = 3;
int pot = 0;
int distance = 0;
int speed = 255;
int buttonpin = 4;
int buttonstate = 0;
int displacement =0;
void setup() {
pinMode(motorPin, OUTPUT);
pinMode(buttonpin, INPUT);
Serial.begin(9600);
servo.attach(5);
}
void loop(){
while (1){
servo.write(180);
}
delay(1000);
while ( digitalRead(buttonpin) == LOW)
{
buttonstate = digitalRead(buttonpin);
Serial.println(buttonstate);
pot = analogRead(A0);
distance =( map(pot,0,1023,0,350)+850);
Serial.print(distance);
delay(10);
}
delay(100);
while (digitalRead(buttonpin) == LOW){}
analogWrite(motorPin,speed);
while (displacement<850)
{// insert code for calculating displacement from mouse
displacement = displacement+1;
}
while (displacement<distance){
analogWrite(motorPin,50);
}
servo.write(180);
while (1){}
}
and
int buttonPin = 2;
int buttonState = LOW;
int motorPin = 3;
int potPin = A0;
int potValue = 0;
int dist = 0;
int lastButtonState = LOW;
int buton(int x , int y){
// the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
boolean running = false;
void setup(){
pinMode(buttonPin, INPUT);
pinMode(motorPin, OUTPUT);
pinMode(potPin, input);
Serial.begin(9600);
}
void loop()
{
Serial.print(buttonState);
delay(2000);
while ( butonState == high ){
potValue = analogRead(potPin);
dist = map(potValue,0,1023,0,350)+850;
int lastButtonState = LOW; // the previous reading from the input pin
if (digitalRead(buttonPin) == LOW)
{ // switch is pressed - pullup keeps pin high normally
delay(100); // delay to debounce switch
running = !running; // toggle running variable
digitalWrite(LEDpin, running) // indicate via LED
}
}
}
}
So, when loop() gets called, forever (re)write the servo to position 180. You might as well delete the rest of the code in loop(). It will never be executed.
These were just tests so there are some points that i just wanted to stop the code.
#include <Servo.h>
Servo servo;
int motorPin = 3;
int pot = 0;
int distance = 0;
int speed = 255;
int buttonpin = 4;
int buttonstate = 0;
int displacement =0;
void setup() {
pinMode(motorPin, OUTPUT);
pinMode(buttonpin, INPUT);
Serial.begin(9600);
servo.attach(5);
}
void loop(){
while (1){
servo.write(180);
}
delay(1000);
while ( digitalRead(buttonpin) == LOW)
{
buttonstate = digitalRead(buttonpin);
Serial.println(buttonstate);
pot = analogRead(A0);
distance =( map(pot,0,1023,0,350)+850);
Serial.print(distance);
delay(10);
}
delay(100);
while (digitalRead(buttonpin) == LOW){}
analogWrite(motorPin,speed);
while (displacement<850)
{// insert code for calculating displacement from mouse
displacement = displacement+1;
}
while (displacement<distance){
analogWrite(motorPin,50);
}
servo.write(180);
while (1){}
}
int buttonPin = 2;
int buttonState = LOW;
int motorPin = 3;
int potPin = A0;
int potValue = 0;
int dist = 0;
int lastButtonState = LOW;
int buton(int x , int y){
// the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
boolean running = false;
void setup(){
pinMode(buttonPin, INPUT);
pinMode(motorPin, OUTPUT);
pinMode(potPin, input);
Serial.begin(9600);
}
void loop()
{
Serial.print(buttonState);
delay(2000);
while ( butonState == high ){
potValue = analogRead(potPin);
dist = map(potValue,0,1023,0,350)+850;
int lastButtonState = LOW; // the previous reading from the input pin
if (digitalRead(buttonPin) == LOW)
{ // switch is pressed - pullup keeps pin high normally
delay(100); // delay to debounce switch
running = !running; // toggle running variable
digitalWrite(LEDpin, running) // indicate via LED
}
}
}
}
Can someone PLEASE define the errors in this code.
const int motorPin = 3; //define pin assignments
const int buttonPin = 2;
const int LEDPin1 = 0;
const int LEDPin3 = 1;
const int LEDPin4 = 5;
const int LEDPin5 = 6;
const int LEDPin6 = 7;
const int LEDPin7 = 8;
const int LEDPin8 = 9;
const int LEDPin9 = 10;
int buttonState = LOW;
//atempt to make function to debounce buttonstate
/*
int button (buttonPin);
{
int lastButtonState = LOW;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState)
{
buttonState = reading;
}
}
return buttonState
}
*/
int potPin = A0;
void setup()
{
pinMode(motorPin,OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
//attempt to make loop that runs untill button is pressed. takes pot value maps it and converts it to binary and displays it on leds
do
{
int dist = map(analogRead(potPin),0,1023,0,350);
if ((dist%2)>0)
{
digitalWrite(LEDPin1,HIGH);
}
if ((dist%2)=0)
{
digitalWrite(LEDPin1,LOW);
}
if ((dist%4)>0)
{
digitalWrite(LEDPin2,HIGH);
}
if ((dist%4)=0)
{
digitalWrite(LEDPin2,LOW);
}
if ((dist%8)>0)
{
digitalWrite(LEDPin3,HIGH);
}
if ((dist%8)=0)
{
digitalWrite(LEDPin3,LOW);
}
if ((dist%16)>0)
{
digitalWrite(LEDPin4,HIGH);
}
if ((dist%16)=0)
{
digitalWrite(LEDPin4,LOW);
}
if ((dist%32)>0)
{
digitalWrite(LEDPin5,HIGH);
}
if ((dist%32)=0)
{
digitalWrite(LEDPin5,LOW);
}
if ((dist%64)>0)
{
digitalWrite(LEDPin6,HIGH);
}
if ((dist%64)=0)
{
digitalWrite(LEDPin6,LOW);
}
if ((dist%128)>0)
{
digitalWrite(LEDPin7,HIGH);
}
if ((dist%128)=0)
{
digitalWrite(LEDPin7,LOW);
}
if ((dist%256)>0)
{
digitalWrite(LEDPin8,HIGH);
}
if ((dist%256)=0)
{
digitalWrite(LEDPin8,LOW);
}
if ((dist%512)>0)
{
digitalWrite(LEDPin9,HIGH);
}
if ((dist%512)=0)
{
digitalWrite(LEDPin9,LOW);
}
}while (digitalRead (buttonPin)==LOW);
These should all be byte for starters, none have values above 255. Save a bunch of memory right there.
const int motorPin = 3; //define pin assignments
Time related variables - make these unsigned long
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
This code appears to be outside of loop(), yet is not a function.
}while (digitalRead (buttonPin)==LOW);
const int LEDPin1 = 0;
const int LEDPin3 = 1;
const int LEDPin4 = 5;
const int LEDPin5 = 6;
const int LEDPin6 = 7;
const int LEDPin7 = 8;
const int LEDPin8 = 9;
const int LEDPin9 = 10;
So I am using 123d circuits for prototyping
this code is to take 10 slide switch inputs and convert it to a number
I cant seem to get the if command to work no mater if I say low or high it always executes the code
this is just for input the rest of the code is not complete
for (x=0;x<=8;(x++))
{
pinMode(x,INPUT);
}
}
void loop()
{
while(1)//waiting for button action to return high
{
for ( int x=0;x<=8;x++)
{
dist=0;
if (digitalRead(x)==HIGH) dist = dist + pow(2,(x+1));
delay(20);
}
Serial.println(dist);
}
//after bution returns true wait for button to return true again
}/code]