Very very late assignment 11 submission

 

 

Sorry for my late submission , I was facing a lot of problems that I was not aware how to solve, apparently my browser (Opera GX) does not support p5 – arduino communication, it took me ages to realize, and I compensated with putting extra effort into my assignment.

1)

 

2)

3)

// bounce detection and wind control
// pin setup
const int potPin = A0;  // pot for wind control
const int ledPin = 9;   // led that lights up on bounce

// vars
int potValue = 0;       // store pot reading
float windValue = 0;    // mapped wind value
String inputString = ""; // string to hold incoming data
boolean stringComplete = false; // flag for complete string
unsigned long ledTimer = 0;      // timer for led
boolean ledState = false;        // led state tracking
const long ledDuration = 200;    // led flash duration ms

void setup() {
  // set led pin as output
  pinMode(ledPin, OUTPUT);
  
  // start serial comm
  Serial.begin(9600);
  
  // reserve 200 bytes for inputString
  inputString.reserve(200);
}

void loop() {
  // read pot val
  potValue = analogRead(potPin);
  
  // map to wind range -2 to 2
  windValue = map(potValue, 0, 1023, -20, 20) / 10.0;
  
  // send wind value to p5
  Serial.print("W:");
  Serial.println(windValue);
  
  // check for bounce info
  if (stringComplete) {
    // check if it was a bounce message
    if (inputString.startsWith("BOUNCE")) {
      // turn on led
      digitalWrite(ledPin, HIGH);
      ledState = true;
      ledTimer = millis();
    }
    
    // clear string
    inputString = "";
    stringComplete = false;
  }
  
  // check if led should turn off
  if (ledState && (millis() - ledTimer >= ledDuration)) {
    digitalWrite(ledPin, LOW);
    ledState = false;
  }
  
  // small delay to prevent serial flood
  delay(50);
}

// serial event occurs when new data arrives
void serialEvent() {
  while (Serial.available()) {
    // get new byte
    char inChar = (char)Serial.read();
    
    // add to input string if not newline
    if (inChar == '\n') {
      stringComplete = true;
    } else {
      inputString += inChar;
    }
  }
}

 

 

Leave a Reply