Skip to content

Instantly share code, notes, and snippets.

@bluenex
Last active August 29, 2015 14:24
Show Gist options
  • Save bluenex/8295727b2b14ff91e328 to your computer and use it in GitHub Desktop.
Save bluenex/8295727b2b14ff91e328 to your computer and use it in GitHub Desktop.
Arduino example code from Peach.
#define PIN_LED 13

char buffer;
int time_delay = 1000;

void setup()
{
    Serial.begin(115200);
    pinMode(PIN_LED, OUTPUT);
}

void loop()
{
    digitalWrite(PIN_LED, HIGH);
    Serial.println("ON");
    delay(time_delay);
    digitalWrite(PIN_LED, LOW);
    Serial.println("OFF");
    delay(time_delay);
    if(Serial.available() > 0){
        buffer = Serial.read();
        Serial.print("Incoming = ");
        Serial.println(buffer, DEC);
        if(buffer =='D'){
            time_delay = 0;
            time_delay += (Serial.read()-48)*1000;
            time_delay += (Serial.read()-'0')*100;
            time_delay += (Serial.read()-'0')*10;
            time_delay += (Serial.read()-'0');
            Serial.println(time_delay);
            Serial.println(time_delay);
        }
    }
}

/*
millis();
delay();
delayMicroseconds();

// pinMode();
// digitalWrite();

// analogWrite(8, 255);

bool HIGH;
digitalRead(PIN);

int a;
analogRead(PIN);

*/
#define PIN_DC_MOTOR 11
#define PIN_LED 13

char buffer;
int speedMotor = 0;
int timeDelay = 1000;

void setup()
{
  Serial.begin(115200);
  pinMode(PIN_DC_MOTOR, OUTPUT);
  pinMode(PIN_LED, OUTPUT);
}

void loop()
{
  digitalWrite(PIN_LED, LOW);
  delay(timeDelay);
  if(Serial.available() > 0)
  {
      buffer = Serial.read();
      Serial.print("Incoming buffer = ");
      Serial.println(buffer);
      if(buffer == 'M')
      {
          speedMotor = 0;
          speedMotor += (Serial.read()-'0')*100;
          speedMotor += (Serial.read()-'0')*10;
          speedMotor += (Serial.read()-'0');
          Serial.print("motor speed = ");
          Serial.println(speedMotor);
          digitalWrite(PIN_LED, HIGH);
          analogWrite(PIN_DC_MOTOR, speedMotor);
      }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment