Continuous servo rotation Arduino - arduino

I have a continuous rotation servo that I need to go clockwise and counterclockwise. I have found the mid point being at 100, and the clockwise and counterclockwise speeds I've found to be the most appropriate are 110 and 85, respectively. The servo rotates clockwise when button A is pushed, and counterclockwise when B is pushed.
I want a way to slow down the servo, once the button is released, at an exponential rate, so that it would say go from 110 to 100, or 85 to 100 in t (user definable) seconds. Is there a function anyone knows of that might help me accomplish this? or a way of doing this. Detailed answers appreciated as I am an Arduino noob.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// CONSTANTS
// PINS
const int crServo = 12; // sets pin 12 as servo
const int buttonPinCW = 2; // sets pin 2 as button; CW => clockwise => FOCUS FAR
const int buttonPinCC = 3; // sets pin 3 as button; CC => counterclockwise => FOCUS NEAR
const int ledPinB = 4; // sets pin 10 as LED
const int ledPinG = 5; // sets pin 10 as LED
const int ledPinR = 6; // sets pin 10 as LED
// SERVO PROPERTIES
const int crSpeedDefault = 100; // is the stay still position, motor should not turn
const int crSpeedCW = 110; // turns the motor full speed clockwise
const int crSpeedCC = 85; // turns the motor full speed counter-clockwise
// SET BUTTON STATES
int buttonStateCW = 0; //sets button 1 as off
int buttonStateCC = 0; // sets button 2 as off
void setup()
{
myservo.attach(crServo); // attaches the servo on pin 12 to the servo object
pinMode (buttonPinCW, INPUT); // sets button as input
pinMode (buttonPinCC, INPUT); // sets button as input
pinMode (ledPinB, OUTPUT); // sets led as output
pinMode (ledPinG, OUTPUT); // sets led as output
pinMode (ledPinR, OUTPUT); // sets led as output
myservo.write(crSpeedDefault); // default servo to crSpeedDefault
startup();
}
int startup() {
blinker(2, ledPinB);
blinker(1, ledPinG);
blinker(1, ledPinR);
}
void blinker(int count, int pin) {
for ( int x = 0; x < count; x++ )
{
digitalWrite(pin, HIGH);
delay(1000);
digitalWrite(pin, LOW);
delay(1000);
}
}
void loop()
{
buttonStateCW = digitalRead(buttonPinCW);
buttonStateCC = digitalRead(buttonPinCC);
// clockwise rotation
if (buttonStateCW == HIGH) {
digitalWrite(ledPinR, HIGH);
myservo.write(crSpeedCW);
// counterclockwise rotation
}
else if (buttonStateCC == HIGH) {
digitalWrite(ledPinG, HIGH);
myservo.write(crSpeedCC);
}
else {
myservo.write(crSpeedDefault);
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinG, LOW); // turn the LED off by making the voltage LOW
}
}
Updated with RMI's answer
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// CONSTANTS
// PINS
const int crServo = 12; // sets pin 12 as servo
const int buttonPinCW = 2; // sets pin 2 as button; CW => clockwise => FOCUS FAR
const int buttonPinCC = 3; // sets pin 3 as button; CC => counterclockwise => FOCUS NEAR
const int ledPinB = 4; // sets pin 10 as LED
const int ledPinG = 5; // sets pin 10 as LED
const int ledPinR = 6; // sets pin 10 as LED
const int t = 1; // slow down
// SERVO PROPERTIES
const int crSpeedDefault = 100; // is the stay still position, motor should not turn
const int crSpeedCW = 107; // turns the motor full speed clockwise
const int crSpeedCC = 87; // turns the motor full speed counter-clockwise
// SET BUTTON STATES
int buttonStateCW = 0; //sets button 1 as off
int buttonStateCC = 0; // sets button 2 as off
void setup()
{
myservo.attach(crServo); // attaches the servo on pin 12 to the servo object
pinMode (buttonPinCW, INPUT); // sets button as input
pinMode (buttonPinCC, INPUT); // sets button as input
pinMode (ledPinB, OUTPUT); // sets led as output
pinMode (ledPinG, OUTPUT); // sets led as output
pinMode (ledPinR, OUTPUT); // sets led as output
myservo.write(crSpeedDefault); // default servo to crSpeedDefault
startup();
}
int startup() {
//blinker(2, ledPinB);
//blinker(1, ledPinG);
//blinker(1, ledPinR);
}
void blinker(int count, int pin) {
for (int x = 0; x < count; x++)
{
digitalWrite(pin, HIGH);
delay(1000);
digitalWrite(pin, LOW);
delay(1000);
}
}
void loop()
{
buttonStateCW = digitalRead(buttonPinCW);
buttonStateCC = digitalRead(buttonPinCC);
// clockwise rotation
if (buttonStateCW == HIGH) {
digitalWrite(ledPinR, HIGH);
float speed = crSpeedCW;
Serial.print("CLOCKWISE-ROTATION \n");
for (int i = 0; i < t * 5; i++) {
speed += ((float)crSpeedDefault - speed)/ 10;
Serial.print(speed);
Serial.print("\n");
myservo.write((int)speed);
delay(100);
}
myservo.write(crSpeedCW);
}
else if (buttonStateCC == HIGH) {
digitalWrite(ledPinG, HIGH);
float speed = crSpeedCC;
Serial.print("COUNTER-CLOCKWISE-ROTATION \n");
for (int i = 0; i < t * 5; i++) {
speed += ((float)crSpeedDefault - speed) / 10;
Serial.print(speed);
Serial.print("\n");
myservo.write((int)speed);
delay(100);
}
myservo.write(crSpeedCC);
}
else {
myservo.write(crSpeedDefault);
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinG, LOW); // turn the LED off by making the voltage LOW
}
}

Try This.
void loop()
{
...
// clockwise rotation
if (buttonStateCW == HIGH) {
digitalWrite(ledPinR, HIGH);
float speed = crSpeedCW;
for (int i = 0; i < t * 5; i++) {
speed -= (speed - (float)crSpeedDefault) / 10;
myservo.write((int)speed);
delay(200);
}
myservo.write(crSpeedCW);
}
else if (buttonStateCC == HIGH) {
digitalWrite(ledPinG, HIGH);
// Do the same here in reverse
myservo.write(crSpeedCC);
}
else {
...
}
}
This will slow down the speed exponentially. You have to adjust hard coded numbers to suit your the ranges.

Related

Servo keeps jittering Arduino code for servo motor jittering

Servo keeps jittering at start up, buttons only reset to initial position being held, if let goes back to jittering.
There is a line of code as well for an osciloscope but im not using it if you can please point it out, also this is the original website and functioning, the tinkercad simulation works like intended, but on my arduino keeps jittering https://www.tinkercad.com/things/3jdNc6hhgZl-arduino-servo-wo-servo-lib
Edit: I've checked that on the simulation it also jitters..
const unsigned int _PERIOD_US = 2200;
const unsigned int _0_DEG_US = 500;
const unsigned int _90_DEG_US = 1500;
const unsigned int _180_DEG_US = 2500;
unsigned int us_elapsed = 0;
unsigned int duty_us = _90_DEG_US;
unsigned int aux = 0;
void setup()
{
pinMode(8, OUTPUT);
pinMode(4, INPUT);
pinMode(3, INPUT);
pinMode(2, INPUT);
digitalWrite(8, LOW);
}
void loop()
{
if (digitalRead(4) == LOW) {
duty_us = _0_DEG_US;
}
if (digitalRead(3) == LOW) {
duty_us = _90_DEG_US;
}
if (digitalRead(2) == LOW) {
duty_us = _180_DEG_US;
}
aux = micros() - us_elapsed;
if (aux >= duty_us) {
digitalWrite(8, LOW);
}
if (aux >= _PERIOD_US) {
digitalWrite(8, HIGH);
us_elapsed = micros();
}
}

Can you help me fix the error message I am getting on my code?

I have these two sets of code and they both run fine on their own, but when I am trying to merge them together I am getting a ton of error messages. I'm new to coding and don't really know a whole lot so I was wondering if someone could just quickly look it over and see what's wrong. The compiler is saying that there is an expected unqualified id before the '{' token at the bottom where I have the code for the pushbutton. However I have tried and removed this and then I get the same message but instead of '{' it says 'if'
Thank you very much
EDIT: I don't know what properly formatting an error message looks like but here is my attempt:
Complete_Code:69:1: error: expected unqualified-id before '{' token
{
^
exit status 1
expected unqualified-id before '{' token
Here is the code in question
int pinButton = 5;
#include <Servo.h>
int servo1Pin = 8;
Servo servo1;
#include <Servo.h>
#define mainArm 0
#define jib 1
#define bucket 2
#define slew 3
Servo servo[2]; //code used for attaching upto 4 servos
byte angle[2] = {90, 90}; // middle point for servo angle
byte potPin[2] = {A1, A2}; // input pins to attach your potentiometers
byte servoPin[2] = {7, 6};
void setup() {
Serial.begin(9600);
Serial.println("Starting DiggerCode.ino");
for (byte n = 0; n < 4; n++) {
servo[n].attach(servoPin[n]);
}
pinMode(pinButton, INPUT);
pinMode(12, OUTPUT); //red LED
pinMode(11, OUTPUT); // yellow LED
pinMode(10, OUTPUT); // green LED
servo1.attach(servo1Pin);
}
void loop() {
readPotentiometers();
moveServos();
delay(10);
}
void readPotentiometers() {
int potVal;
for (byte n = 0; n < 4; n++) {
potVal = analogRead(potPin[n]);
if (potVal < 200) { // dead zone for the joystick I used is 200 to 550.
angle[n] += 1;
if (angle[n] > 170) {
angle[n] = 170;
}
}
if (potVal > 550) { // deadzone upper value
angle[n] -= 1;
if (angle[n] < 10) {
angle[n] = 10;
}
}
}
}
void moveServos() {
for (byte n = 0; n < 4; n++) {
servo[n].write(angle[n]);
}
}
{
int stateButton = digitalRead(pinButton); //read the state of the button
if (stateButton == HIGH) { //if is pressed
Serial.println("Switch = High");
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
delay(1000);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
delay(1000);
digitalWrite(10, LOW);
delay(500);
servo1.write(180);
delay(1000);
// Make servo go to 180 degrees
servo1.write(90);
delay(1000);
servo1.write(180);
}
}
It seems you dropped the first things of function definition for some reason.
Add function return type, name and arguments for example
void anImportedFunction()
Before the part starting from
{
int stateButton = digitalRead(pinButton); //read the state of the button
You may also need to call the added function from somewhere.
I looked through your code and edited, some unnecessary and missing parts.
I checked it, no more syntax errors. I think it must work.
#include <Servo.h>
#define mainArm 0
#define jib 1
#define bucket 2
#define slew 3
Servo servo1;
Servo servo[4]; //code used for attaching upto 4 servos
int pinButton = 5;
int servo1Pin = 8;
byte angle[2] = {90, 90}; // middle point for servo angle
byte potPin[2] = {A1, A2}; // input pins to attach your potentiometers
byte servoPin[2] = {7, 6};
void setup() {
Serial.begin(9600);
Serial.println("Starting DiggerCode.ino");
for (byte n = 0; n <= 4; n++) {
servo[n].attach(servoPin[n]);
}
pinMode(pinButton, INPUT);
pinMode(12, OUTPUT); //red LED
pinMode(11, OUTPUT); // yellow LED
pinMode(10, OUTPUT); // green LED
servo1.attach(servo1Pin);
}
void loop() {
readPotentiometers();
moveServos();
delay(10);
}
void readPotentiometers() {
int potVal;
for (byte n = 0; n <= 4; n++) {
potVal = analogRead(potPin[n]);
if (potVal < 200) { // dead zone for the joystick I used is 200 to 550.
angle[n] += 1;
if (angle[n] > 170) {
angle[n] = 170;
}
}
if (potVal > 550) { // deadzone upper value
angle[n] -= 1;
if (angle[n] < 10) {
angle[n] = 10;
}
}
}
}
void moveServos() {
for (byte n = 0; n <= 4; n++) {
servo[n].write(angle[n]);
}
}
void Switch() //here instead of "Switch" you can use any function name you want
{
int stateButton = digitalRead(pinButton); //read the state of the button
if (stateButton == HIGH) { //if is pressed
Serial.println("Switch = High");
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
delay(1000);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
delay(1000);
digitalWrite(10, LOW);
delay(500);
servo1.write(180);
delay(1000);
// Make servo go to 180 degrees
servo1.write(90);
delay(1000);
servo1.write(180);
}
}

Arduino Combination Button?

I am trying to set up an Arduino Uno plus a RelayShield along with 6 push buttons(standard), and 3 LED's(red, yellow, green). Now what I have code to do is to take in a button push combination that is 6 pushes long(order only matters for what is set in the array). I have everything wired up to a breadboard and all that other jazz. My problem is, none of the LED's are working(I followed detailed instructions from instructables - minus the code), and it doesn't matter how many button pushes there are, only button 3 triggers the relay.... Now, for simplicities sake, I didn't wire up all 6 buttons, and I just shortened the array down to 3 and adjusted accordingly(I didn't want to hook up 6 buttons right now). Can someone verify this code for me? Or tell me what is wrong with it? Thanks in advance!
//button pins
const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
const int button4 = 5;
const int button5 = 6;
const int button6 = 7;
const int grnLed = 9;
const int redLed = 10;
const int yellowLed = 11;
const int openRelay = 8;
// how long is our code, kinda personal
const int codelen = 3;
//pin code values must match button pin values
char PIN[codelen] = {
'1', '2', '3'
};
// attempted combo
char attempt[codelen] = {
'0', '0', '0'
};
// attempt count
int z = 0;
void setup() {
// you've been setup
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
pinMode(button6, INPUT);
pinMode(openRelay, OUTPUT);
// set pullup resistor for buttons
digitalWrite(button1, HIGH);
digitalWrite(button2, HIGH);
digitalWrite(button3, HIGH);
digitalWrite(button4, HIGH);
digitalWrite(button5, HIGH);
digitalWrite(button6, HIGH);
// set openRelay state to open or closed
digitalWrite(openRelay, LOW);
}
void correctPIN()
{
pulseLED(grnLed, 3000);
digitalWrite(openRelay, HIGH);
delay(2000);
digitalWrite(openRelay, LOW);
delay(2000);
z = 0;
}
void incorrectPIN()
{
pulseLED(redLed, 3000);
z = 0;
}
void checkPIN()
{
int correct = 0;
int i;
for (i = 0; i < codelen; i++)
{
if (attempt[i] == PIN[i])
{
correct++;
}
}
if (correct == codelen)
{
correctPIN();
}
else
{
incorrectPIN();
}
for (int zz = 0; zz < codelen; zz++)
{
attempt[zz] = '0';
}
}
void checkButton(int button){
if (digitalRead(button) == LOW)
{
while (digitalRead(button) == LOW) { } // do nothing
//convert int to string for tracking/compare
char buttStr = button + '0';
attempt[z] = buttStr;
z++;
//light up led so we know btn press worked
pulseLED(yellowLed, 500);
}
}
void pulseLED(int ledpin, int msec) {
digitalWrite(ledpin, HIGH);
delay(msec);
digitalWrite(ledpin, LOW);
}
void loop() {
// check buttons
checkButton(button1);
checkButton(button2);
checkButton(button3);
checkButton(button4);
checkButton(button5);
checkButton(button6);
//if number of buttons pressed, z, matches code/pin length then check
if (z >= codelen)
{
checkPIN();
}
}

continuous rotation servo (arduino) responding to button press

I am trying to make a continous rotation servo move clockwise if button on pin2 is pressed, and counterclockwise if button on pin3 is pressed. I want the servo to keep moving in the direction set according to the button until the button is released. This is the code I have so far (I am new to arduino):
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// CONSTANTS
// PINS
const int crServo = 12; // sets pin 12 as servo
const int buttonPinCW = 2; // sets pin 2 as button; CW => clockwise => FOCUS FAR
const int buttonPinCC = 3; // sets pin 3 as button; CC => counterclockwise => FOCUS NEAR
const int ledPin = 10; // sets pin 10 as LED
// SERVO PROPERTIES
const int crSpeedDefault = 1500; // 1500 is the stay still position, motor should not turn
const int crSpeedCW = 1300; // 1300 turns the motor full speed clockwise
const int crSpeedCC = 1700; // 1700 turns the motor full speed counter-clockwise
const int crStepDefault = 2;
// SET BUTTON STATES
int buttonStateCW = 0; //sets button 1 as off
int buttonStateCC = 0; // sets button 2 as off
void setup()
{
myservo.attach(crServo); // attaches the servo on pin 12 to the servo object
pinMode (buttonPinCW, INPUT); // sets button as input
pinMode (buttonPinCC, INPUT); // sets button as input
pinMode (ledPin, OUTPUT); // sets led as output
myservo.write(crSpeedDefault); // default servo to crSpeedDefault
}
int slowFocusPull(int x){
int result;
result = abs(x - crSpeedDefault) / crStepDefault;
return result;
}
void loop()
{
buttonStateCW = digitalRead(buttonPinCW);
buttonStateCC = digitalRead(buttonPinCC);
// clockwise rotation
if (buttonStateCW == HIGH) {
digitalWrite(ledPin, HIGH);
myservo.write(slowFocusPull(crSpeedCW));
// counterclockwise rotation
} else if (buttonStateCC == HIGH) {
digitalWrite(ledPin, HIGH);
myservo.write(slowFocusPull(crSpeedCC));
} else {
digitalWrite(ledPin, LOW);
}
}
The issue lies in the function slowFocusPull. Basically I just want to be able to adjust the speed with just modifying the constant. Without this function everything works fine.
UPDATE: final loop for reference
void loop()
{
buttonStateCW = digitalRead(buttonPinCW);
buttonStateCC = digitalRead(buttonPinCC);
// clockwise rotation
if (buttonStateCW == HIGH) {
digitalWrite(ledPinR, HIGH);
float speed = crSpeedCW;
Serial.print("CLOCKWISE-ROTATION \n");
for (int i = 0; i < t * 5; i++) {
speed += ((float)crSpeedDefault - speed)/ 10;
Serial.print(speed);
Serial.print("\n");
myservo.write((int)speed);
delay(100);
}
myservo.write(crSpeedCW);
}
else if (buttonStateCC == HIGH) {
digitalWrite(ledPinG, HIGH);
float speed = crSpeedCC;
Serial.print("COUNTER-CLOCKWISE-ROTATION \n");
for (int i = 0; i < t * 5; i++) {
speed += ((float)crSpeedDefault - speed) / 10;
Serial.print(speed);
Serial.print("\n");
myservo.write((int)speed);
delay(100);
}
myservo.write(crSpeedCC);
}
else {
myservo.write(crSpeedDefault);
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinG, LOW); // turn the LED off by making the voltage LOW
}
}
Looks like your project would benefit from using Hardware Interrupts, which asynchronously call functions when events (like button presses) occur (these are perfect for controllers, and remove the overhead of polling).
Try wiring up the two pins and wiring up the buttons to pins 2 and 3 as is shown in this diagram:
Hardware interrupts literally interrupt the code, the uno has two such pins: digital pin 2 and digital pin 3 (this is really useful for robotics, also the mega has 6 such pins!)
here's a skeleton for how your code might want to look
void setup() {
attachInterrupt(0, goClockwise, RISING); //the "0" places arduino uno's interrupt pin 2 (for uno r3)
attachInterrupt(1, goCounterClockwise, RISING); //the "1" places interrupt for arduino uno's pin 3
}
void loop() {
delay(1000); dummy delay, code is handled in interrupt functions
}
void goClockwise () {
//runs when pin 2's button is pressed
//code for making servo go clockwise
}
void goCounterClockwise () {
//code triggered when pin 3's button is pressed
//code for ccw goes here
}
If you have any questions on I'd be happy to work through them with you.
Here's a link to the Arduino ref page for hardware interrupts:
click here to learn more about arduino hardware interrupts

How to control an Mcp41010 wiper position with an analog value

I am using an Mcp41010 digipot chip and am wondering how to vary the wiper position of the chip with an analog input voltage that I can adjust, I need a way of decrementing(--) the wiper position if the voltage goes over a certain point and incrementing(++) the wiper position of the chip back to the normal position this is some code that i found that just fades the wiper position up and down I need a way of controlling it. I am still very new to arduino so sorry if my explanation was clear enough.
int CS_signal = 12; // Chip Select signal onsul pin 2 of Arduino
int CLK_signal = 52; // Clock signal on pin 4 of Arduino
int MOSI_signal = 51; // MOSI signal on pin 5 of Arduino
byte cmd_byte2 = B00010001 ; // Command byte
int initial_value = 100; // Setting up the initial value
void initialize() { // send the command byte of value 100 (initial value)
spi_out(CS_signal, cmd_byte2, initial_value);
}
void spi_out(int CS, byte cmd_byte, byte data_byte){ // we need this function to send command byte and data byte to the chip
digitalWrite (CS, LOW); // to start the transmission, the chip select must be low
spi_transfer(cmd_byte); // invio il COMMAND BYTE
delay(2);
spi_transfer(data_byte); // invio il DATA BYTE
delay(2);
digitalWrite(CS, HIGH); // to stop the transmission, the chip select must be high
}
void spi_transfer(byte working) {
for(int i = 1; i <= 8; i++) { // Set up a loop of 8 iterations (8 bits in a byte)
if (working > 127) {
digitalWrite (MOSI_signal,HIGH) ; // If the MSB is a 1 then set MOSI high
} else {
digitalWrite (MOSI_signal, LOW) ; } // If the MSB is a 0 then set MOSI low
digitalWrite (CLK_signal,HIGH) ; // Pulse the CLK_signal high
working = working << 1 ; // Bit-shift the working byte
digitalWrite(CLK_signal,LOW) ; // Pulse the CLK_signal low
}
}
void setup() {
pinMode (CS_signal, OUTPUT);
pinMode (CLK_signal, OUTPUT);
pinMode (MOSI_signal, OUTPUT);
initialize();
Serial.begin(9600); // setting the serial speed
Serial.println("ready!");
}
void loop() {
for (int i = 0; i < 255; i++) {
spi_out(CS_signal, cmd_byte2, i);
Serial.println(i); delay(10);
}
for (int i = 255; i > 0; --i) {
spi_out(CS_signal, cmd_byte2, i);
Serial.println(i);
delay(10);
}
}
int CS_signal = 12; // Chip Select signal onsul pin 2 of Arduino
int CLK_signal = 52; // Clock signal on pin 4 of Arduino
int MOSI_signal = 51; // MOSI signal on pin 5 of Arduino
byte cmd_byte2 = B00010001 ; // Command byte
int initial_value = 100; // Setting up the initial value
void initialize() { // send the command byte of value 100 (initial value)
spi_out(CS_signal, cmd_byte2, initial_value);
}
void spi_out(int CS, byte cmd_byte, byte data_byte){ // we need this function to send command byte and data byte to the chip
digitalWrite (CS, LOW); // to start the transmission, the chip select must be low
spi_transfer(cmd_byte); // invio il COMMAND BYTE
delay(2);
spi_transfer(data_byte); // invio il DATA BYTE
delay(2);
digitalWrite(CS, HIGH); // to stop the transmission, the chip select must be high
}
void spi_transfer(byte working) {
for(int i = 1; i <= 8; i++) { // Set up a loop of 8 iterations (8 bits in a byte)
if (working > 127) {
digitalWrite (MOSI_signal,HIGH) ; // If the MSB is a 1 then set MOSI high
} else {
digitalWrite (MOSI_signal, LOW) ; } // If the MSB is a 0 then set MOSI low
digitalWrite (CLK_signal,HIGH) ; // Pulse the CLK_signal high
working = working << 1 ; // Bit-shift the working byte
digitalWrite(CLK_signal,LOW) ; // Pulse the CLK_signal low
}
}
void setup() {
pinMode (CS_signal, OUTPUT);
pinMode (CLK_signal, OUTPUT);
pinMode (MOSI_signal, OUTPUT);
initialize();
Serial.begin(9600); // setting the serial speed
Serial.println("ready!");
}
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
if(sensorValue <= 200){
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
int i = sensorValue;{
spi_out(CS_signal, cmd_byte2, i);
Serial.println(i);
}
}
}

Resources