Kode til bruk på tekologidagene


Dette er testkode for motor delen

const int MotorA_IA = 2;
const int MotorA_IB = 3; //PWM
const int MotorB_IA = 4;
const int MotorB_IB = 5; //PWM

const int Motor2A_IA = 44;
const int Motor2A_IB = 45; //PWM
const int Motor2B_IA = 47;
const int Motor2B_IB = 46;//PWM

const int KOMMUNIKASJON= 12;

const int MOTOR1 = 1;
const int MOTOR2 = 2;
const int MOTOR3 = 3;
const int MOTOR4 = 4;

void drive(int Motor,int Speed)
{
int MotorPinA = MotorA_IA;
int MotorPinB = MotorA_IB;

if (Speed > 255 || Speed < -255)
Speed = 0;

Speed *= -1;

switch (Motor)
{
case MOTOR1:
MotorPinA = MotorA_IA;
MotorPinB = MotorA_IB;
break;
case MOTOR2:
MotorPinA = MotorB_IA;
MotorPinB = MotorB_IB;
break;
case MOTOR3:
MotorPinA = Motor2A_IA;
MotorPinB = Motor2A_IB;
break;
case MOTOR4:
MotorPinA = Motor2B_IA;
MotorPinB = Motor2B_IB;
break;
}

if (Speed >= 0)
{
digitalWrite(MotorPinA, 0);
analogWrite(MotorPinB, Speed);
}
else
{
digitalWrite(MotorPinA, 1);
analogWrite(MotorPinB, 255+Speed);
}
}

void setup()
{
Serial.begin(9600);
pinMode(MotorA_IA, OUTPUT);
pinMode(MotorA_IB, OUTPUT);
pinMode(MotorB_IA, OUTPUT);
pinMode(MotorB_IB, OUTPUT);
pinMode(Motor2A_IA, OUTPUT);
pinMode(Motor2A_IB, OUTPUT);
pinMode(Motor2B_IA, OUTPUT);
pinMode(Motor2B_IA, OUTPUT);

pinMode(KOMMUNIKASJON, OUTPUT);
}

void loop()
{

//Her skal koden deres stå

}

 


One response to “Kode til bruk på tekologidagene”

  1. Dette er kode for lys og sirene

    const int speakerOut = 9;
    const int c=3830; // 261 Hz
    const int d=3400; // 294 Hz
    const int e=3038; // 329 Hz
    const int f=2864; // 349 Hz
    const int g=2550; // 392 Hz
    const int a=2272; // 440 Hz
    const int h=2028; // 493 Hz
    const int C=1912; // 523 Hz

    const int LED1 = 1;
    const int LED2 = 2;
    const int LED3 = 3;
    const int LED4 = 4;

    const int AV = 0;
    const int ROD = 1;
    const int BLA = 3;

    const int KOMUNIKASJON=12;

    void setup()
    {
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(KOMUNIKASJON, INPUT_PULLUP);

    }

    void playtone(long int duration, int tone_)
    {
    if (tone_ == 0)
    {
    delay(duration);
    }
    else
    {
    duration *= 1000;
    long elapsed_time = 0;
    while (elapsed_time < duration) { digitalWrite(speakerOut,HIGH); delayMicroseconds(tone_ / 2); // DOWN digitalWrite(speakerOut, LOW); delayMicroseconds(tone_ / 2); // Keep track of how long we pulsed elapsed_time += (tone_); } } } void farge(int LED, int Farge) { int pinR= 2; int pinB= 4; switch (LED) { case LED1: pinR= 2; pinB= 3; break; case LED2: pinR= 4; pinB= 5; break; case LED3: pinR= 6; pinB= 7; break; case LED4: pinR= 8; pinB= 9; break; } switch (Farge) { case AV: digitalWrite(pinR, LOW); digitalWrite(pinB, LOW); break; case ROD: digitalWrite(pinR, HIGH); digitalWrite(pinB, LOW); break; case BLA: digitalWrite(pinR, LOW); digitalWrite(pinB, HIGH); break; } } void loop() { //Her skal koden deres stå }

Leave a Reply