Wednesday, March 30, 2011

code

 Servo Motor Control= code used to test if the motor worked and try to get the motor
spin in the way i wanted it to  
int servoPin = 2;     // Control pin for servo motor
 int minPulse = 500;   // Minimum servo position
 int maxPulse = 2500;  // Maximum servo position
 int pulse = 0;        // Amount to pulse the servo

 long lastPulse = 0;    // the time in milliseconds of the last pulse
 int refreshTime = 20; // the time needed in between pulses

 int analogValue = 0;  // the value returned from the analog sensor
 int analogPin = 0;    // the analog pin that the sensor's on

 void setup() {
  pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
  pulse = minPulse;           // Set the motor position value to the minimum
  Serial.begin(9600);
 }

 void loop() {
  analogValue = analogRead(analogPin); 
pulse = map(analogValue,0,1023,minPulse,maxPulse);   
// pulse the servo again if rhe refresh time (20 ms) have passed:
  if (millis() - lastPulse >= refreshTime) {
    digitalWrite(servoPin, HIGH);   // Turn the motor on
    delayMicroseconds(pulse);       // Length of the pulse sets the motor position
    digitalWrite(servoPin, LOW);    // Turn the motor off
    lastPulse = millis();           // save the time of the last pulse
  }
 }
led and relays= simple code that tells the microcomputer to send a change in current that turns on and off the different leds and relays    
;    // pin that the sensor is attached to
const int ledPin = 7;       // pin that the LED is attached to
void setup() {
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(3, INPUT);
}
 
void loop() {
    digitalWrite(ledPin, HIGH);
    delay(10000);
    digitalWrite(ledPin,LOW);
   delay(10000);
  }
Coin Sensor= code that used to test the coin sensor and a light attached to it
int inputPin = 3;    // pin that the sensor is attached to
int ledPin = 7;       // pin that the LED is attached to
int val = 0; // variable for reading the pin status
int prevState = 0;
void setup() {
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(inputPin, INPUT);
}
 
void loop() {
prevState = val;
val = digitalRead(inputPin); // read input value
if (val != prevState) {
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH);
delay(10000);
} else {
digitalWrite(ledPin, LOW);
}
}
}
Tilt sensor=  code to test tilt sensor with an led attached to it
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

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

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);    
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);    
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
    // turn LED on:    
    digitalWrite(ledPin, HIGH);
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

No comments:

Post a Comment