繁体   English   中英

Arduino触摸代码在Spark Core中不起作用

Arduino touch code is not Working in spark core

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

嗨,我找到了使用石墨和纸的触摸界面的Arduino代码,但是该代码在Spark Core中不起作用,如下所示,Arduino代码

// Pin for the LED
int LEDPin = 7;
// Pin to connect to your drawing
int capSensePin = 0;
// This is how high the sensor needs to read in order
//  to trigger a touch.  You'll find this number
//  by trial and error, or you could take readings at 
//  the start of the program to dynamically calculate this.
int touchedCutoff = 60;

void setup(){
  Serial.begin(9600);
  // Set up the LED
  pinMode(LEDPin, OUTPUT);
  digitalWrite(LEDPin, LOW);
}

void loop(){
  // If the capacitive sensor reads above a certain threshold,
  //  turn on the LED
  if (readCapacitivePin(capSensePin) > touchedCutoff) {
    digitalWrite(LEDPin, HIGH);
  }
  else {
    digitalWrite(LEDPin, LOW);
  }

  // Every 500 ms, print the value of the capacitive sensor
  if ( (millis() % 500) == 0){
    Serial.print("Capacitive Sensor on Pin 2 reads: ");
    Serial.println(readCapacitivePin(capSensePin));
  }
}

// readCapacitivePin
//  Input: Arduino pin number
//  Output: A number, from 0 to 17 expressing
//          how much capacitance is on the pin
//  When you touch the pin, or whatever you have
//  attached to it, the number will get higher
//  In order for this to work now,
// The pin should have a 1+Megaohm resistor pulling
//  it up to +5v.
uint8_t readCapacitivePin(int pinToMeasure){
  // This is how you declare a variable which
  //  will hold the PORT, PIN, and DDR registers
  //  on an AVR
  volatile uint8_t* port;
  volatile uint8_t* ddr;
  volatile uint8_t* pin;
  // Here we translate the input pin number from
  //  Arduino pin number to the AVR PORT, PIN, DDR,
  //  and which bit of those registers we care about.
  byte bitmask;
  if ((pinToMeasure >= 0) && (pinToMeasure <= 7)){
    port = &PORTD;
    ddr = &DDRD;
    bitmask = 1 << pinToMeasure;
    pin = &PIND;
  }
  if ((pinToMeasure > 7) && (pinToMeasure <= 13)){
    port = &PORTB;
    ddr = &DDRB;
    bitmask = 1 << (pinToMeasure - 8);
    pin = &PINB;
  }
  if ((pinToMeasure > 13) && (pinToMeasure <= 19)){
    port = &PORTC;
    ddr = &DDRC;
    bitmask = 1 << (pinToMeasure - 13);
    pin = &PINC;
  }
  // Discharge the pin first by setting it low and output
  *port &= ~(bitmask);
  *ddr  |= bitmask;
  delay(1);
  // Make the pin an input WITHOUT the internal pull-up on
  *ddr &= ~(bitmask);
  // Now see how long the pin to get pulled up
  int cycles = 16000;
  for(int i = 0; i < cycles; i++){
    if (*pin & bitmask){
      cycles = i;
      break;
    }
  }
  // Discharge the pin again by setting it low and output
  //  It's important to leave the pins low if you want to 
  //  be able to touch more than 1 sensor at a time - if
  //  the sensor is left pulled high, when you touch
  //  two sensors, your body will transfer the charge between
  //  sensors.
  *port &= ~(bitmask);
  *ddr  |= bitmask;

  return cycles;
}

Spark Core抛出以下错误

在../inc/spark_wiring.h:29:0中包含的文件中,

from ../inc/application.h:29,
from new_lwed.cpp:3:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
new_lwed.cpp: In function 'uint8_t readCapacitivePin(int)':
new_lwed.cpp:57:13: error: 'PORTD' was not declared in this scope
volatile uint8_t* pin; 
^
new_lwed.cpp:58:12: error: 'DDRD' was not declared in this scope
// Here we translate the input pin number from 
^
new_lwed.cpp:60:12: error: 'PIND' was not declared in this scope
// and which bit of those registers we care about. 
^
new_lwed.cpp:63:13: error: 'PORTB' was not declared in this scope
port = &PORTD; 
^
new_lwed.cpp:64:12: error: 'DDRB' was not declared in this scope
ddr = &DDRD; 
^
new_lwed.cpp:66:12: error: 'PINB' was not declared in this scope
pin = &PIND; 
^
new_lwed.cpp:69:13: error: 'PORTC' was not declared in this scope
port = &PORTB; 
^
new_lwed.cpp:70:12: error: 'DDRC' was not declared in this scope
ddr = &DDRB; 
^
new_lwed.cpp:72:12: error: 'PINC' was not declared in this scope
pin = &PINB; 
^
make: *** [new_lwed.o] Error 1
1 个回复

Spark Core使用与Arduino不同的CPU,因此PORTCDDRC等常量不可用。 电容库已被移植: https : //community.spark.io/t/include-capacitivesensor-library/2965/30,因此您可以包含CapTouch库并使用示例。

1 验证码在 Arduino 中不起作用

我是 Arduino 的新手,我正在尝试制作一个从电视遥控器接收 IR 代码的程序,将它们用作 4 个数字密码,在您按下每个按钮时点亮 LED。 然后将代码与硬编码的代码进行比较。 在本例中为 1234。我做了一个函数来验证输入的值是否等于通过。 如果是这样,点亮绿色 LED,否则点亮红色 LED。 ...

2 Arduino代码不起作用

我的计划是制造可调速频闪灯。 我只是在学习编码,这就是我到目前为止所拥有的。 我在数字7的LED +上有一个220欧姆的电阻,在模拟0上有一个电位计,它是一个10K电位计,一侧连接至5v +,另一侧接地。 我的问题是LED保持点亮,打开锅盖只会改变亮度。 关于做什么的任何帮助-不仅 ...

2012-01-19 01:56:41 2 679   arduino
4 Arduino键盘简单代码不起作用

我不明白这个简单的代码,该代码经认证不起作用。 我收到消息:小键盘:41:错误:未在此范围内声明“ led_pin”,否则digitalWrite(led_pin,LOW); 而且,如果我将另一个ledd_pin声明放入循环中,则会收到消息:在函数'void loop()'中:Key ...

5 为什么此Arduino代码不起作用? [关闭]

为什么此代码不起作用? 我正在http://www.codebender.cc上运行此Arduino代码,但无法在我的Arduino Uno上运行。 在串行监视器上运行时,什么都不会打印出来,我也不知道为什么? 看一看... ...

2016-03-13 19:54:28 1 33   arduino
6 组合循环在Arduino中不起作用

我有一个arduino代码正在执行这样的任务: 当它们分别运行时,这些循环将正确运行。 但是问题来了,当我尝试结合两个循环即 以下是完整的代码: 如果您需要更多帮助,我在这里。 请发表评论。 ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM