Thursday, November 7, 2019

Maya PM 1 hour Rube-Goldberg

So we designed something that would:
1. Count Marbles
2.Turn on a motor when 4 marbles were counted
3.Whack a magnet when the motor turned on
4.Magnet triggers hall magnet sensor
5.LED turns on, final output
Our code is a basic untested version but this is what we got:

int ldrValue = 0;
const int Ainput=A0;
int range = 0;
int Dark;
int motorPin=5;
int LED=2;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  attachInterrupt(0, magnet_detect, RISING);//thing for the magnet sensor
  pinMode(motorPin,OUTPUT);
  pinMode(LED,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  ldrValue = analogRead(Ainput);
  Serial.println(ldrValue);
  range = map(ldrValue, 0, 1023, 0, 255);//Light sensor
 
  Serial.println(range);//counts when it is dark, meaning a marble has rolled over the sensor
  if(range<45)
  {
  Serial.println("DARK");
  Dark=Dark++;
  }

  if (Dark>=4)
  {
    digitalWrite(motorPin,HIGH);
    delay(2000);//turns on motor when 4 marbles roll over
    }
   
}

 void magnet_detect() {
   Serial.println("detect");//magnet sensor that turns on led
   digitalWrite(LED,HIGH);
 }

No comments:

Post a Comment