project G:Prototype A Arduino Code

From student
Jump to: navigation, search

/*

Code for making two DC motor move simutaneously. A potentiometer controls the speed of the motors whilst a push buttons controls the direction of the rotation. An 10 A H-Bridge is used to connect the Arduino and the two 12V motors.


const int buttonPin = 4; // the number of the pushbutton pin const int buttonledPin = 11; // the number of the LED pin const int fadeledPin = 10; const int sensorPin = A0;


// variables will change: int buttonState = 0; // variable for reading the pushbutton status int sensorValue = 0; boolean lastbuttonState =0; int buttonledState =0;

void setup()

{

                 // initialize the LED pin as an output:
 pinMode(buttonledPin, OUTPUT);     
                 // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT);    

}

void loop() {

 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);
 sensorValue = analogRead(sensorPin);


//flipping of a switch

 if ((buttonState == 0) && (lastbuttonState==1)){
   lastbuttonState = 0;}

 else if ((buttonState == 1) && (lastbuttonState==0)){
   lastbuttonState = 1;
   if (buttonledState==0) buttonledState = 1;
   else buttonledState = 0; }
  

// turn the switch LED on:

   digitalWrite(buttonledPin, buttonledState); 
   

// set the fade LED:

   analogWrite(fadeledPin, sensorValue/3);
  

delay (100);

}

Personal tools
Namespaces
Variants
Actions
Projects
Atoms
Toolbox