2017年1月5日 星期四

指標運用

int sum(int a,int b);
int (*f)(int x,int y)=sum;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  int s;
  s=(*f)(112,56);
  Serial.println(s);
}

void loop() {
  // put your main code here, to run repeatedly:

}
int sum(int a,int b)
{
  int total;
  total=a+b;
  return total;
}
=====================

void fun1();
void fun2();
void fun3();

void (*f[3])()={fun1,fun2,fun3};
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  (*f[0])();
  (*f[1])();
  (*f[2])();
}

void loop() {
  // put your main code here, to run repeatedly:

}
void fun1()
{
  Serial.println("block1");
}
void fun2()
{
  Serial.println("block2");
}
void fun3()
{
  Serial.println("block3");
}
==============================
#include <Keypad.h>
const byte numRows=4;
const byte numCols=4;

char Keymap[4][4]=
{
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'},
};
byte rowPins[numRows]={9,8,7,6};
byte colPins[numCols]={5,4,3,2};
 Keypad myKeypad=Keypad(makeKeymap(Keymap),rowPins,colPins,numRows,numCols);
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
char keypressed=myKeypad.getKey();
if (keypressed!=NO_KEY)
{
  Serial.println(keypressed);
}

}
======================================
#include <Keypad.h>
const byte numRows=4;
const byte numCols=4;

char Keymap[4][4]=
{
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'},
};
byte rowPins[numRows]={9,8,7,6};
byte colPins[numCols]={5,4,3,2};
 Keypad myKeypad=Keypad(makeKeymap(Keymap),rowPins,colPins,numRows,numCols);
 int led1=11;
 int led2=12;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
char keypressed=myKeypad.getKey();

if (keypressed!=NO_KEY)
{
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  Serial.println(keypressed);
  if (keypressed=='1')
  {
    digitalWrite(led1,HIGH);
  }
  else if (keypressed=='2')
  {
    digitalWrite(led2,HIGH);
  }
  else if (keypressed=='3')
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
  }
}

}
============================
馬達
const int motor=7;
void setup() {
  // put your setup code here, to run once:
 pinMode(motor,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(motor,HIGH);
  delay(2000);
  digitalWrite(motor,LOW);
  delay(1000);
}
==========================
const int motor=3;
int speed=0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  Serial.println("(+)--->up(-)--->stop");
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available()>0)
  {
    char k=Serial.read();
    Serial.print(k);
    if (k=='+')
    {
      speed=200;
    }
    if (k=='-')
    {
      speed=0;
    }
    analogWrite(motor,speed);
  }
}
==========================================
用Keypad驅動馬達
#include <Keypad.h>
const byte numRows=4;
const byte numCols=4;

char Keymap[4][4]=
{
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'},
};
byte rowPins[numRows]={9,8,7,6};
byte colPins[numCols]={5,4,3,2};
 Keypad myKeypad=Keypad(makeKeymap(Keymap),rowPins,colPins,numRows,numCols);

const int motor=11;
int speed=0;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
char keypressed=myKeypad.getKey();
if (keypressed!=NO_KEY)
{
  Serial.println(keypressed);
  if (keypressed=='1')
  {
    speed=200;
  }
  if (keypressed=='2')
  {
    speed=0;
  }
  analogWrite(motor,speed);
}

}

沒有留言:

張貼留言