Sunday, November 27, 2011

Logic for the cardboard sextapus

When executed, the following code will modulate two strings of LEDs based on the extent of flexing detected by one of two flex sensors (one for each string). A push button is incorporated so that the user can disconnect the analog input from the flex sensors to the LED string outputs. Basically, this would allow the user to transport the sextapus without turning it on and draining the battery.

int flexPin = A0;
int flexPinT = A1;
int LEDPin = 6;
int LEDPinT = 7;
int buttonPin = 10;
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
 pinMode(buttonPin, INPUT);    // initialize the button pin as a input
 pinMode(LEDPin, OUTPUT);      // initialize the LED as an output
 pinMode(LEDPinT, OUTPUT);      // initialize the LED as an output
 Serial.begin(9600);           // Set up serial communication at 9600bps
}


void loop(){
    // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter++;
    }
    else {
      // if the current state is LOW then the button
      // went from on to off:
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;

if (buttonPushCounter % 2 == 0) {
 int ledbright = map(analogRead(flexPin), 0, 300, 260, 0);
 int ledbrightT = map(analogRead(flexPinT), 0, 300, 260, 0);

 constrain (ledbright, 0, 273);
 constrain (ledbrightT, 0, 273);

 if (ledbright < 50) {
   analogWrite(LEDPin, 0);
 }
 if (ledbrightT < 50) {
   analogWrite(LEDPinT, 0);
 }


 if (ledbright >= 50) {
   int LED = ledbright - 50;
   analogWrite(LEDPin, LED);
 }

 if (ledbrightT >= 50) {
   int LEDs = ledbrightT - 50;
   analogWrite(LEDPinT, LEDs);
 }

 }
}

No comments:

Post a Comment